-
Notifications
You must be signed in to change notification settings - Fork 283
Statically disable debug logging in optimized builds #58
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)+) => ({ | ||
|
|
@@ -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() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)+)) | ||
| } | ||
| }); | ||
|
|
@@ -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)*) => ( | ||
|
|
@@ -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)*) => ( | ||
|
|
@@ -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)*) => ( | ||
|
|
@@ -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)*) => ( | ||
|
|
@@ -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)*) => ( | ||
|
|
@@ -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)) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.0here? The intention with that is to ensure that this continues to always compile on 1.0.0 RustThere was a problem hiding this comment.
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
There was a problem hiding this comment.
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...