Skip to content

Commit

Permalink
Mimic log crate static max log level
Browse files Browse the repository at this point in the history
  • Loading branch information
dpc committed Aug 3, 2016
1 parent 95a75a2 commit 6e3d6ea
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 29 deletions.
16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ debug = false
lto = true
debug-assertions = false

[features]
max_level_off = []
max_level_error = []
max_level_warn = []
max_level_info = []
max_level_debug = []
max_level_trace = []

release_max_level_off = []
release_max_level_error = []
release_max_level_warn = []
release_max_level_info = []
release_max_level_debug = []
release_max_level_trace = []

[workspace]

[dependencies]
Expand All @@ -24,6 +39,7 @@ chrono = "0.2.22"
rustc-serialize = "0.3.19"
error-chain = "0.2.1"


[dev-dependencies]
slog-json = { path = "crates/json", version = "0.7.0" }
slog-term = { path = "crates/term", version = "0.7.0" }
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Inspired by [log15] for Go.
* Bunyan (`slog-bunyan` crate)
* syslog (`slog-syslog` crate)
* support for first class custom ones
* compile time log level using cargo features (same as in `log` crate)

### Advantages over `log` crate

Expand Down
59 changes: 48 additions & 11 deletions src/_level.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// Logging level
#[derive(Copy, Clone, Debug)]
#[repr(usize)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Level {
/// Critical
Critical,
Expand All @@ -15,6 +16,20 @@ pub enum Level {
Trace
}

#[repr(usize)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[doc(hidden)]
/// Not part of the API
pub enum LevelFilter {
Off,
Critical,
Error,
Warning,
Info,
Debug,
Trace,
}

impl Level {
/// Convert to short string
///
Expand All @@ -30,16 +45,32 @@ impl Level {
}
}

/// Cast `Level` to ordering integer, where
/// `Critical` is 0, and `Trace` is 5
pub fn as_int(&self) -> i32 {
/// Cast `Level` to ordering integer where `Critical` is the smallest and
/// `Trace` the biggest value
pub fn as_usize(&self) -> usize {
match *self {
Level::Critical => 0,
Level::Error => 1,
Level::Warning => 2,
Level::Info => 3,
Level::Debug => 4,
Level::Trace => 5,
Level::Critical => 1,
Level::Error => 2,
Level::Warning => 3,
Level::Info => 4,
Level::Debug => 5,
Level::Trace => 6,
}
}
}

impl LevelFilter {
#[doc(hidden)]
/// Not part of the API
pub fn as_usize(&self) -> usize {
match *self {
LevelFilter::Off => 0,
LevelFilter::Critical => 1,
LevelFilter::Error => 2,
LevelFilter::Warning => 3,
LevelFilter::Info => 4,
LevelFilter::Debug => 5,
LevelFilter::Trace => 6,
}
}
}
Expand All @@ -53,7 +84,7 @@ impl fmt::Display for Level {
impl Level {
/// Returns true if `self` is at least `level` logging level
pub fn is_at_least(&self, level : Self) -> bool {
self.as_int() <= level.as_int()
self.as_usize() <= level.as_usize()
}
}

Expand All @@ -64,3 +95,9 @@ fn level_at_least() {
assert!(!Level::Debug.is_at_least(Level::Info));
}

#[test]
fn levelfilter_sanity() {
assert!(Level::Critical.as_usize() > LevelFilter::Off.as_usize());
assert!(Level::Critical.as_usize() <= LevelFilter::Critical.as_usize());
assert!(Level::Trace.as_usize() <= LevelFilter::Trace.as_usize());
}
79 changes: 61 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ macro_rules! o(
///
/// Use wrappers `error!`, `warn!` etc. instead
///
/// The `max_level_*` features can be used to statically disable logging at
/// various levels.
///
/// ```
/// #[macro_use]
/// extern crate slog;
Expand All @@ -55,28 +58,34 @@ macro_rules! o(
#[macro_export]
macro_rules! log(
($l:expr, $lvl:expr, $msg:expr) => {
$l.log(
&$crate::RecordInfo::new(
$lvl,
&$msg,
file!(),
line!(),
module_path!(),
&[]
let lvl = $lvl;
if lvl.as_usize() <= $crate::__slog_static_max_level().as_usize() {
$l.log(
&$crate::RecordInfo::new(
lvl,
&$msg,
file!(),
line!(),
module_path!(),
&[]
)
)
)
}
};
($l:expr, $lvl:expr, $msg:expr, $($k:expr => $v:expr),*) => {
$l.log(
&$crate::RecordInfo::new(
$lvl,
&$msg,
file!(),
line!(),
module_path!(),
&[$(($k, &$v)),*]
let lvl = $lvl;
if lvl.as_usize() <= $crate::__slog_static_max_level().as_usize() {
$l.log(
&$crate::RecordInfo::new(
$lvl,
&$msg,
file!(),
line!(),
module_path!(),
&[$(($k, &$v)),*]
)
)
)
}
};
);

Expand Down Expand Up @@ -351,5 +360,39 @@ impl<'a> Iterator for OwnedKeyValueNodeIterator<'a> {
}
}

#[inline(always)]
#[doc(hidden)]
/// Not an API
pub fn __slog_static_max_level() -> LevelFilter {
if !cfg!(debug_assertions) {
if cfg!(feature = "release_max_level_off") {
return LevelFilter::Off
} else if cfg!(feature = "release_max_level_error") {
return LevelFilter::Error
} else if cfg!(feature = "release_max_level_warn") {
return LevelFilter::Warning
} else if cfg!(feature = "release_max_level_info") {
return LevelFilter::Info
} else if cfg!(feature = "release_max_level_debug") {
return LevelFilter::Debug
} else if cfg!(feature = "release_max_level_trace") {
return LevelFilter::Trace
}
}
if cfg!(feature = "max_level_off") {
LevelFilter::Off
} else if cfg!(feature = "max_level_error") {
LevelFilter::Error
} else if cfg!(feature = "max_level_warn") {
LevelFilter::Warning
} else if cfg!(feature = "max_level_info") {
LevelFilter::Info
} else if cfg!(feature = "max_level_debug") {
LevelFilter::Debug
} else {
LevelFilter::Trace
}
}

#[cfg(test)]
mod tests;

0 comments on commit 6e3d6ea

Please sign in to comment.