Skip to content

Commit

Permalink
Remove local_inner_macros usage
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jul 10, 2023
1 parent e9123d6 commit 9d052b1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 67 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,7 @@ pub fn __private_api_enabled(level: Level, target: &str) -> bool {
#[doc(hidden)]
pub mod __private_api {
pub use std::option::Option;
pub use std::{file, format_args, line, module_path, stringify};
}

/// The statically resolved maximum log level.
Expand Down
92 changes: 25 additions & 67 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@
/// data.0, data.1, private_data);
/// # }
/// ```
#[macro_export(local_inner_macros)]
#[macro_export]
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() {
$crate::__private_api_log(
__log_format_args!($($arg)+),
$crate::__private_api::format_args!($($arg)+),
lvl,
&($target, __log_module_path!(), __log_file!(), __log_line!()),
$crate::__private_api::Option::Some(&[$((__log_key!($key), &$value)),+])
&($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 @@ -47,16 +47,16 @@ macro_rules! log {
let lvl = $lvl;
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
$crate::__private_api_log(
__log_format_args!($($arg)+),
$crate::__private_api::format_args!($($arg)+),
lvl,
&($target, __log_module_path!(), __log_file!(), __log_line!()),
&($target, $crate::__private_api::module_path!(), $crate::__private_api::file!(), $crate::__private_api::line!()),
$crate::__private_api::Option::None,
);
}
});

// log!(Level::Info, "a log event")
($lvl:expr, $($arg:tt)+) => (log!(target: __log_module_path!(), $lvl, $($arg)+));
($lvl:expr, $($arg:tt)+) => ($crate::log!(target: $crate::__private_api::module_path!(), $lvl, $($arg)+));
}

/// Logs a message at the error level.
Expand All @@ -73,14 +73,14 @@ macro_rules! log {
/// error!(target: "app_events", "App Error: {}, Port: {}", err_info, 22);
/// # }
/// ```
#[macro_export(local_inner_macros)]
#[macro_export]
macro_rules! error {
// error!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
// error!(target: "my_target", "a {} event", "log")
(target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Error, $($arg)+));
(target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Error, $($arg)+));

// error!("a {} event", "log")
($($arg:tt)+) => (log!($crate::Level::Error, $($arg)+))
($($arg:tt)+) => ($crate::log!($crate::Level::Error, $($arg)+))
}

/// Logs a message at the warn level.
Expand All @@ -97,14 +97,14 @@ macro_rules! error {
/// warn!(target: "input_events", "App received warning: {}", warn_description);
/// # }
/// ```
#[macro_export(local_inner_macros)]
#[macro_export]
macro_rules! warn {
// warn!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
// warn!(target: "my_target", "a {} event", "log")
(target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Warn, $($arg)+));
(target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Warn, $($arg)+));

// warn!("a {} event", "log")
($($arg:tt)+) => (log!($crate::Level::Warn, $($arg)+))
($($arg:tt)+) => ($crate::log!($crate::Level::Warn, $($arg)+))
}

/// Logs a message at the info level.
Expand All @@ -123,14 +123,14 @@ macro_rules! warn {
/// conn_info.port, conn_info.speed);
/// # }
/// ```
#[macro_export(local_inner_macros)]
#[macro_export]
macro_rules! info {
// info!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
// info!(target: "my_target", "a {} event", "log")
(target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Info, $($arg)+));
(target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Info, $($arg)+));

// info!("a {} event", "log")
($($arg:tt)+) => (log!($crate::Level::Info, $($arg)+))
($($arg:tt)+) => ($crate::log!($crate::Level::Info, $($arg)+))
}

/// Logs a message at the debug level.
Expand All @@ -148,14 +148,14 @@ macro_rules! info {
/// debug!(target: "app_events", "New position: x: {}, y: {}", pos.x, pos.y);
/// # }
/// ```
#[macro_export(local_inner_macros)]
#[macro_export]
macro_rules! debug {
// debug!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
// debug!(target: "my_target", "a {} event", "log")
(target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Debug, $($arg)+));
(target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Debug, $($arg)+));

// debug!("a {} event", "log")
($($arg:tt)+) => (log!($crate::Level::Debug, $($arg)+))
($($arg:tt)+) => ($crate::log!($crate::Level::Debug, $($arg)+))
}

/// Logs a message at the trace level.
Expand All @@ -175,14 +175,14 @@ macro_rules! debug {
/// if pos.y >= 0.0 { "positive" } else { "negative" });
/// # }
/// ```
#[macro_export(local_inner_macros)]
#[macro_export]
macro_rules! trace {
// trace!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
// trace!(target: "my_target", "a {} event", "log")
(target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Trace, $($arg)+));
(target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Trace, $($arg)+));

// trace!("a {} event", "log")
($($arg:tt)+) => (log!($crate::Level::Trace, $($arg)+))
($($arg:tt)+) => ($crate::log!($crate::Level::Trace, $($arg)+))
}

/// Determines if a message logged at the specified level in that module will
Expand Down Expand Up @@ -211,7 +211,7 @@ macro_rules! trace {
/// # fn expensive_call() -> Data { Data { x: 0, y: 0 } }
/// # fn main() {}
/// ```
#[macro_export(local_inner_macros)]
#[macro_export]
macro_rules! log_enabled {
(target: $target:expr, $lvl:expr) => {{
let lvl = $lvl;
Expand All @@ -220,49 +220,7 @@ macro_rules! log_enabled {
&& $crate::__private_api_enabled(lvl, $target)
}};
($lvl:expr) => {
log_enabled!(target: __log_module_path!(), $lvl)
};
}

// The log macro above cannot invoke format_args directly because it uses
// local_inner_macros. A format_args invocation there would resolve to
// $crate::format_args which does not exist. Instead invoke format_args here
// outside of local_inner_macros so that it resolves (probably) to
// core::format_args or std::format_args. Same for the several macros that
// follow.
//
// This is a workaround until we drop support for pre-1.30 compilers. At that
// point we can remove use of local_inner_macros, use $crate:: when invoking
// local macros, and invoke format_args directly.
#[doc(hidden)]
#[macro_export]
macro_rules! __log_format_args {
($($args:tt)*) => {
format_args!($($args)*)
};
}

#[doc(hidden)]
#[macro_export]
macro_rules! __log_module_path {
() => {
module_path!()
};
}

#[doc(hidden)]
#[macro_export]
macro_rules! __log_file {
() => {
file!()
};
}

#[doc(hidden)]
#[macro_export]
macro_rules! __log_line {
() => {
line!()
$crate::log_enabled!(target: $crate::__private_api::module_path!(), $lvl)
};
}

Expand All @@ -271,7 +229,7 @@ macro_rules! __log_line {
macro_rules! __log_key {
// key1 = 42
($($args:ident)*) => {
stringify!($($args)*)
$crate::__private_api::stringify!($($args)*)
};
// "key1" = 42
($($args:expr)*) => {
Expand Down

0 comments on commit 9d052b1

Please sign in to comment.