From 734ba7ba18b8e71b496a6338dd952455ebaee019 Mon Sep 17 00:00:00 2001 From: Kevin Boos Date: Tue, 13 Jul 2021 16:49:47 -0700 Subject: [PATCH 1/3] Leverage cargo features to selectively enable logging levels --- Cargo.toml | 13 +++++- src/lib.rs | 4 +- src/log_macros.rs | 113 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 src/log_macros.rs diff --git a/Cargo.toml b/Cargo.toml index dd199b8..4141d76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,8 +27,19 @@ lfn = [] alloc = [] # Full Unicode support. Disabling it reduces code size by avoiding Unicode-aware character case conversion unicode = [] +# Enable only error-level logging +log_level_error = [] +# Enable logging levels warn and up +log_level_warn = ["log_level_error"] +# Enable logging levels info and up +log_level_info = ["log_level_warn"] +# Enable logging levels debug and up +log_level_debug = ["log_level_info"] +# Enable all logging levels: trace and up +log_level_trace = ["log_level_debug"] + # Default features -default = ["chrono", "std", "alloc", "lfn", "unicode"] +default = ["chrono", "std", "alloc", "lfn", "unicode", "log_level_trace"] [dependencies] bitflags = "1.0" diff --git a/src/lib.rs b/src/lib.rs index 991ca8e..363e6b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -67,7 +67,6 @@ #[macro_use] extern crate bitflags; -#[macro_use] extern crate log; extern crate core; @@ -75,6 +74,9 @@ extern crate core; #[cfg(all(not(feature = "std"), feature = "alloc"))] extern crate alloc; +#[macro_use] +mod log_macros; + mod boot_sector; mod dir; mod dir_entry; diff --git a/src/log_macros.rs b/src/log_macros.rs new file mode 100644 index 0000000..177a06a --- /dev/null +++ b/src/log_macros.rs @@ -0,0 +1,113 @@ +//! This module offers a convenient way to enable only a subset of logging levels +//! for just this `fatfs` crate only without changing the logging levels +//! of other crates in a given project. +//! +//! This only applies to the five level-specific logging macros: +//! `error`, `warn`, `info`, `debug`, and `trace`. +//! The core `log!()` macro is unaffected. + +use log::LevelFilter; + +#[cfg(feature = "log_level_trace")] +pub const MAX_LOG_LEVEL: LevelFilter = LevelFilter::Trace; + +#[cfg(all( + not(feature = "log_level_trace"), + feature = "log_level_debug", +))] +pub const MAX_LOG_LEVEL: LevelFilter = LevelFilter::Debug; + +#[cfg(all( + not(feature = "log_level_trace"), + not(feature = "log_level_debug"), + feature = "log_level_info", +))] +pub const MAX_LOG_LEVEL: LevelFilter = LevelFilter::Info; + +#[cfg(all( + not(feature = "log_level_trace"), + not(feature = "log_level_debug"), + not(feature = "log_level_info"), + feature = "log_level_warn", +))] +pub const MAX_LOG_LEVEL: LevelFilter = LevelFilter::Warn; + +#[cfg(all( + not(feature = "log_level_trace"), + not(feature = "log_level_debug"), + not(feature = "log_level_info"), + not(feature = "log_level_warn"), + feature = "log_level_error", +))] +pub const MAX_LOG_LEVEL: LevelFilter = LevelFilter::Error; + +#[cfg(all( + not(feature = "log_level_trace"), + not(feature = "log_level_debug"), + not(feature = "log_level_info"), + not(feature = "log_level_warn"), + not(feature = "log_level_error"), +))] +pub const MAX_LOG_LEVEL: LevelFilter = LevelFilter::Off; + + +#[macro_export] +macro_rules! log { + (target: $target:expr, $lvl:expr, $($arg:tt)+) => ({ + let lvl = $lvl; + if lvl <= $crate::log_macros::MAX_LOG_LEVEL { + log::log!(target: $target, lvl, $($arg)+); + } + }); + ($lvl:expr, $($arg:tt)+) => (log!(target: log::__log_module_path!(), $lvl, $($arg)+)) +} + +#[macro_export] +macro_rules! error { + (target: $target:expr, $($arg:tt)+) => ( + log!(target: $target, log::Level::Error, $($arg)+); + ); + ($($arg:tt)+) => ( + log!(log::Level::Error, $($arg)+); + ) +} + +#[macro_export] +macro_rules! warn { + (target: $target:expr, $($arg:tt)+) => ( + log!(target: $target, log::Level::Warn, $($arg)+); + ); + ($($arg:tt)+) => ( + log!(log::Level::Warn, $($arg)+); + ) +} + +#[macro_export] +macro_rules! info { + (target: $target:expr, $($arg:tt)+) => ( + log!(target: $target, log::Level::Info, $($arg)+); + ); + ($($arg:tt)+) => ( + log!(log::Level::Info, $($arg)+); + ) +} + +#[macro_export] +macro_rules! debug { + (target: $target:expr, $($arg:tt)+) => ( + log!(target: $target, log::Level::Debug, $($arg)+); + ); + ($($arg:tt)+) => ( + log!(log::Level::Debug, $($arg)+); + ) +} + +#[macro_export] +macro_rules! trace { + (target: $target:expr, $($arg:tt)+) => ( + log!(target: $target, log::Level::Trace, $($arg)+); + ); + ($($arg:tt)+) => ( + log!(log::Level::Trace, $($arg)+); + ) +} From cf9fa6a5315b78c0316d38c6fc60c915263e2281 Mon Sep 17 00:00:00 2001 From: Kevin Boos Date: Tue, 13 Jul 2021 17:04:38 -0700 Subject: [PATCH 2/3] remove outdated comment --- src/log_macros.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/log_macros.rs b/src/log_macros.rs index 177a06a..c5bd15e 100644 --- a/src/log_macros.rs +++ b/src/log_macros.rs @@ -1,10 +1,6 @@ //! This module offers a convenient way to enable only a subset of logging levels //! for just this `fatfs` crate only without changing the logging levels //! of other crates in a given project. -//! -//! This only applies to the five level-specific logging macros: -//! `error`, `warn`, `info`, `debug`, and `trace`. -//! The core `log!()` macro is unaffected. use log::LevelFilter; From b4bedf21ec83f1e4331d4e5d982e3526840709f3 Mon Sep 17 00:00:00 2001 From: Kevin Boos Date: Wed, 20 Oct 2021 16:50:04 -0500 Subject: [PATCH 3/3] Add discussion of configurable logging level features to changelog and readme --- CHANGELOG.md | 2 ++ README.md | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d5b2d5..9352e44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,8 @@ New features: * Remove `byteorder` dependency. * Bump up minimal Rust compiler version to 1.46.0. * Build the crate using the 2018 edition. +* Add support for compile-time configuration of logging levels via Cargo features. By default, all logging levels are + enabled, including "trace" and up. 0.3.4 (2020-07-20) ------------------ diff --git a/README.md b/README.md index 5f0613a..a5a440e 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ Features: * FAT12, FAT16, FAT32 compatibility * LFN (Long File Names) extension is supported * Basic no_std environment support +* logging configurable at compile time using cargo features Usage ----- @@ -59,6 +60,13 @@ Additional features: a memory allocator implementation. * `unicode` - use Unicode-compatible case conversion in file names - you may want to have it disabled for lower memory footprint +* `log_level_*` - enable specific logging levels at compile time. +The options are as follows: + * `log_level_error` - enable only error-level logging. + * `log_level_warn` - enable warn-level logging and higher. + * `log_level_info` - enable info-level logging and higher. + * `log_level_debug` - enable debug-level logging and higher. + * `log_level_trace` - (default) enable all logging levels, trace and higher. Note: above features are enabled by default and were designed primarily for `no_std` usage.