summaryrefslogtreecommitdiffstats
path: root/vendor/tracing/tests/enabled.rs
blob: ea1c69804d8b962d22bbc04ae862fb33bd0795b1 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#![cfg(feature = "std")]
use tracing::Level;
use tracing_mock::*;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn level_and_target() {
    let subscriber = subscriber::mock()
        .with_filter(|meta| {
            if meta.target() == "debug_module" {
                meta.level() <= &Level::DEBUG
            } else {
                meta.level() <= &Level::INFO
            }
        })
        .done()
        .run();

    let _guard = tracing::subscriber::set_default(subscriber);

    assert!(tracing::enabled!(target: "debug_module", Level::DEBUG));
    assert!(tracing::enabled!(Level::ERROR));
    assert!(!tracing::enabled!(Level::DEBUG));
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn span_and_event() {
    let subscriber = subscriber::mock()
        .with_filter(|meta| {
            if meta.target() == "debug_module" {
                meta.level() <= &Level::DEBUG
            } else if meta.is_span() {
                meta.level() <= &Level::TRACE
            } else if meta.is_event() {
                meta.level() <= &Level::DEBUG
            } else {
                meta.level() <= &Level::INFO
            }
        })
        .done()
        .run();

    let _guard = tracing::subscriber::set_default(subscriber);

    // Ensure that the `_event` and `_span` alternatives work corretly
    assert!(!tracing::event_enabled!(Level::TRACE));
    assert!(tracing::event_enabled!(Level::DEBUG));
    assert!(tracing::span_enabled!(Level::TRACE));

    // target variants
    assert!(tracing::span_enabled!(target: "debug_module", Level::DEBUG));
    assert!(tracing::event_enabled!(target: "debug_module", Level::DEBUG));
}