Skip to content

implement Targets::enabled_for #1903

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

Merged
merged 3 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 43 additions & 1 deletion tracing-subscriber/src/filter/directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloc::vec;
use alloc::{string::String, vec::Vec};

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.
#[derive(Debug)]
pub struct ParseError {
Expand Down Expand Up @@ -38,6 +38,10 @@ pub(in crate::filter) trait Match {
fn level(&self) -> &LevelFilter;
}

pub(in crate::filter) trait TargetMatch: Match {
fn cares_about_target(&self, to_check: &str) -> bool;
}

#[derive(Debug)]
enum ParseErrorKind {
#[cfg(feature = "std")]
Expand Down Expand Up @@ -80,6 +84,17 @@ impl<T: Match + Ord> DirectiveSet<T> {
self.directives().filter(move |d| d.cares_about(metadata))
}

pub(crate) fn directives_for_target<'a>(
&'a self,
target: &'a str,
) -> impl Iterator<Item = &'a T> + 'a
where
T: TargetMatch,
{
self.directives()
.filter(move |d| d.cares_about_target(target))
}

pub(crate) fn add(&mut self, directive: T) {
// does this directive enable a more verbose level than the current
// max? if so, update the max level.
Expand Down Expand Up @@ -142,6 +157,15 @@ impl DirectiveSet<StaticDirective> {
None => false,
}
}

/// Same as `enabled` above, but short circuits to false if the
/// `Directive` has fields
pub(crate) fn enabled_for(&self, target: &'static str, level: &Level) -> bool {
match self.directives_for_target(target).next() {
Some(d) => d.level >= *level,
None => false,
}
}
}

// === impl StaticDirective ===
Expand Down Expand Up @@ -237,6 +261,24 @@ impl Match for StaticDirective {
}
}

impl TargetMatch for StaticDirective {
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 Default for StaticDirective {
fn default() -> Self {
StaticDirective {
Expand Down
32 changes: 31 additions & 1 deletion tracing-subscriber/src/filter/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use core::{
slice,
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]
/// and [level].
Expand Down Expand Up @@ -313,6 +313,36 @@ impl Targets {
Interest::never()
}
}

/// Returns whether a [target]-[`Level`] pair would be enabled
/// for this `Targets`.
///
/// You can use [`module_path!`] from `std` as the target
/// if you want to emulate the behavior of the
/// [`tracing::event!`] suite of 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.enabled_for("my_crate", &Level::INFO));
/// assert!(!filter.enabled_for("my_crate::interesting_module", &Level::TRACE));
/// ```
///
/// [target]: tracing_core::Metadata::target
/// [`tracing::event!`]: tracing::event!
Copy link
Member

Choose a reason for hiding this comment

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

i'm surprised this link needs a reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not great at rustdoc links so ill check if its needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this took forever to figure out, its not required, but rustdoc doesnt work for this unless --all-features is on (which is on for tracing-subscribers's docs.rs, which is good), as tracing is an optional dep!

/// [`module_path!`]: std::module_path!
pub fn enabled_for(&self, target: &'static str, level: &Level) -> bool {
// "Correct" to call because `Targets` only produces `StaticDirective`'s with NO
// fields
self.0.enabled_for(target, level)
}
}

impl<T, L> Extend<(T, L)> for Targets
Expand Down