Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tracing: check log max level prior to dispatch check #1175

Merged
merged 4 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions tracing/src/level_filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,23 @@
//! [dependencies]
//! tracing = { version = "0.1", features = ["max_level_debug", "release_max_level_warn"] }
//! ```
//! ## Notes
//!
//! *Compiler support: requires rustc 1.39+*
//! Please note that `tracing`'s static max level features do *not* control the
//! [`log`] records that may be emitted when [`tracing`'s "log" feature flag][f] is
//! enabled. This is to allow `tracing` to be disabled entirely at compile time
//! while still emitting `log` records --- such as when a library using
//! `tracing` is used by an application using `log` that doesn't want to
//! generate any `tracing`-related code, but does want to collect `log` records.
//!
//! [`log` crate]: https://docs.rs/log/0.4.6/log/#compile-time-filters
//! This means that if the "log" feature is in use, some code may be generated
//! for `log` records emitted by disabled `tracing` events. If this is not
//! desirable, `log` records may be disabled separately using [`log`'s static
//! max level features][`log` crate].
//!
//! [`log`]: https://docs.rs/log/
//! [`log` crate]: https://docs.rs/log/latest/log/#compile-time-filters
//! [f]: : https://docs.rs/tracing/latest/tracing/#emitting-log-records
pub use tracing_core::{metadata::ParseLevelFilterError, LevelFilter};

