Skip to content

Commit

Permalink
Move lvl <= $crate::max_level() inside called functions
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed May 31, 2022
1 parent 7fb28c3 commit 85822bf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
55 changes: 30 additions & 25 deletions src/lib.rs
Expand Up @@ -1588,22 +1588,24 @@ pub fn __private_api_log(
&(target, module_path, file, line): &(&str, &'static str, &'static str, u32),
kvs: Option<&[(&str, &str)]>,
) {
if kvs.is_some() {
panic!(
if level <= max_level() {
if kvs.is_some() {
panic!(
"key-value support is experimental and must be enabled using the `kv_unstable` feature"
)
}
}

logger().log(
&Record::builder()
.args(args)
.level(level)
.target(target)
.module_path_static(Some(module_path))
.file_static(Some(file))
.line(Some(line))
.build(),
);
logger().log(
&Record::builder()
.args(args)
.level(level)
.target(target)
.module_path_static(Some(module_path))
.file_static(Some(file))
.line(Some(line))
.build(),
);
}
}

// WARNING: this is not part of the crate's public API and is subject to change at any time
Expand All @@ -1615,23 +1617,26 @@ pub fn __private_api_log(
&(target, module_path, file, line): &(&str, &'static str, &'static str, u32),
kvs: Option<&[(&str, &dyn kv::ToValue)]>,
) {
logger().log(
&Record::builder()
.args(args)
.level(level)
.target(target)
.module_path_static(Some(module_path))
.file_static(Some(file))
.line(Some(line))
.key_values(&kvs)
.build(),
);
if level <= max_level() {
logger().log(
&Record::builder()
.args(args)
.level(level)
.target(target)
.module_path_static(Some(module_path))
.file_static(Some(file))
.line(Some(line))
.key_values(&kvs)
.build(),
);
}
}

// WARNING: this is not part of the crate's public API and is subject to change at any time
#[doc(hidden)]
pub fn __private_api_enabled(level: Level, target: &str) -> bool {
logger().enabled(&Metadata::builder().level(level).target(target).build())
level <= max_level()
&& logger().enabled(&Metadata::builder().level(level).target(target).build())
}

// WARNING: this is not part of the crate's public API and is subject to change at any time
Expand Down
8 changes: 3 additions & 5 deletions src/macros.rs
Expand Up @@ -32,7 +32,7 @@ macro_rules! log {
// log!(target: "my_target", Level::Info; key1 = 42, key2 = true; "a {} event", "log");
(target: $target:expr, $lvl:expr, $($key:tt = $value:expr),+; $($arg:tt)+) => ({
let lvl = $lvl;
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
if lvl <= $crate::STATIC_MAX_LEVEL {
$crate::__private_api_log(
__log_format_args!($($arg)+),
lvl,
Expand All @@ -45,7 +45,7 @@ macro_rules! log {
// log!(target: "my_target", Level::Info; "a {} event", "log");
(target: $target:expr, $lvl:expr, $($arg:tt)+) => ({
let lvl = $lvl;
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
if lvl <= $crate::STATIC_MAX_LEVEL {
$crate::__private_api_log(
__log_format_args!($($arg)+),
lvl,
Expand Down Expand Up @@ -215,9 +215,7 @@ macro_rules! trace {
macro_rules! log_enabled {
(target: $target:expr, $lvl:expr) => {{
let lvl = $lvl;
lvl <= $crate::STATIC_MAX_LEVEL
&& lvl <= $crate::max_level()
&& $crate::__private_api_enabled(lvl, $target)
lvl <= $crate::STATIC_MAX_LEVEL && $crate::__private_api_enabled(lvl, $target)
}};
($lvl:expr) => {
log_enabled!(target: __log_module_path!(), $lvl)
Expand Down

0 comments on commit 85822bf

Please sign in to comment.