From 1b1eb917598d8b1817f6aae2abccbd2d3be63439 Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Wed, 13 Nov 2024 23:34:29 -0800 Subject: [PATCH] feat: Implement Off, Debug, and Trace levels Fixes: #122 --- src/log.rs | 109 +++++++++++++++++++++++++++++++++++++++++++++++-- src/tracing.rs | 105 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 206 insertions(+), 8 deletions(-) diff --git a/src/log.rs b/src/log.rs index 519f418..f603792 100644 --- a/src/log.rs +++ b/src/log.rs @@ -70,7 +70,7 @@ macro_rules! log_levels { type Level = Level; type LevelFilter = LevelFilter; fn default() -> Option { - Some($level) + $level } } )* @@ -78,9 +78,12 @@ macro_rules! log_levels { } log_levels! { - ErrorLevel => Level::Error, - WarnLevel => Level::Warn, - InfoLevel => Level::Info, + OffLevel => None, + ErrorLevel => Some(Level::Error), + WarnLevel => Some(Level::Warn), + InfoLevel => Some(Level::Info), + DebugLevel => Some(Level::Debug), + TraceLevel => Some(Level::Trace), } #[cfg(test)] @@ -89,6 +92,39 @@ mod tests { use super::*; + #[test] + fn verbosity_off_level() { + let tests = [ + // verbose, quiet, expected_level, expected_filter + (0, 0, None, LevelFilter::Off), + (1, 0, Some(Level::Error), LevelFilter::Error), + (2, 0, Some(Level::Warn), LevelFilter::Warn), + (3, 0, Some(Level::Info), LevelFilter::Info), + (4, 0, Some(Level::Debug), LevelFilter::Debug), + (5, 0, Some(Level::Trace), LevelFilter::Trace), + (6, 0, Some(Level::Trace), LevelFilter::Trace), + (255, 0, Some(Level::Trace), LevelFilter::Trace), + (0, 1, None, LevelFilter::Off), + (0, 2, None, LevelFilter::Off), + (0, 255, None, LevelFilter::Off), + (255, 255, None, LevelFilter::Off), + ]; + + for (verbose, quiet, expected_level, expected_filter) in tests.iter() { + let v = Verbosity::::new(*verbose, *quiet); + assert_eq!( + v.log_level(), + *expected_level, + "verbose = {verbose}, quiet = {quiet}" + ); + assert_eq!( + v.log_level_filter(), + *expected_filter, + "verbose = {verbose}, quiet = {quiet}" + ); + } + } + #[test] fn verbosity_error_level() { let tests = [ @@ -184,4 +220,69 @@ mod tests { ); } } + + #[test] + fn verbosity_debug_level() { + let tests = [ + // verbose, quiet, expected_level, expected_filter + (0, 0, Some(Level::Debug), LevelFilter::Debug), + (1, 0, Some(Level::Trace), LevelFilter::Trace), + (2, 0, Some(Level::Trace), LevelFilter::Trace), + (3, 0, Some(Level::Trace), LevelFilter::Trace), + (255, 0, Some(Level::Trace), LevelFilter::Trace), + (0, 1, Some(Level::Info), LevelFilter::Info), + (0, 2, Some(Level::Warn), LevelFilter::Warn), + (0, 3, Some(Level::Error), LevelFilter::Error), + (0, 4, None, LevelFilter::Off), + (0, 255, None, LevelFilter::Off), + (255, 255, Some(Level::Debug), LevelFilter::Debug), + ]; + + for (verbose, quiet, expected_level, expected_filter) in tests.iter() { + let v = Verbosity::::new(*verbose, *quiet); + assert_eq!( + v.log_level(), + *expected_level, + "verbose = {verbose}, quiet = {quiet}" + ); + assert_eq!( + v.log_level_filter(), + *expected_filter, + "verbose = {verbose}, quiet = {quiet}" + ); + } + } + + #[test] + fn verbosity_trace_level() { + let tests = [ + // verbose, quiet, expected_level, expected_filter + (0, 0, Some(Level::Trace), LevelFilter::Trace), + (1, 0, Some(Level::Trace), LevelFilter::Trace), + (2, 0, Some(Level::Trace), LevelFilter::Trace), + (3, 0, Some(Level::Trace), LevelFilter::Trace), + (255, 0, Some(Level::Trace), LevelFilter::Trace), + (0, 1, Some(Level::Debug), LevelFilter::Debug), + (0, 2, Some(Level::Info), LevelFilter::Info), + (0, 3, Some(Level::Warn), LevelFilter::Warn), + (0, 4, Some(Level::Error), LevelFilter::Error), + (0, 5, None, LevelFilter::Off), + (0, 255, None, LevelFilter::Off), + (255, 255, Some(Level::Trace), LevelFilter::Trace), + ]; + + for (verbose, quiet, expected_level, expected_filter) in tests.iter() { + let v = Verbosity::::new(*verbose, *quiet); + assert_eq!( + v.log_level(), + *expected_level, + "verbose = {verbose}, quiet = {quiet}" + ); + assert_eq!( + v.log_level_filter(), + *expected_filter, + "verbose = {verbose}, quiet = {quiet}" + ); + } + } } diff --git a/src/tracing.rs b/src/tracing.rs index 0fd31c7..5bca3e1 100644 --- a/src/tracing.rs +++ b/src/tracing.rs @@ -69,7 +69,7 @@ macro_rules! log_levels { type Level = Level; type LevelFilter = LevelFilter; fn default() -> Option { - Some($level) + $level } } )* @@ -77,9 +77,12 @@ macro_rules! log_levels { } log_levels! { - ErrorLevel => Level::ERROR, - WarnLevel => Level::WARN, - InfoLevel => Level::INFO, + OffLevel => None, + ErrorLevel => Some(Level::ERROR), + WarnLevel => Some(Level::WARN), + InfoLevel => Some(Level::INFO), + DebugLevel => Some(Level::DEBUG), + TraceLevel => Some(Level::TRACE), } #[cfg(test)] @@ -88,6 +91,38 @@ mod tests { use super::*; + #[test] + fn verbosity_off_level() { + let tests = [ + // verbose, quiet, expected_level, expected_filter + (0, 0, None, LevelFilter::OFF), + (1, 0, Some(Level::ERROR), LevelFilter::ERROR), + (2, 0, Some(Level::WARN), LevelFilter::WARN), + (3, 0, Some(Level::INFO), LevelFilter::INFO), + (4, 0, Some(Level::DEBUG), LevelFilter::DEBUG), + (5, 0, Some(Level::TRACE), LevelFilter::TRACE), + (255, 0, Some(Level::TRACE), LevelFilter::TRACE), + (0, 1, None, LevelFilter::OFF), + (0, 2, None, LevelFilter::OFF), + (0, 255, None, LevelFilter::OFF), + (255, 255, None, LevelFilter::OFF), + ]; + + for (verbose, quiet, expected_level, expected_filter) in tests.iter() { + let v = Verbosity::::new(*verbose, *quiet); + assert_eq!( + v.log_level(), + *expected_level, + "verbose = {verbose}, quiet = {quiet}" + ); + assert_eq!( + v.log_level_filter(), + *expected_filter, + "verbose = {verbose}, quiet = {quiet}" + ); + } + } + #[test] fn verbosity_error_level() { let tests = [ @@ -183,4 +218,66 @@ mod tests { ); } } + + #[test] + fn verbosity_debug_level() { + let tests = [ + // verbose, quiet, expected_level, expected_filter + (0, 0, Some(Level::DEBUG), LevelFilter::DEBUG), + (1, 0, Some(Level::TRACE), LevelFilter::TRACE), + (2, 0, Some(Level::TRACE), LevelFilter::TRACE), + (255, 0, Some(Level::TRACE), LevelFilter::TRACE), + (0, 1, Some(Level::INFO), LevelFilter::INFO), + (0, 2, Some(Level::WARN), LevelFilter::WARN), + (0, 3, Some(Level::ERROR), LevelFilter::ERROR), + (0, 4, None, LevelFilter::OFF), + (0, 255, None, LevelFilter::OFF), + (255, 255, Some(Level::DEBUG), LevelFilter::DEBUG), + ]; + + for (verbose, quiet, expected_level, expected_filter) in tests.iter() { + let v = Verbosity::::new(*verbose, *quiet); + assert_eq!( + v.log_level(), + *expected_level, + "verbose = {verbose}, quiet = {quiet}" + ); + assert_eq!( + v.log_level_filter(), + *expected_filter, + "verbose = {verbose}, quiet = {quiet}" + ); + } + } + + #[test] + fn verbosity_trace_level() { + let tests = [ + // verbose, quiet, expected_level, expected_filter + (0, 0, Some(Level::TRACE), LevelFilter::TRACE), + (1, 0, Some(Level::TRACE), LevelFilter::TRACE), + (255, 0, Some(Level::TRACE), LevelFilter::TRACE), + (0, 1, Some(Level::DEBUG), LevelFilter::DEBUG), + (0, 2, Some(Level::INFO), LevelFilter::INFO), + (0, 3, Some(Level::WARN), LevelFilter::WARN), + (0, 4, Some(Level::ERROR), LevelFilter::ERROR), + (0, 5, None, LevelFilter::OFF), + (0, 255, None, LevelFilter::OFF), + (255, 255, Some(Level::TRACE), LevelFilter::TRACE), + ]; + + for (verbose, quiet, expected_level, expected_filter) in tests.iter() { + let v = Verbosity::::new(*verbose, *quiet); + assert_eq!( + v.log_level(), + *expected_level, + "verbose = {verbose}, quiet = {quiet}" + ); + assert_eq!( + v.log_level_filter(), + *expected_filter, + "verbose = {verbose}, quiet = {quiet}" + ); + } + } }