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

pass through max_level_hint in reload::Layer #2204

Merged
merged 1 commit into from Jul 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion tracing-subscriber/src/reload.rs
Expand Up @@ -30,7 +30,7 @@ use std::{
use tracing_core::{
callsite,
collect::{Collect, Interest},
span, Event, Metadata,
span, Event, LevelFilter, Metadata,
};

/// Wraps a `Filter` or `Subscribe`, allowing it to be reloaded dynamically at runtime.
Expand Down Expand Up @@ -135,6 +135,11 @@ where
fn on_id_change(&self, old: &span::Id, new: &span::Id, ctx: subscribe::Context<'_, C>) {
try_lock!(self.inner.read()).on_id_change(old, new, ctx)
}

#[inline]
fn max_level_hint(&self) -> Option<LevelFilter> {
try_lock!(self.inner.read(), else return None).max_level_hint()
}
}

#[cfg(all(feature = "registry", feature = "std"))]
Expand Down Expand Up @@ -188,6 +193,11 @@ where
fn on_close(&self, id: span::Id, ctx: subscribe::Context<'_, C>) {
try_lock!(self.inner.read()).on_close(id, ctx)
}

#[inline]
fn max_level_hint(&self) -> Option<LevelFilter> {
try_lock!(self.inner.read(), else return None).max_level_hint()
}
}

impl<T> Subscriber<T> {
Expand Down
32 changes: 24 additions & 8 deletions tracing-subscriber/tests/reload.rs
@@ -1,12 +1,16 @@
#![cfg(feature = "reload")]
#![cfg(feature = "registry")]
use std::sync::atomic::{AtomicUsize, Ordering};
use tracing_core::{
collect::Interest,
span::{Attributes, Id, Record},
Collect, Event, Metadata,
Collect, Event, LevelFilter, Metadata,
};
use tracing_subscriber::{prelude::*, reload::*, subscribe};

fn event() {
tracing::info!("my event");
}

pub struct NopCollector;

impl Collect for NopCollector {
Expand Down Expand Up @@ -67,9 +71,13 @@ fn reload_handle() {
};
true
}
}
fn event() {
tracing::trace!("my event");

fn max_level_hint(&self) -> Option<LevelFilter> {
match self {
Filter::One => Some(LevelFilter::INFO),
Filter::Two => Some(LevelFilter::DEBUG),
}
}
}

let (subscriber, handle) = Subscriber::new(Filter::One);
Expand All @@ -85,7 +93,9 @@ fn reload_handle() {
assert_eq!(FILTER1_CALLS.load(Ordering::SeqCst), 1);
assert_eq!(FILTER2_CALLS.load(Ordering::SeqCst), 0);

assert_eq!(LevelFilter::current(), LevelFilter::INFO);
handle.reload(Filter::Two).expect("should reload");
assert_eq!(LevelFilter::current(), LevelFilter::DEBUG);

event();

Expand Down Expand Up @@ -113,9 +123,13 @@ fn reload_filter() {
};
true
}
}
fn event() {
tracing::trace!("my event");

fn max_level_hint(&self) -> Option<LevelFilter> {
match self {
Filter::One => Some(LevelFilter::INFO),
Filter::Two => Some(LevelFilter::DEBUG),
}
}
}

let (filter, handle) = Subscriber::new(Filter::One);
Expand All @@ -133,7 +147,9 @@ fn reload_filter() {
assert_eq!(FILTER1_CALLS.load(Ordering::SeqCst), 1);
assert_eq!(FILTER2_CALLS.load(Ordering::SeqCst), 0);

assert_eq!(LevelFilter::current(), LevelFilter::INFO);
handle.reload(Filter::Two).expect("should reload");
assert_eq!(LevelFilter::current(), LevelFilter::DEBUG);
Comment on lines +150 to +152
Copy link
Member

Choose a reason for hiding this comment

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

slight pref for having a separate test for max level hinting, but 🤷‍♀️ i don't care that much


event();

Expand Down