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 all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------
Expand Down
13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down Expand Up @@ -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.

Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@
#![warn(clippy::pedantic)]
#![allow(clippy::module_name_repetitions, clippy::cast_possible_truncation)]

#[macro_use]
extern crate log;

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