Skip to content

Commit

Permalink
feat: Implement Off, Debug, and Trace levels
Browse files Browse the repository at this point in the history
Fixes: #122
  • Loading branch information
joshka committed Nov 14, 2024
1 parent db29ee7 commit 1b1eb91
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 8 deletions.
109 changes: 105 additions & 4 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,20 @@ macro_rules! log_levels {
type Level = Level;
type LevelFilter = LevelFilter;
fn default() -> Option<Level> {
Some($level)
$level
}
}
)*
}
}

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)]
Expand All @@ -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::<OffLevel>::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 = [
Expand Down Expand Up @@ -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::<DebugLevel>::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::<TraceLevel>::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}"
);
}
}
}
105 changes: 101 additions & 4 deletions src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,20 @@ macro_rules! log_levels {
type Level = Level;
type LevelFilter = LevelFilter;
fn default() -> Option<Level> {
Some($level)
$level
}
}
)*
};
}

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)]
Expand All @@ -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::<OffLevel>::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 = [
Expand Down Expand Up @@ -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::<DebugLevel>::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::<TraceLevel>::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}"
);
}
}
}

0 comments on commit 1b1eb91

Please sign in to comment.