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 Nov 24, 2023
1 parent c6bedbe commit d1112bd
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
36 changes: 34 additions & 2 deletions tracing-mock/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
//!
//! ```should_panic
//! use tracing_mock::{expect, field, layer};
//! use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
//! use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
//!
//! let span = expect::span()
//! .named("my_span");
Expand Down Expand Up @@ -123,7 +123,7 @@ use crate::{
};
use tracing_core::{
span::{Attributes, Id, Record},
Event, Subscriber,
Event, Metadata, Subscriber,
};
use tracing_subscriber::{
layer::{Context, Layer},
Expand Down Expand Up @@ -188,6 +188,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 Down Expand Up @@ -259,6 +261,8 @@ pub fn named(name: impl std::fmt::Display) -> MockLayerBuilder {
pub struct MockLayerBuilder {
expected: VecDeque<Expect>,
enabled: bool,
event_enabled: bool,
name: String,
}

Expand All @@ -273,6 +277,8 @@ pub struct MockLayerBuilder {
pub struct MockLayer {
expected: Arc<Mutex<VecDeque<Expect>>>,
current: Mutex<Vec<Id>>,
enabled: bool,
event_enabled: bool,
name: String,
}

Expand Down Expand Up @@ -353,6 +359,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
}

/// Adds an expectation that an event matching the [`ExpectedEvent`]
/// will be recorded next.
///
Expand Down Expand Up @@ -729,6 +745,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 Down Expand Up @@ -764,6 +782,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 @@ -893,6 +913,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 @@ -1040,6 +1064,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 @@ -1802,11 +1802,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)
.only()
.run_with_handle();
let debug_layer = debug_layer.with_filter(LevelFilter::DEBUG);

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

let (error_layer, error_handle) = layer::named("error").only().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)
.only()
.run_with_handle();
let debug_layer = debug_layer.with_filter(LevelFilter::DEBUG);

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

let (error_layer, error_handle) = layer::named("error").only().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 d1112bd

Please sign in to comment.