aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src/test_utils/tracing.rs
blob: 207a462562aabafcc19e8cb9432cce3683aa7d51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use bevy_log::tracing_subscriber::{
    self, EnvFilter, Layer,
    layer::{Context, SubscriberExt},
    util::SubscriberInitExt,
};
use tracing::{Event, Level, Subscriber};

pub fn init_tracing() {
    init_tracing_with_level(Level::WARN);
}

pub fn init_tracing_with_level(max_level: Level) {
    tracing_subscriber::registry()
        .with(
            tracing_subscriber::fmt::layer().with_filter(
                EnvFilter::builder()
                    .with_default_directive(max_level.into())
                    .from_env_lossy(),
            ),
        )
        .with(TestTracingLayer {
            panic_on_level: max_level,
        })
        .init();
}

struct TestTracingLayer {
    panic_on_level: Level,
}
impl<S: Subscriber> Layer<S> for TestTracingLayer {
    fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) {
        let level = *event.metadata().level();
        if level <= self.panic_on_level {
            panic!("logged on level {level}");
        }
    }
}