Allow logging to specify an explicit log level instead of a macro
authorMatt Corallo <git@bluematt.me>
Mon, 28 Jun 2021 20:38:48 +0000 (20:38 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 29 Jun 2021 19:36:47 +0000 (19:36 +0000)
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.

lightning/src/util/logger.rs
lightning/src/util/macro_logger.rs

index df2b7f704051958a1e8ddc63523108061c8cfb08..98037aac298280a5b418b9c84bb94b948ae3bc30 100644 (file)
 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);
+       }
 }
index c4630c638c7568aa5a2d9bbfc6835985a4260866..3ac294fbff9097686d4d28df669bcab76c402f5e 100644 (file)
@@ -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)*);
        )
 }