Skip to content

Commit

Permalink
Merge pull request #575 from EFanZh/group-target-module-path-and-file
Browse files Browse the repository at this point in the history
Group `target_module`, `path` and `file` arguments
  • Loading branch information
KodrAus committed Aug 12, 2023
2 parents cab1088 + 3985711 commit 838920c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 64 deletions.
57 changes: 57 additions & 0 deletions src/__private_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! WARNING: this is not part of the crate's public API and is subject to change at any time

use crate::{Level, Metadata, Record};
use std::fmt::Arguments;
pub use std::option::Option;
pub use std::{file, format_args, line, module_path, stringify};

#[cfg(not(feature = "kv_unstable"))]
pub fn log(
args: Arguments,
level: Level,
&(target, module_path, file): &(&str, &'static str, &'static str),
line: u32,
kvs: Option<&[(&str, &str)]>,
) {
if kvs.is_some() {
panic!(
"key-value support is experimental and must be enabled using the `kv_unstable` feature"
)
}

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

#[cfg(feature = "kv_unstable")]
pub fn log(
args: Arguments,
level: Level,
&(target, module_path, file): &(&str, &'static str, &'static str),
line: u32,
kvs: Option<&[(&str, &dyn crate::kv::ToValue)]>,
) {
crate::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(),
);
}

pub fn enabled(level: Level, target: &str) -> bool {
crate::logger().enabled(&Metadata::builder().level(level).target(target).build())
}
60 changes: 1 addition & 59 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1466,65 +1466,7 @@ pub fn logger() -> &'static dyn Log {

// WARNING: this is not part of the crate's public API and is subject to change at any time
#[doc(hidden)]
#[cfg(not(feature = "kv_unstable"))]
pub fn __private_api_log(
args: fmt::Arguments,
level: Level,
&(target, module_path, file, line): &(&str, &'static str, &'static str, u32),
kvs: Option<&[(&str, &str)]>,
) {
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(),
);
}

// WARNING: this is not part of the crate's public API and is subject to change at any time
#[doc(hidden)]
#[cfg(feature = "kv_unstable")]
pub fn __private_api_log(
args: fmt::Arguments,
level: Level,
&(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(),
);
}

// 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())
}

// WARNING: this is not part of the crate's public API and is subject to change at any time
#[doc(hidden)]
pub mod __private_api {
pub use std::option::Option;
pub use std::{file, format_args, line, module_path, stringify};
}
pub mod __private_api;

/// The statically resolved maximum log level.
///
Expand Down
12 changes: 7 additions & 5 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ macro_rules! 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() {
$crate::__private_api_log(
$crate::__private_api::log(
$crate::__private_api::format_args!($($arg)+),
lvl,
&($target, $crate::__private_api::module_path!(), $crate::__private_api::file!(), $crate::__private_api::line!()),
&($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()),
$crate::__private_api::line!(),
$crate::__private_api::Option::Some(&[$(($crate::__log_key!($key), &$value)),+])
);
}
Expand All @@ -46,10 +47,11 @@ macro_rules! log {
(target: $target:expr, $lvl:expr, $($arg:tt)+) => ({
let lvl = $lvl;
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
$crate::__private_api_log(
$crate::__private_api::log(
$crate::__private_api::format_args!($($arg)+),
lvl,
&($target, $crate::__private_api::module_path!(), $crate::__private_api::file!(), $crate::__private_api::line!()),
&($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()),
$crate::__private_api::line!(),
$crate::__private_api::Option::None,
);
}
Expand Down Expand Up @@ -217,7 +219,7 @@ macro_rules! log_enabled {
let lvl = $lvl;
lvl <= $crate::STATIC_MAX_LEVEL
&& lvl <= $crate::max_level()
&& $crate::__private_api_enabled(lvl, $target)
&& $crate::__private_api::enabled(lvl, $target)
}};
($lvl:expr) => {
$crate::log_enabled!(target: $crate::__private_api::module_path!(), $lvl)
Expand Down

0 comments on commit 838920c

Please sign in to comment.