From: Matt Corallo Date: Mon, 28 Jun 2021 20:38:48 +0000 (+0000) Subject: Allow logging to specify an explicit log level instead of a macro X-Git-Tag: v0.0.99~12^2~6 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=7fa6a7d48e5cec139481ceee95baadb2cf06486e;p=rust-lightning Allow logging to specify an explicit log level instead of a macro For log entries which may have a variable level, we can't call an arbitrary macro and need to be able to pass an explicit level. This does so without breaking the compile-time disabling of certain log levels. Further, we "fix" the comparison order of log levels to make more significant levels sort "higher", which implicitly makes more sense than sorting "lower". Finally, we remove the "Off" log level as no log entry should ever be logged at the "Off" level - that would be nonsensical. --- diff --git a/lightning/src/util/logger.rs b/lightning/src/util/logger.rs index df2b7f704..98037aac2 100644 --- a/lightning/src/util/logger.rs +++ b/lightning/src/util/logger.rs @@ -17,23 +17,21 @@ use core::cmp; use core::fmt; -static LOG_LEVEL_NAMES: [&'static str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"]; +static LOG_LEVEL_NAMES: [&'static str; 5] = ["TRACE", "DEBUG", "INFO", "WARN", "ERROR"]; /// An enum representing the available verbosity levels of the logger. #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum Level { - ///Designates logger being silent - Off, - /// Designates very serious errors - Error, - /// Designates hazardous situations - Warn, - /// Designates useful information - Info, - /// Designates lower priority information - Debug, /// Designates very low priority, often extremely verbose, information Trace, + /// Designates lower priority information + Debug, + /// Designates useful information + Info, + /// Designates hazardous situations + Warn, + /// Designates very serious errors + Error, } impl PartialOrd for Level { @@ -163,4 +161,35 @@ mod tests { let wrapper = WrapperLog::new(Arc::clone(&logger)); wrapper.call_macros(); } + + #[test] + fn test_log_ordering() { + assert!(Level::Error > Level::Warn); + assert!(Level::Error >= Level::Warn); + assert!(Level::Error >= Level::Error); + assert!(Level::Warn > Level::Info); + assert!(Level::Warn >= Level::Info); + assert!(Level::Warn >= Level::Warn); + assert!(Level::Info > Level::Debug); + assert!(Level::Info >= Level::Debug); + assert!(Level::Info >= Level::Info); + assert!(Level::Debug > Level::Trace); + assert!(Level::Debug >= Level::Trace); + assert!(Level::Debug >= Level::Debug); + assert!(Level::Trace >= Level::Trace); + + assert!(Level::Error <= Level::Error); + assert!(Level::Warn < Level::Error); + assert!(Level::Warn <= Level::Error); + assert!(Level::Warn <= Level::Warn); + assert!(Level::Info < Level::Warn); + assert!(Level::Info <= Level::Warn); + assert!(Level::Info <= Level::Info); + assert!(Level::Debug < Level::Info); + assert!(Level::Debug <= Level::Info); + assert!(Level::Debug <= Level::Debug); + assert!(Level::Trace < Level::Debug); + assert!(Level::Trace <= Level::Debug); + assert!(Level::Trace <= Level::Trace); + } } diff --git a/lightning/src/util/macro_logger.rs b/lightning/src/util/macro_logger.rs index c4630c638..3ac294fbf 100644 --- a/lightning/src/util/macro_logger.rs +++ b/lightning/src/util/macro_logger.rs @@ -164,33 +164,53 @@ macro_rules! log_internal { ); } +/// Logs an entry at the given level. +#[macro_export] +macro_rules! log_given_level { + ($logger: expr, $lvl:expr, $($arg:tt)+) => ( + match $lvl { + #[cfg(not(any(feature = "max_level_off")))] + $crate::util::logger::Level::Error => log_internal!($logger, $lvl, $($arg)*), + #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))] + $crate::util::logger::Level::Warn => log_internal!($logger, $lvl, $($arg)*), + #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))] + $crate::util::logger::Level::Info => log_internal!($logger, $lvl, $($arg)*), + #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))] + $crate::util::logger::Level::Debug => log_internal!($logger, $lvl, $($arg)*), + #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))] + $crate::util::logger::Level::Trace => log_internal!($logger, $lvl, $($arg)*), + + #[cfg(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug"))] + _ => { + // The level is disabled at compile-time + }, + } + ); +} + /// Log an error. #[macro_export] macro_rules! log_error { ($logger: expr, $($arg:tt)*) => ( - #[cfg(not(any(feature = "max_level_off")))] - log_internal!($logger, $crate::util::logger::Level::Error, $($arg)*); + log_given_level!($logger, $crate::util::logger::Level::Error, $($arg)*); ) } macro_rules! log_warn { ($logger: expr, $($arg:tt)*) => ( - #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))] - log_internal!($logger, $crate::util::logger::Level::Warn, $($arg)*); + log_given_level!($logger, $crate::util::logger::Level::Warn, $($arg)*); ) } macro_rules! log_info { ($logger: expr, $($arg:tt)*) => ( - #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))] - log_internal!($logger, $crate::util::logger::Level::Info, $($arg)*); + log_given_level!($logger, $crate::util::logger::Level::Info, $($arg)*); ) } macro_rules! log_debug { ($logger: expr, $($arg:tt)*) => ( - #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))] - log_internal!($logger, $crate::util::logger::Level::Debug, $($arg)*); + log_given_level!($logger, $crate::util::logger::Level::Debug, $($arg)*); ) } @@ -198,7 +218,6 @@ macro_rules! log_debug { #[macro_export] macro_rules! log_trace { ($logger: expr, $($arg:tt)*) => ( - #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))] - log_internal!($logger, $crate::util::logger::Level::Trace, $($arg)*); + log_given_level!($logger, $crate::util::logger::Level::Trace, $($arg)*); ) }