Skip to content

Commit

Permalink
Fix Vec implementation of tracing_subscriber::layer::Layer
Browse files Browse the repository at this point in the history
  • Loading branch information
stormshield-kg committed Jul 6, 2023
1 parent 4302064 commit dae763a
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
34 changes: 33 additions & 1 deletion tracing-mock/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
};
use tracing_core::{
span::{Attributes, Id, Record},
Event, Subscriber,
Event, Metadata, Subscriber,
};
use tracing_subscriber::{
layer::{Context, Layer},
Expand All @@ -23,6 +23,8 @@ use std::{
pub fn mock() -> MockLayerBuilder {
MockLayerBuilder {
expected: Default::default(),
enabled: true,
event_enabled: true,
name: std::thread::current()
.name()
.map(String::from)
Expand All @@ -37,12 +39,16 @@ pub fn named(name: impl std::fmt::Display) -> MockLayerBuilder {

pub struct MockLayerBuilder {
expected: VecDeque<Expect>,
enabled: bool,
event_enabled: bool,
name: String,
}

pub struct MockLayer {
expected: Arc<Mutex<VecDeque<Expect>>>,
current: Mutex<Vec<Id>>,
enabled: bool,
event_enabled: bool,
name: String,
}

Expand All @@ -57,6 +63,16 @@ impl MockLayerBuilder {
self
}

pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}

pub fn event_enabled(mut self, event_enabled: bool) -> Self {
self.event_enabled = event_enabled;
self
}

pub fn event(mut self, event: MockEvent) -> Self {
self.expected.push_back(Expect::Event(event));
self
Expand Down Expand Up @@ -88,6 +104,8 @@ impl MockLayerBuilder {
pub fn run(self) -> MockLayer {
MockLayer {
expected: Arc::new(Mutex::new(self.expected)),
enabled: self.enabled,
event_enabled: self.event_enabled,
name: self.name,
current: Mutex::new(Vec::new()),
}
Expand All @@ -98,6 +116,8 @@ impl MockLayerBuilder {
let handle = MockHandle::new(expected.clone(), self.name.clone());
let subscriber = MockLayer {
expected,
enabled: self.enabled,
event_enabled: self.event_enabled,
name: self.name,
current: Mutex::new(Vec::new()),
};
Expand Down Expand Up @@ -187,6 +207,10 @@ where
}

fn on_event(&self, event: &Event<'_>, cx: Context<'_, C>) {
if !self.enabled || !self.event_enabled {
return;
}

let name = event.metadata().name();
println!(
"[{}] event: {}; level: {}; target: {}",
Expand Down Expand Up @@ -362,6 +386,14 @@ where
fn on_id_change(&self, _old: &Id, _new: &Id, _ctx: Context<'_, C>) {
panic!("well-behaved subscribers should never do this to us, lol");
}

fn enabled(&self, _metadata: &Metadata<'_>, _ctx: Context<'_, C>) -> bool {
self.enabled
}

fn event_enabled(&self, _event: &Event<'_>, _ctx: Context<'_, C>) -> bool {
self.event_enabled
}
}

impl fmt::Debug for MockLayer {
Expand Down
10 changes: 8 additions & 2 deletions tracing-subscriber/src/layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1784,11 +1784,17 @@ feature! {
}

fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool {
self.iter().all(|l| l.enabled(metadata, ctx.clone()))
// We can't use `any()` here because we *must* iterate over
// all the subscribers, rather than short-circuiting, in case any of
// them are using per-layer filtering.
self.iter().fold(false, |enabled, l| enabled | l.enabled(metadata, ctx.clone()))
}

fn event_enabled(&self, event: &Event<'_>, ctx: Context<'_, S>) -> bool {
self.iter().all(|l| l.event_enabled(event, ctx.clone()))
// We can't use `any()` here because we *must* iterate over
// all the subscribers, rather than short-circuiting, in case any of
// them are using per-layer filtering.
self.iter().fold(false, |event_enabled, l| event_enabled | l.event_enabled(event, ctx.clone()))
}

fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) {
Expand Down
56 changes: 56 additions & 0 deletions tracing-subscriber/tests/layer_filters/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,62 @@ fn with_filters_boxed() {
target_handle.assert_finished();
}

#[test]
fn check_enabled() {
let (debug_layer, debug_handle) = layer::named("debug")
.enabled(false)
.done()
.run_with_handle();
let debug_layer = debug_layer.with_filter(LevelFilter::DEBUG);

let (info_layer, info_handle) = layer::named("info")
.event(event::mock().at_level(Level::INFO))
.done()
.run_with_handle();
let info_layer = info_layer.with_filter(LevelFilter::INFO);

let (error_layer, error_handle) = layer::named("error").done().run_with_handle();
let error_layer = error_layer.with_filter(LevelFilter::ERROR);

let _subscriber = tracing_subscriber::registry()
.with(vec![debug_layer, info_layer, error_layer])
.set_default();

tracing::info!("hello info");

debug_handle.assert_finished();
info_handle.assert_finished();
error_handle.assert_finished();
}

#[test]
fn check_event_enabled() {
let (debug_layer, debug_handle) = layer::named("debug")
.event_enabled(false)
.done()
.run_with_handle();
let debug_layer = debug_layer.with_filter(LevelFilter::DEBUG);

let (info_layer, info_handle) = layer::named("info")
.event(event::mock().at_level(Level::INFO))
.done()
.run_with_handle();
let info_layer = info_layer.with_filter(LevelFilter::INFO);

let (error_layer, error_handle) = layer::named("error").done().run_with_handle();
let error_layer = error_layer.with_filter(LevelFilter::ERROR);

let _subscriber = tracing_subscriber::registry()
.with(vec![debug_layer, info_layer, error_layer])
.set_default();

tracing::info!("hello info");

debug_handle.assert_finished();
info_handle.assert_finished();
error_handle.assert_finished();
}

#[test]
fn mixed_max_level_hint() {
let unfiltered = layer::named("unfiltered").run().boxed();
Expand Down

0 comments on commit dae763a

Please sign in to comment.