From 3f534d00e485ba7f029b9ef348385154e4e51dfd Mon Sep 17 00:00:00 2001 From: EFanZh Date: Sun, 6 Aug 2023 18:50:48 +0800 Subject: [PATCH 1/2] Move private APIs into a single module --- src/__private_api.rs | 55 ++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 60 +------------------------------------------- src/macros.rs | 6 ++--- 3 files changed, 59 insertions(+), 62 deletions(-) create mode 100644 src/__private_api.rs diff --git a/src/__private_api.rs b/src/__private_api.rs new file mode 100644 index 000000000..e89c07b5c --- /dev/null +++ b/src/__private_api.rs @@ -0,0 +1,55 @@ +//! 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, 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" + ) + } + + 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, line): &(&str, &'static str, &'static str, 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()) +} diff --git a/src/lib.rs b/src/lib.rs index f19f00f99..c8df4b21c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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. /// diff --git a/src/macros.rs b/src/macros.rs index a66c92a4e..308682b6b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -33,7 +33,7 @@ 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!()), @@ -46,7 +46,7 @@ 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!()), @@ -217,7 +217,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) From 3985711f74bb75a6504c41f6f4b66ad5fd29ef41 Mon Sep 17 00:00:00 2001 From: EFanZh Date: Sun, 6 Aug 2023 18:54:26 +0800 Subject: [PATCH 2/2] Group `target`, `module_path` and `file` arguments --- src/__private_api.rs | 6 ++++-- src/macros.rs | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/__private_api.rs b/src/__private_api.rs index e89c07b5c..7304deb89 100644 --- a/src/__private_api.rs +++ b/src/__private_api.rs @@ -9,7 +9,8 @@ pub use std::{file, format_args, line, module_path, stringify}; pub fn log( args: Arguments, level: Level, - &(target, module_path, file, line): &(&str, &'static str, &'static str, u32), + &(target, module_path, file): &(&str, &'static str, &'static str), + line: u32, kvs: Option<&[(&str, &str)]>, ) { if kvs.is_some() { @@ -34,7 +35,8 @@ pub fn log( pub fn log( args: Arguments, level: Level, - &(target, module_path, file, line): &(&str, &'static str, &'static str, u32), + &(target, module_path, file): &(&str, &'static str, &'static str), + line: u32, kvs: Option<&[(&str, &dyn crate::kv::ToValue)]>, ) { crate::logger().log( diff --git a/src/macros.rs b/src/macros.rs index 308682b6b..281ff2572 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -36,7 +36,8 @@ macro_rules! 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)),+]) ); } @@ -49,7 +50,8 @@ macro_rules! 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, ); }