Skip to content
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
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ language: rust
sudo: false
rust:
- 1.0.0
- stable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you leave 1.0.0 here? The intention with that is to ensure that this continues to always compile on 1.0.0 Rust

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust 1.0.0 does not support Cargo features. :(

EDIT: Actually, it did... but for some reason Cargo from Rust 1.0.0 can't parse the Cargo.toml files in this PR. https://travis-ci.org/rust-lang-nursery/log/builds/83363467

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? Features have been around since Cargo's inception basically...

- beta
- nightly
script:
- cargo build --verbose
- cargo test --verbose
- cargo test --verbose --manifest-path env/Cargo.toml
- cargo run --verbose --manifest-path tests/max_level_features/Cargo.toml
- cargo run --verbose --manifest-path tests/max_level_features/Cargo.toml --release
- cargo doc --manifest-path env/Cargo.toml
after_success: |
[ $TRAVIS_BRANCH = master ] &&
Expand Down
15 changes: 15 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,18 @@ harness = false

[dependencies]
libc = "0.1"

[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 = []
34 changes: 34 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,40 @@ pub fn max_log_level() -> LogLevelFilter {
unsafe { mem::transmute(MAX_LOG_LEVEL_FILTER.load(Ordering::Relaxed)) }
}

#[inline(always)]
#[doc(hidden)]
pub fn __static_max_level() -> LogLevelFilter {
if !cfg!(debug_assertions) {
// This is a release build. Check `release_max_level_*` first.
if cfg!(feature = "release_max_level_off") {
return LogLevelFilter::Off
} else if cfg!(feature = "release_max_level_error") {
return LogLevelFilter::Error
} else if cfg!(feature = "release_max_level_warn") {
return LogLevelFilter::Warn
} else if cfg!(feature = "release_max_level_info") {
return LogLevelFilter::Info
} else if cfg!(feature = "release_max_level_debug") {
return LogLevelFilter::Debug
} else if cfg!(feature = "release_max_level_trace") {
return LogLevelFilter::Trace
}
}
if cfg!(feature = "max_level_off") {
LogLevelFilter::Off
} else if cfg!(feature = "max_level_error") {
LogLevelFilter::Error
} else if cfg!(feature = "max_level_warn") {
LogLevelFilter::Warn
} else if cfg!(feature = "max_level_info") {
LogLevelFilter::Info
} else if cfg!(feature = "max_level_debug") {
LogLevelFilter::Debug
} else {
LogLevelFilter::Trace
}
}

/// Sets the global logger.
///
/// The `make_logger` closure is passed a `MaxLogLevel` object, which the
Expand Down
62 changes: 35 additions & 27 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
/// This macro will generically log with the specified `LogLevel` and `format!`
/// based argument list.
///
/// The `log_level` cfg can be used to statically disable logging at various
/// levels.
/// The `max_level_*` features can be used to statically disable logging at
/// various levels.
#[macro_export]
macro_rules! log {
(target: $target:expr, $lvl:expr, $($arg:tt)+) => ({
Expand All @@ -23,12 +23,7 @@ macro_rules! log {
__module_path: module_path!(),
};
let lvl = $lvl;
if !cfg!(log_level = "off") &&
(lvl <= $crate::LogLevel::Error || !cfg!(log_level = "error")) &&
(lvl <= $crate::LogLevel::Warn || !cfg!(log_level = "warn")) &&
(lvl <= $crate::LogLevel::Debug || !cfg!(log_level = "debug")) &&
(lvl <= $crate::LogLevel::Info || !cfg!(log_level = "info")) &&
lvl <= $crate::max_log_level() {
if lvl <= $crate::__static_max_level() && lvl <= $crate::max_log_level() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we worry about breaking back-compat for people that are actually using the log_level cfg? I don't know if any of those people exist, but might be worth leaving this around to support them? Thoughts @alexcrichton?

$crate::__log(lvl, $target, &_LOC, format_args!($($arg)+))
}
});
Expand All @@ -37,8 +32,7 @@ macro_rules! log {

/// Logs a message at the error level.
///
/// Logging at this level is disabled if the `log_level = "off"` cfg is
/// present.
/// Logging at this level is disabled if the `max_level_off` feature is present.
#[macro_export]
macro_rules! error {
(target: $target:expr, $($arg:tt)*) => (
Expand All @@ -51,8 +45,12 @@ macro_rules! error {

/// Logs a message at the warn level.
///
/// Logging at this level is disabled if any of the following cfgs are present:
/// `log_level = "off"` or `log_level = "error"`.
/// Logging at this level is disabled if any of the following features are
/// present: `max_level_off` or `max_level_error`.
///
/// When building in release mode (i.e., without the `debug_assertions` option),
/// logging at this level is also disabled if any of the following features are
/// present: `release_max_level_off` or `max_level_error`.
#[macro_export]
macro_rules! warn {
(target: $target:expr, $($arg:tt)*) => (
Expand All @@ -65,9 +63,13 @@ macro_rules! warn {

/// Logs a message at the info level.
///
/// Logging at this level is disabled if any of the following cfgs are present:
/// `log_level = "off"`, `log_level = "error"`, or
/// `log_level = "warn"`.
/// Logging at this level is disabled if any of the following features are
/// present: `max_level_off`, `max_level_error`, or `max_level_warn`.
///
/// When building in release mode (i.e., without the `debug_assertions` option),
/// logging at this level is also disabled if any of the following features are
/// present: `release_max_level_off`, `release_max_level_error`, or
/// `release_max_level_warn`.
#[macro_export]
macro_rules! info {
(target: $target:expr, $($arg:tt)*) => (
Expand All @@ -80,9 +82,14 @@ macro_rules! info {

/// Logs a message at the debug level.
///
/// Logging at this level is disabled if any of the following cfgs are present:
/// `log_level = "off"`, `log_level = "error"`, `log_level = "warn"`,
/// or `log_level = "info"`.
/// Logging at this level is disabled if any of the following features are
/// present: `max_level_off`, `max_level_error`, `max_level_warn`, or
/// `max_level_info`.
///
/// When building in release mode (i.e., without the `debug_assertions` option),
/// logging at this level is also disabled if any of the following features are
/// present: `release_max_level_off`, `release_max_level_error`,
/// `release_max_level_warn`, or `release_max_level_info`.
#[macro_export]
macro_rules! debug {
(target: $target:expr, $($arg:tt)*) => (
Expand All @@ -95,9 +102,15 @@ macro_rules! debug {

/// Logs a message at the trace level.
///
/// Logging at this level is disabled if any of the following cfgs are present:
/// `log_level = "off"`, `log_level = "error"`, `log_level = "warn"`,
/// `log_level = "info"`, or `log_level = "debug"`.
/// Logging at this level is disabled if any of the following features are
/// present: `max_level_off`, `max_level_error`, `max_level_warn`,
/// `max_level_info`, or `max_level_debug`.
///
/// When building in release mode (i.e., without the `debug_assertions` option),
/// logging at this level is also disabled if any of the following features are
/// present: `release_max_level_off`, `release_max_level_error`,
/// `release_max_level_warn`, `release_max_level_info`, or
/// `release_max_level_debug`.
#[macro_export]
macro_rules! trace {
(target: $target:expr, $($arg:tt)*) => (
Expand Down Expand Up @@ -135,12 +148,7 @@ macro_rules! trace {
macro_rules! log_enabled {
(target: $target:expr, $lvl:expr) => ({
let lvl = $lvl;
!cfg!(log_level = "off") &&
(lvl <= $crate::LogLevel::Error || !cfg!(log_level = "error")) &&
(lvl <= $crate::LogLevel::Warn || !cfg!(log_level = "warn")) &&
(lvl <= $crate::LogLevel::Debug || !cfg!(log_level = "debug")) &&
(lvl <= $crate::LogLevel::Info || !cfg!(log_level = "info")) &&
lvl <= $crate::max_log_level() &&
lvl <= $crate::__static_max_level() && lvl <= $crate::max_log_level() &&
$crate::__enabled(lvl, $target)
});
($lvl:expr) => (log_enabled!(target: module_path!(), $lvl))
Expand Down
11 changes: 11 additions & 0 deletions tests/max_level_features/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "optimized"
version = "0.1.0"

[[bin]]
name = "max_level_features"
path = "main.rs"

[dependencies.log]
path = "../.."
features = ["max_level_debug", "release_max_level_info"]
72 changes: 72 additions & 0 deletions tests/max_level_features/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#[macro_use] extern crate log;

use std::sync::{Arc, Mutex};
use log::{LogLevel, set_logger, LogLevelFilter, Log, LogRecord, LogMetadata};
use log::MaxLogLevelFilter;

struct State {
last_log: Mutex<Option<LogLevel>>,
filter: MaxLogLevelFilter,
}

struct Logger(Arc<State>);

impl Log for Logger {
fn enabled(&self, _: &LogMetadata) -> bool {
true
}

fn log(&self, record: &LogRecord) {
*self.0.last_log.lock().unwrap() = Some(record.level());
}
}

fn main() {
let mut a = None;
set_logger(|max| {
let me = Arc::new(State {
last_log: Mutex::new(None),
filter: max,
});
a = Some(me.clone());
Box::new(Logger(me))
}).unwrap();
let a = a.unwrap();

test(&a, LogLevelFilter::Off);
test(&a, LogLevelFilter::Error);
test(&a, LogLevelFilter::Warn);
test(&a, LogLevelFilter::Info);
test(&a, LogLevelFilter::Debug);
test(&a, LogLevelFilter::Trace);
}

fn test(a: &State, filter: LogLevelFilter) {
a.filter.set(filter);
error!("");
last(&a, t(LogLevel::Error, filter));
warn!("");
last(&a, t(LogLevel::Warn, filter));
info!("");
last(&a, t(LogLevel::Info, filter));

debug!("");
if cfg!(debug_assertions) {
last(&a, t(LogLevel::Debug, filter));
} else {
last(&a, None);
}

trace!("");
last(&a, None);

fn t(lvl: LogLevel, filter: LogLevelFilter) -> Option<LogLevel> {
if lvl <= filter {Some(lvl)} else {None}
}
}

fn last(state: &State, expected: Option<LogLevel>) {
let mut lvl = state.last_log.lock().unwrap();
assert_eq!(*lvl, expected);
*lvl = None;
}