Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leverage cargo features to selectively enable logging levels #44

Merged
merged 4 commits into from
Oct 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@
#[macro_use]
extern crate bitflags;

#[macro_use]
extern crate log;

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;
Expand Down
109 changes: 109 additions & 0 deletions src/log_macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//! 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.

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)+);
)
}