|
| 1 | +//! This module offers a convenient way to enable only a subset of logging levels |
| 2 | +//! for just this `fatfs` crate only without changing the logging levels |
| 3 | +//! of other crates in a given project. |
| 4 | +//! |
| 5 | +//! This only applies to the five level-specific logging macros: |
| 6 | +//! `error`, `warn`, `info`, `debug`, and `trace`. |
| 7 | +//! The core `log!()` macro is unaffected. |
| 8 | +
|
| 9 | +use log::LevelFilter; |
| 10 | + |
| 11 | +#[cfg(feature = "log_level_trace")] |
| 12 | +pub const MAX_LOG_LEVEL: LevelFilter = LevelFilter::Trace; |
| 13 | + |
| 14 | +#[cfg(all( |
| 15 | + not(feature = "log_level_trace"), |
| 16 | + feature = "log_level_debug", |
| 17 | +))] |
| 18 | +pub const MAX_LOG_LEVEL: LevelFilter = LevelFilter::Debug; |
| 19 | + |
| 20 | +#[cfg(all( |
| 21 | + not(feature = "log_level_trace"), |
| 22 | + not(feature = "log_level_debug"), |
| 23 | + feature = "log_level_info", |
| 24 | +))] |
| 25 | +pub const MAX_LOG_LEVEL: LevelFilter = LevelFilter::Info; |
| 26 | + |
| 27 | +#[cfg(all( |
| 28 | + not(feature = "log_level_trace"), |
| 29 | + not(feature = "log_level_debug"), |
| 30 | + not(feature = "log_level_info"), |
| 31 | + feature = "log_level_warn", |
| 32 | +))] |
| 33 | +pub const MAX_LOG_LEVEL: LevelFilter = LevelFilter::Warn; |
| 34 | + |
| 35 | +#[cfg(all( |
| 36 | + not(feature = "log_level_trace"), |
| 37 | + not(feature = "log_level_debug"), |
| 38 | + not(feature = "log_level_info"), |
| 39 | + not(feature = "log_level_warn"), |
| 40 | + feature = "log_level_error", |
| 41 | +))] |
| 42 | +pub const MAX_LOG_LEVEL: LevelFilter = LevelFilter::Error; |
| 43 | + |
| 44 | +#[cfg(all( |
| 45 | + not(feature = "log_level_trace"), |
| 46 | + not(feature = "log_level_debug"), |
| 47 | + not(feature = "log_level_info"), |
| 48 | + not(feature = "log_level_warn"), |
| 49 | + not(feature = "log_level_error"), |
| 50 | +))] |
| 51 | +pub const MAX_LOG_LEVEL: LevelFilter = LevelFilter::Off; |
| 52 | + |
| 53 | + |
| 54 | +#[macro_export] |
| 55 | +macro_rules! log { |
| 56 | + (target: $target:expr, $lvl:expr, $($arg:tt)+) => ({ |
| 57 | + let lvl = $lvl; |
| 58 | + if lvl <= $crate::log_macros::MAX_LOG_LEVEL { |
| 59 | + log::log!(target: $target, lvl, $($arg)+); |
| 60 | + } |
| 61 | + }); |
| 62 | + ($lvl:expr, $($arg:tt)+) => (log!(target: log::__log_module_path!(), $lvl, $($arg)+)) |
| 63 | +} |
| 64 | + |
| 65 | +#[macro_export] |
| 66 | +macro_rules! error { |
| 67 | + (target: $target:expr, $($arg:tt)+) => ( |
| 68 | + log!(target: $target, log::Level::Error, $($arg)+); |
| 69 | + ); |
| 70 | + ($($arg:tt)+) => ( |
| 71 | + log!(log::Level::Error, $($arg)+); |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +#[macro_export] |
| 76 | +macro_rules! warn { |
| 77 | + (target: $target:expr, $($arg:tt)+) => ( |
| 78 | + log!(target: $target, log::Level::Warn, $($arg)+); |
| 79 | + ); |
| 80 | + ($($arg:tt)+) => ( |
| 81 | + log!(log::Level::Warn, $($arg)+); |
| 82 | + ) |
| 83 | +} |
| 84 | + |
| 85 | +#[macro_export] |
| 86 | +macro_rules! info { |
| 87 | + (target: $target:expr, $($arg:tt)+) => ( |
| 88 | + log!(target: $target, log::Level::Info, $($arg)+); |
| 89 | + ); |
| 90 | + ($($arg:tt)+) => ( |
| 91 | + log!(log::Level::Info, $($arg)+); |
| 92 | + ) |
| 93 | +} |
| 94 | + |
| 95 | +#[macro_export] |
| 96 | +macro_rules! debug { |
| 97 | + (target: $target:expr, $($arg:tt)+) => ( |
| 98 | + log!(target: $target, log::Level::Debug, $($arg)+); |
| 99 | + ); |
| 100 | + ($($arg:tt)+) => ( |
| 101 | + log!(log::Level::Debug, $($arg)+); |
| 102 | + ) |
| 103 | +} |
| 104 | + |
| 105 | +#[macro_export] |
| 106 | +macro_rules! trace { |
| 107 | + (target: $target:expr, $($arg:tt)+) => ( |
| 108 | + log!(target: $target, log::Level::Trace, $($arg)+); |
| 109 | + ); |
| 110 | + ($($arg:tt)+) => ( |
| 111 | + log!(log::Level::Trace, $($arg)+); |
| 112 | + ) |
| 113 | +} |
0 commit comments