From 2d5445645c5edd13fa06336d1539b7284b86f07b Mon Sep 17 00:00:00 2001 From: Ivan Babrou Date: Wed, 13 Jan 2021 17:44:08 -0800 Subject: [PATCH] Allow disabling logging feature Logging macro `if_log_enabled` checks both `log` and `always-log` crate features to select which macro version to use. Unfortunately, `log` feature cannot be set by crate consumers in either existing form or if added to `Cargo.toml`, probably because of a name clash with `log` crate: ```diff $ git diff Cargo.toml diff --git a/tracing/Cargo.toml b/tracing/Cargo.toml index 37bb61f..e030d27 100644 --- a/tracing/Cargo.toml +++ b/tracing/Cargo.toml @@ -63,7 +63,7 @@ release_max_level_trace = [] async-await = [] std = ["tracing-core/std"] +log = ["log"] log-always = ["log"] attributes = ["tracing-attributes"] ``` ``` Caused by: optional dependency `log` is not included in any feature Make sure that `dep:log` is included in one of features in the [features] table. ``` I'm renaming `log` feature to `logging` to work around that. There's a slight improvement in throughput with logging disabled: ``` global/collector/span_no_fields time: [7.9682 ns 7.9704 ns 7.9729 ns] change: [-13.902% -13.812% -13.720%] (p = 0.00 < 0.05) Performance has improved. ``` --- tracing/Cargo.toml | 3 ++- tracing/src/lib.rs | 6 +++--- tracing/src/macros.rs | 16 ++++++++-------- tracing/src/span.rs | 14 +++++++------- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/tracing/Cargo.toml b/tracing/Cargo.toml index 8f6ac422b3..e40807e4f3 100644 --- a/tracing/Cargo.toml +++ b/tracing/Cargo.toml @@ -67,7 +67,8 @@ async-await = [] alloc = ["tracing-core/alloc"] std = ["tracing-core/std", "alloc"] -log-always = ["log"] +logging = ["log"] +log-always = ["logging"] attributes = ["tracing-attributes"] [[bench]] diff --git a/tracing/src/lib.rs b/tracing/src/lib.rs index 68cd74ba91..2b99f6dffd 100644 --- a/tracing/src/lib.rs +++ b/tracing/src/lib.rs @@ -910,7 +910,7 @@ #[macro_use] extern crate cfg_if; -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[doc(hidden)] pub use log; @@ -1049,13 +1049,13 @@ pub mod __macro_support { } #[inline] - #[cfg(feature = "log")] + #[cfg(feature = "logging")] pub fn disabled_span(&self) -> crate::Span { crate::Span::new_disabled(self.meta) } #[inline] - #[cfg(not(feature = "log"))] + #[cfg(not(feature = "logging"))] pub fn disabled_span(&self) -> crate::Span { crate::Span::none() } diff --git a/tracing/src/macros.rs b/tracing/src/macros.rs index f69064ed8b..3e8fdb177c 100644 --- a/tracing/src/macros.rs +++ b/tracing/src/macros.rs @@ -2149,7 +2149,7 @@ macro_rules! fieldset { } -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[doc(hidden)] #[macro_export] macro_rules! level_to_log { @@ -2172,14 +2172,14 @@ macro_rules! __tracing_stringify { }; } -#[cfg(not(feature = "log"))] +#[cfg(not(feature = "logging"))] #[doc(hidden)] #[macro_export] macro_rules! __tracing_log { (target: $target:expr, $level:expr, $($field:tt)+ ) => {}; } -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[doc(hidden)] #[macro_export] macro_rules! __mk_format_string { @@ -2227,7 +2227,7 @@ macro_rules! __mk_format_string { } } -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[doc(hidden)] #[macro_export] macro_rules! __mk_format_args { @@ -2294,7 +2294,7 @@ macro_rules! __mk_format_args { }; } -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[doc(hidden)] #[macro_export] macro_rules! __tracing_log { @@ -2322,7 +2322,7 @@ macro_rules! __tracing_log { }; } -#[cfg(not(feature = "log"))] +#[cfg(not(feature = "logging"))] #[doc(hidden)] #[macro_export] macro_rules! if_log_enabled { @@ -2337,7 +2337,7 @@ macro_rules! if_log_enabled { }; } -#[cfg(all(feature = "log", not(feature = "log-always")))] +#[cfg(all(feature = "logging", not(feature = "log-always")))] #[doc(hidden)] #[macro_export] macro_rules! if_log_enabled { @@ -2356,7 +2356,7 @@ macro_rules! if_log_enabled { }; } -#[cfg(all(feature = "log", feature = "log-always"))] +#[cfg(all(feature = "logging", feature = "log-always"))] #[doc(hidden)] #[macro_export] macro_rules! if_log_enabled { diff --git a/tracing/src/span.rs b/tracing/src/span.rs index b9dd4ff0f7..3ded71d598 100644 --- a/tracing/src/span.rs +++ b/tracing/src/span.rs @@ -383,10 +383,10 @@ pub struct Entered<'a> { } /// `log` target for all span lifecycle (creation/enter/exit/close) records. -#[cfg(feature = "log")] +#[cfg(feature = "logging")] const LIFECYCLE_LOG_TARGET: &str = "tracing::span"; /// `log` target for span activity (enter/exit) records. -#[cfg(feature = "log")] +#[cfg(feature = "logging")] const ACTIVITY_LOG_TARGET: &str = "tracing::span::active"; // ===== impl Span ===== @@ -1030,7 +1030,7 @@ impl Span { self.meta } - #[cfg(feature = "log")] + #[cfg(feature = "logging")] #[inline] fn log(&self, target: &str, level: log::Level, message: fmt::Arguments<'_>) { if let Some(ref meta) = self.meta { @@ -1259,10 +1259,10 @@ impl<'a> Drop for Entered<'a> { } } -#[cfg(feature = "log")] +#[cfg(feature = "logging")] struct FmtValues<'a>(&'a Record<'a>); -#[cfg(feature = "log")] +#[cfg(feature = "logging")] impl<'a> fmt::Display for FmtValues<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut res = Ok(()); @@ -1275,10 +1275,10 @@ impl<'a> fmt::Display for FmtValues<'a> { } } -#[cfg(feature = "log")] +#[cfg(feature = "logging")] struct FmtAttrs<'a>(&'a Attributes<'a>); -#[cfg(feature = "log")] +#[cfg(feature = "logging")] impl<'a> fmt::Display for FmtAttrs<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut res = Ok(());