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

Clarify documentation and simplify initialization of STATIC_MAX_LEVEL #594

Merged
merged 4 commits into from
Nov 19, 2023
Merged
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
134 changes: 72 additions & 62 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,26 +241,28 @@
//!
//! # Compile time filters
//!
//! Log levels can be statically disabled at compile time via Cargo features. Log invocations at
//! disabled levels will be skipped and will not even be present in the resulting binary.
//! This level is configured separately for release and debug builds. The features are:
//! Log levels can be statically disabled at compile time by enabling one of these Cargo features:
//!
//! * `max_level_off`
//! * `max_level_error`
//! * `max_level_warn`
//! * `max_level_info`
//! * `max_level_debug`
//! * `max_level_trace`
//!
//! Log invocations at disabled levels will be skipped and will not even be present in the
//! resulting binary. These features control the value of the `STATIC_MAX_LEVEL` constant. The
//! logging macros check this value before logging a message. By default, no levels are disabled.
//!
//! It is possible to override this level for release builds only with the following features:
//!
//! * `release_max_level_off`
//! * `release_max_level_error`
//! * `release_max_level_warn`
//! * `release_max_level_info`
//! * `release_max_level_debug`
//! * `release_max_level_trace`
//!
//! These features control the value of the `STATIC_MAX_LEVEL` constant. The logging macros check
//! this value before logging a message. By default, no levels are disabled.
//!
//! Libraries should avoid using the max level features because they're global and can't be changed
//! once they're set.
//!
Expand Down Expand Up @@ -334,6 +336,7 @@
#[cfg(all(not(feature = "std"), not(test)))]
extern crate core as std;

use std::cfg;
#[cfg(feature = "std")]
use std::error;
use std::str::FromStr;
Expand Down Expand Up @@ -1477,65 +1480,24 @@ pub mod __private_api;
/// should compare the level against this value.
///
/// [`logger`]: fn.logger.html
pub const STATIC_MAX_LEVEL: LevelFilter = MAX_LEVEL_INNER;

const MAX_LEVEL_INNER: LevelFilter = get_max_level_inner();

const fn get_max_level_inner() -> LevelFilter {
#[allow(unreachable_code)]
{
#[cfg(all(not(debug_assertions), feature = "release_max_level_off"))]
{
return LevelFilter::Off;
}
#[cfg(all(not(debug_assertions), feature = "release_max_level_error"))]
{
return LevelFilter::Error;
}
#[cfg(all(not(debug_assertions), feature = "release_max_level_warn"))]
{
return LevelFilter::Warn;
}
#[cfg(all(not(debug_assertions), feature = "release_max_level_info"))]
{
return LevelFilter::Info;
}
#[cfg(all(not(debug_assertions), feature = "release_max_level_debug"))]
{
return LevelFilter::Debug;
}
#[cfg(all(not(debug_assertions), feature = "release_max_level_trace"))]
{
return LevelFilter::Trace;
}
#[cfg(feature = "max_level_off")]
{
return LevelFilter::Off;
}
#[cfg(feature = "max_level_error")]
{
return LevelFilter::Error;
}
#[cfg(feature = "max_level_warn")]
{
return LevelFilter::Warn;
}
#[cfg(feature = "max_level_info")]
{
return LevelFilter::Info;
}
#[cfg(feature = "max_level_debug")]
{
return LevelFilter::Debug;
}

LevelFilter::Trace
}
}
pub const STATIC_MAX_LEVEL: LevelFilter = match cfg!(debug_assertions) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this will work with our MSRV?

Copy link
Contributor Author

@ptosi ptosi Oct 20, 2023

Choose a reason for hiding this comment

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

Interesting, which bit?

I've checked these changes with cargo +1.60.0 test; how do you recommend I test it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right, log used to have a MSRV of 1.34 or something, but 1.60 should work 👍

false if cfg!(feature = "release_max_level_off") => LevelFilter::Off,
false if cfg!(feature = "release_max_level_error") => LevelFilter::Error,
false if cfg!(feature = "release_max_level_warn") => LevelFilter::Warn,
false if cfg!(feature = "release_max_level_info") => LevelFilter::Info,
false if cfg!(feature = "release_max_level_debug") => LevelFilter::Debug,
false if cfg!(feature = "release_max_level_trace") => LevelFilter::Trace,
_ if cfg!(feature = "max_level_off") => LevelFilter::Off,
_ if cfg!(feature = "max_level_error") => LevelFilter::Error,
_ if cfg!(feature = "max_level_warn") => LevelFilter::Warn,
_ if cfg!(feature = "max_level_info") => LevelFilter::Info,
_ if cfg!(feature = "max_level_debug") => LevelFilter::Debug,
_ => LevelFilter::Trace,
};

#[cfg(test)]
mod tests {
use super::{Level, LevelFilter, ParseLevelError};
use super::{Level, LevelFilter, ParseLevelError, STATIC_MAX_LEVEL};

#[test]
fn test_levelfilter_from_str() {
Expand Down Expand Up @@ -1648,6 +1610,54 @@ mod tests {
}
}

#[test]
#[cfg_attr(not(debug_assertions), ignore)]
fn test_static_max_level_debug() {
if cfg!(feature = "max_level_off") {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Off);
} else if cfg!(feature = "max_level_error") {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Error);
} else if cfg!(feature = "max_level_warn") {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Warn);
} else if cfg!(feature = "max_level_info") {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Info);
} else if cfg!(feature = "max_level_debug") {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Debug);
} else {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Trace);
}
}

#[test]
#[cfg_attr(debug_assertions, ignore)]
fn test_static_max_level_release() {
if cfg!(feature = "release_max_level_off") {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Off);
} else if cfg!(feature = "release_max_level_error") {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Error);
} else if cfg!(feature = "release_max_level_warn") {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Warn);
} else if cfg!(feature = "release_max_level_info") {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Info);
} else if cfg!(feature = "release_max_level_debug") {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Debug);
} else if cfg!(feature = "release_max_level_trace") {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Trace);
} else if cfg!(feature = "max_level_off") {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Off);
} else if cfg!(feature = "max_level_error") {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Error);
} else if cfg!(feature = "max_level_warn") {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Warn);
} else if cfg!(feature = "max_level_info") {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Info);
} else if cfg!(feature = "max_level_debug") {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Debug);
} else {
assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Trace);
}
}

#[test]
#[cfg(feature = "std")]
fn test_error_trait() {
Expand Down