/// The statically configured maximum trace level.
Expand Down
54 changes: 31 additions & 23 deletions tracing/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ macro_rules! span {
)
} else {
let span = CALLSITE.disabled_span();
$crate::if_log_enabled! {{
$crate::if_log_enabled! { $lvl, {
span.record_all(&$crate::valueset!(CALLSITE.metadata().fields(), $($fields)*));
}};
span
Expand Down Expand Up @@ -76,7 +76,7 @@ macro_rules! span {
)
} else {
let span = CALLSITE.disabled_span();
$crate::if_log_enabled! {{
$crate::if_log_enabled! { $lvl, {
span.record_all(&$crate::valueset!(CALLSITE.metadata().fields(), $($fields)*));
}};
span
Expand Down Expand Up @@ -2299,10 +2299,10 @@ macro_rules! __mk_format_args {
#[macro_export]
macro_rules! __tracing_log {
(target: $target:expr, $level:expr, $($field:tt)+ ) => {
$crate::if_log_enabled! {{
$crate::if_log_enabled! { $level, {
use $crate::log;
let level = $crate::level_to_log!($level);
if level <= log::STATIC_MAX_LEVEL && level <= log::max_level() {
if level <= log::max_level() {
let log_meta = log::Metadata::builder()
.level(level)
.target($target)
Expand All @@ -2326,13 +2326,13 @@ macro_rules! __tracing_log {
#[doc(hidden)]
#[macro_export]
macro_rules! if_log_enabled {
($e:expr;) => {
$crate::if_log_enabled! { $e }
($lvl:expr, $e:expr;) => {
$crate::if_log_enabled! { $lvl, $e }
};
($if_log:block) => {
$crate::if_log_enabled! { $if_log else {} }
($lvl:expr, $if_log:block) => {
$crate::if_log_enabled! { $lvl, $if_log else {} }
};
($if_log:block else $else_block:block) => {
($lvl:expr, $if_log:block else $else_block:block) => {
$else_block
};
}
Expand All @@ -2341,15 +2341,19 @@ macro_rules! if_log_enabled {
#[doc(hidden)]
#[macro_export]
macro_rules! if_log_enabled {
($e:expr;) => {
$crate::if_log_enabled! { $e }
($lvl:expr, $e:expr;) => {
$crate::if_log_enabled! { $lvl, $e }
};
($if_log:block) => {
$crate::if_log_enabled! { $if_log else {} }
($lvl:expr, $if_log:block) => {
$crate::if_log_enabled! { $lvl, $if_log else {} }
};
($if_log:block else $else_block:block) => {
if !$crate::dispatch::has_been_set() {
$if_log
($lvl:expr, $if_log:block else $else_block:block) => {
if $crate::level_to_log!($lvl) <= $crate::log::STATIC_MAX_LEVEL {
if !$crate::dispatcher::has_been_set() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails, similar to #1191:

error[E0433]: failed to resolve: maybe a missing crate `dispatcher`?
    --> /Users/ivan/projects/tracing/tracing/src/macros.rs:2352:25
     |
2352 |               if !$crate::dispatcher::has_been_set() {
     |                           ^^^^^^^^^^ maybe a missing crate `dispatcher`?
     |
    ::: /Users/ivan/projects/tracing/tracing/src/span.rs:765:9
     |
765  | /         if_log_enabled! { crate::Level::TRACE, {
766  | |             if let Some(ref meta) = self.meta {
767  | |                 self.log(ACTIVITY_LOG_TARGET, log::Level::Trace, format_args!("-> {}", meta.name()));
768  | |             }
769  | |         }}
     | |__________- in this macro invocation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #1217 to address this.

$if_log
} else {
$else_block
}
} else {
$else_block
}
Expand All @@ -2360,14 +2364,18 @@ macro_rules! if_log_enabled {
#[doc(hidden)]
#[macro_export]
macro_rules! if_log_enabled {
($e:expr;) => {
$crate::if_log_enabled! { $e }
($lvl:expr, $e:expr;) => {
$crate::if_log_enabled! { $lvl, $e }
};
($if_log:block) => {
$crate::if_log_enabled! { $if_log else {} }
($lvl:expr, $if_log:block) => {
$crate::if_log_enabled! { $lvl, $if_log else {} }
};
($if_log:block else $else_block:block) => {
#[allow(unused_braces)]
$if_log
($lvl:expr, $if_log:block else $else_block:block) => {
if $crate::level_to_log!($lvl) <= $crate::log::STATIC_MAX_LEVEL {
#[allow(unused_braces)]
$if_log
} else {
$else_block
}
};
}
37 changes: 19 additions & 18 deletions tracing/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ impl Span {
meta: Some(meta),
};

if_log_enabled! {{
if_log_enabled! { *meta.level(), {
let target = if attrs.is_empty() {
LIFECYCLE_LOG_TARGET
} else {
Expand All @@ -556,6 +556,7 @@ impl Span {

span
}

/// Enters this span, returning a guard that will exit the span when dropped.
///
/// If this span is enabled by the current collector, then this function will
Expand Down Expand Up @@ -761,7 +762,7 @@ impl Span {
inner.collector.enter(&inner.id);
}

if_log_enabled! {{
if_log_enabled! { crate::Level::TRACE, {
if let Some(ref meta) = self.meta {
self.log(ACTIVITY_LOG_TARGET, log::Level::Trace, format_args!("-> {}", meta.name()));
}
Expand Down Expand Up @@ -933,16 +934,16 @@ impl Span {
inner.record(&record);
}

if_log_enabled! {{
if let Some(ref meta) = self.meta {
if let Some(ref _meta) = self.meta {
if_log_enabled! { *_meta.level(), {
let target = if record.is_empty() {
LIFECYCLE_LOG_TARGET
} else {
meta.target()
_meta.target()
};
self.log(target, level_to_log!(*meta.level()), format_args!("{}{}", meta.name(), FmtValues(&record)));
}
}}
self.log(target, level_to_log!(*_meta.level()), format_args!("{}{}", _meta.name(), FmtValues(&record)));
}}
}

self
}
Expand Down Expand Up @@ -1148,15 +1149,15 @@ impl Drop for Span {
collector.try_close(id.clone());
}

if_log_enabled!({
if let Some(ref meta) = self.meta {
if let Some(ref _meta) = self.meta {
if_log_enabled! { crate::Level::TRACE, {
self.log(
LIFECYCLE_LOG_TARGET,
log::Level::Trace,
format_args!("-- {}", meta.name()),
format_args!("-- {}", _meta.name()),
);
}
})
}}
}
}
}

Expand Down Expand Up @@ -1251,11 +1252,11 @@ impl<'a> Drop for Entered<'a> {
inner.collector.exit(&inner.id);
}

if_log_enabled! {{
if let Some(ref meta) = self.span.meta {
self.span.log(ACTIVITY_LOG_TARGET, log::Level::Trace, format_args!("<- {}", meta.name()));
}
}}
if let Some(ref _meta) = self.span.meta {
if_log_enabled! { crate::Level::TRACE, {
self.span.log(ACTIVITY_LOG_TARGET, log::Level::Trace, format_args!("<- {}", _meta.name()));
}}
}
}
}

Expand Down