//! Control `log` level with a `--verbose` flag for your CLI //! //! # Examples //! //! To get `--quiet` and `--verbose` flags through your entire program, just `flatten` //! [`Verbosity`]: //! ```rust,no_run //! # use clap::Parser; //! # use clap_verbosity_flag::Verbosity; //! # //! # /// Le CLI //! # #[derive(Debug, Parser)] //! # struct Cli { //! #[command(flatten)] //! verbose: Verbosity, //! # } //! ``` //! //! You can then use this to configure your logger: //! ```rust,no_run //! # use clap::Parser; //! # use clap_verbosity_flag::Verbosity; //! # //! # /// Le CLI //! # #[derive(Debug, Parser)] //! # struct Cli { //! # #[command(flatten)] //! # verbose: Verbosity, //! # } //! let cli = Cli::parse(); //! # #[cfg(feature = "log")] //! env_logger::Builder::new() //! .filter_level(cli.verbose.log_level_filter()) //! .init(); //! ``` //! //! By default, this will only report errors. //! - `-q` silences output //! - `-v` show warnings //! - `-vv` show info //! - `-vvv` show debug //! - `-vvvv` show trace //! //! By default, the log level is set to Error. To customize this to a different level, pass a type //! implementing the [`LogLevel`] trait to [`Verbosity`]: //! //! ```rust,no_run //! # use clap::Parser; //! use clap_verbosity_flag::{Verbosity, InfoLevel}; //! //! /// Le CLI //! #[derive(Debug, Parser)] //! struct Cli { //! #[command(flatten)] //! verbose: Verbosity<InfoLevel>, //! } //! ``` //! //! Or implement our [`LogLevel`] trait to customize the default log level and help output.
impl<L: LogLevel> Verbosity<L> { /// Create a new verbosity instance by explicitly setting the values pubfn new(verbose: u8, quiet: u8) -> Self {
Verbosity {
verbose,
quiet,
phantom: std::marker::PhantomData,
}
}
/// Whether any verbosity flags (either `--verbose` or `--quiet`) /// are present on the command line. pubfn is_present(&self) -> bool { self.verbose != 0 || self.quiet != 0
}
/// If the user requested complete silence (i.e. not just no-logging). pubfn is_silent(&self) -> bool { self.filter() == VerbosityFilter::Off
}
/// Gets the filter that should be applied to the logger. pubfn filter(&self) -> VerbosityFilter { let offset = self.verbose as i16 - self.quiet as i16;
L::default_filter().with_offset(offset)
}
}
#[cfg(feature = "log")] impl<L: LogLevel> Verbosity<L> { /// Get the log level. /// /// `None` means all output is disabled. pubfn log_level(&self) -> Option<log::Level> { self.filter().into()
}
/// Get the log level filter. pubfn log_level_filter(&self) -> log::LevelFilter { self.filter().into()
}
}
#[cfg(feature = "tracing")] impl<L: LogLevel> Verbosity<L> { /// Get the tracing level. /// /// `None` means all output is disabled. pubfn tracing_level(&self) -> Option<tracing_core::Level> { self.filter().into()
}
/// Get the tracing level filter. pubfn tracing_level_filter(&self) -> tracing_core::LevelFilter { self.filter().into()
}
}
/// Customize the default log-level and associated help pubtrait LogLevel { /// Baseline level before applying `--verbose` and `--quiet` fn default_filter() -> VerbosityFilter;
/// A representation of the log level filter. /// /// Used to calculate the log level and filter. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pubenum VerbosityFilter {
Off,
Error,
Warn,
Info,
Debug,
Trace,
}
impl VerbosityFilter { /// Apply an offset to the filter level. /// /// Negative values will decrease the verbosity, while positive values will increase it. fn with_offset(&self, offset: i16) -> VerbosityFilter { let value = matchself { Self::Off => 0_i16, Self::Error => 1, Self::Warn => 2, Self::Info => 3, Self::Debug => 4, Self::Trace => 5,
}; match value.saturating_add(offset) {
i16::MIN..=0 => Self::Off, 1 => Self::Error, 2 => Self::Warn, 3 => Self::Info, 4 => Self::Debug, 5..=i16::MAX => Self::Trace,
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.