Skip to content

Commit 1318135

Browse files
kevinaboosrafalh
authored andcommitted
Leverage cargo features to selectively enable logging levels
1 parent 8831657 commit 1318135

File tree

3 files changed

+128
-2
lines changed

3 files changed

+128
-2
lines changed

Cargo.toml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,19 @@ lfn = []
2424
alloc = []
2525
# Full Unicode support. Disabling it reduces code size by avoiding Unicode-aware character case conversion
2626
unicode = []
27+
# Enable only error-level logging
28+
log_level_error = []
29+
# Enable logging levels warn and up
30+
log_level_warn = ["log_level_error"]
31+
# Enable logging levels info and up
32+
log_level_info = ["log_level_warn"]
33+
# Enable logging levels debug and up
34+
log_level_debug = ["log_level_info"]
35+
# Enable all logging levels: trace and up
36+
log_level_trace = ["log_level_debug"]
37+
2738
# Default features
28-
default = ["chrono", "std", "alloc", "lfn", "unicode"]
39+
default = ["chrono", "std", "alloc", "lfn", "unicode", "log_level_trace"]
2940

3041
[dependencies]
3142
bitflags = "1.0"

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@
4949
#![warn(clippy::pedantic)]
5050
#![allow(clippy::module_name_repetitions, clippy::cast_possible_truncation)]
5151

52-
#[macro_use]
5352
extern crate log;
5453

5554
#[cfg(all(not(feature = "std"), feature = "alloc"))]
5655
extern crate alloc;
5756

57+
#[macro_use]
58+
mod log_macros;
59+
5860
mod boot_sector;
5961
mod dir;
6062
mod dir_entry;

src/log_macros.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)