mirror of
https://github.com/tokio-rs/tracing.git
synced 2025-10-02 23:34:40 +00:00
subscriber: add Targets::would_enable
(#1903
## Motivation As discussed on discord, this API + `Targets` being `: Clone` makes it easier to solve the original problem I had tried to solve in https://github.com/tokio-rs/tracing/pull/1889. My plan on how to use this is in https://github.com/MaterializeInc/materialize/issues/10441 if you are interested! ## Solution I considered doing some macro magic to create a `Metadata` with a callsite and empty fields and everything, to be able to called `DirectiveSet::enabled`, but it felt cleaner and easier to reason about the special-case-ness (`Targets` never having field filters) using a new set of methods that do a similar thing. For testing I opted for just a doc-test, let me know if thats fine!
This commit is contained in:
parent
4b2903fa5a
commit
b411a5c939
@ -5,7 +5,7 @@ use alloc::vec;
|
|||||||
use alloc::{string::String, vec::Vec};
|
use alloc::{string::String, vec::Vec};
|
||||||
|
|
||||||
use core::{cmp::Ordering, fmt, iter::FromIterator, slice, str::FromStr};
|
use core::{cmp::Ordering, fmt, iter::FromIterator, slice, str::FromStr};
|
||||||
use tracing_core::Metadata;
|
use tracing_core::{Level, Metadata};
|
||||||
/// Indicates that a string could not be parsed as a filtering directive.
|
/// Indicates that a string could not be parsed as a filtering directive.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ParseError {
|
pub struct ParseError {
|
||||||
@ -142,6 +142,22 @@ impl DirectiveSet<StaticDirective> {
|
|||||||
None => false,
|
None => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Same as `enabled` above, but skips `Directive`'s with fields.
|
||||||
|
pub(crate) fn target_enabled(&self, target: &str, level: &Level) -> bool {
|
||||||
|
match self.directives_for_target(target).next() {
|
||||||
|
Some(d) => d.level >= *level,
|
||||||
|
None => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn directives_for_target<'a>(
|
||||||
|
&'a self,
|
||||||
|
target: &'a str,
|
||||||
|
) -> impl Iterator<Item = &'a StaticDirective> + 'a {
|
||||||
|
self.directives()
|
||||||
|
.filter(move |d| d.cares_about_target(target))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// === impl StaticDirective ===
|
// === impl StaticDirective ===
|
||||||
@ -158,6 +174,22 @@ impl StaticDirective {
|
|||||||
level,
|
level,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(in crate::filter) fn cares_about_target(&self, to_check: &str) -> bool {
|
||||||
|
// Does this directive have a target filter, and does it match the
|
||||||
|
// metadata's target?
|
||||||
|
if let Some(ref target) = self.target {
|
||||||
|
if !to_check.starts_with(&target[..]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !self.field_names.is_empty() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ord for StaticDirective {
|
impl Ord for StaticDirective {
|
||||||
|
@ -20,7 +20,7 @@ use core::{
|
|||||||
slice,
|
slice,
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
use tracing_core::{Interest, Metadata, Subscriber};
|
use tracing_core::{Interest, Level, Metadata, Subscriber};
|
||||||
|
|
||||||
/// A filter that enables or disables spans and events based on their [target]
|
/// A filter that enables or disables spans and events based on their [target]
|
||||||
/// and [level].
|
/// and [level].
|
||||||
@ -313,6 +313,35 @@ impl Targets {
|
|||||||
Interest::never()
|
Interest::never()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns whether a [target]-[`Level`] pair would be enabled
|
||||||
|
/// by this `Targets`.
|
||||||
|
///
|
||||||
|
/// This method can be used with [`module_path!`] from `std` as the target
|
||||||
|
/// in order to emulate the behavior of the [`tracing::event!`] and [`tracing::span!`]
|
||||||
|
/// macros.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use tracing_subscriber::filter::{Targets, LevelFilter};
|
||||||
|
/// use tracing_core::Level;
|
||||||
|
///
|
||||||
|
/// let filter = Targets::new()
|
||||||
|
/// .with_target("my_crate", Level::INFO)
|
||||||
|
/// .with_target("my_crate::interesting_module", Level::DEBUG);
|
||||||
|
///
|
||||||
|
/// assert!(filter.would_enable("my_crate", &Level::INFO));
|
||||||
|
/// assert!(!filter.would_enable("my_crate::interesting_module", &Level::TRACE));
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// [target]: tracing_core::Metadata::target
|
||||||
|
/// [`module_path!`]: std::module_path!
|
||||||
|
pub fn would_enable(&self, target: &str, level: &Level) -> bool {
|
||||||
|
// "Correct" to call because `Targets` only produces `StaticDirective`'s with NO
|
||||||
|
// fields
|
||||||
|
self.0.target_enabled(target, level)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, L> Extend<(T, L)> for Targets
|
impl<T, L> Extend<(T, L)> for Targets
|
||||||
|
Loading…
x
Reference in New Issue
Block a user