#![cfg(feature = "logging")]
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use stratify::logging::{BaseStack, ConsoleConfig};
use tracing::subscriber::with_default;
use tracing_subscriber::layer::Context;
use tracing_subscriber::Layer;
#[derive(Clone)]
struct CountingLayer {
seen: Arc<AtomicUsize>,
}
impl CountingLayer {
fn new() -> (Self, Arc<AtomicUsize>) {
let seen = Arc::new(AtomicUsize::new(0));
(Self { seen: seen.clone() }, seen)
}
}
impl Layer<BaseStack> for CountingLayer {
fn on_event(&self, _event: &tracing::Event<'_>, _ctx: Context<'_, BaseStack>) {
self.seen.fetch_add(1, Ordering::SeqCst);
}
}
#[test]
fn a_custom_layer_receives_events() {
let (layer, seen) = CountingLayer::new();
let (subscriber, _handle) = stratify::logging::builder()
.with_filter(tracing_subscriber::EnvFilter::new("info"))
.with_layer(layer)
.build()
.expect("the stack should build with a custom layer");
with_default(subscriber, || {
tracing::info!("one");
tracing::info!("two");
});
assert_eq!(seen.load(Ordering::SeqCst), 2);
}
#[test]
fn several_custom_layers_all_receive_events() {
let (first, first_seen) = CountingLayer::new();
let (second, second_seen) = CountingLayer::new();
let (subscriber, _handle) = stratify::logging::builder()
.with_filter(tracing_subscriber::EnvFilter::new("info"))
.with_layer(first)
.with_layer(second)
.build()
.expect("two custom layers should compose");
with_default(subscriber, || tracing::info!("event"));
assert_eq!(first_seen.load(Ordering::SeqCst), 1);
assert_eq!(second_seen.load(Ordering::SeqCst), 1);
}
#[test]
fn a_custom_layer_coexists_with_the_built_in_sinks() {
let (layer, seen) = CountingLayer::new();
let (subscriber, _handle) = stratify::logging::builder()
.console(ConsoleConfig::default())
.with_filter(tracing_subscriber::EnvFilter::new("info"))
.with_layer(layer)
.build()
.expect("a custom layer and a console sink should coexist");
with_default(subscriber, || tracing::info!("event"));
assert_eq!(seen.load(Ordering::SeqCst), 1);
}
#[test]
fn the_filter_applies_to_custom_layers() {
let (layer, seen) = CountingLayer::new();
let (subscriber, _handle) = stratify::logging::builder()
.with_filter(tracing_subscriber::EnvFilter::new("warn"))
.with_layer(layer)
.build()
.expect("builds");
with_default(subscriber, || {
tracing::info!("filtered out");
tracing::warn!("kept");
});
assert_eq!(seen.load(Ordering::SeqCst), 1);
}
#[test]
fn adding_no_custom_layer_still_delivers_events() {
let (layer, seen) = CountingLayer::new();
let (subscriber, _handle) = stratify::logging::builder()
.with_filter(tracing_subscriber::EnvFilter::new("info"))
.with_layer(layer)
.build()
.expect("builds");
let (control, control_handle) = stratify::logging::builder()
.with_filter(tracing_subscriber::EnvFilter::new("info"))
.build()
.expect("builds without a custom layer");
drop(control_handle);
with_default(subscriber, || tracing::info!("with a layer"));
with_default(control, || tracing::info!("without a layer"));
assert_eq!(seen.load(Ordering::SeqCst), 1);
}