• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![cfg(feature = "std")]
2 
3 use tracing::Level;
4 use tracing_mock::*;
5 
6 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
7 #[test]
multiple_max_level_hints()8 fn multiple_max_level_hints() {
9     // This test ensures that when multiple subscribers are active, their max
10     // level hints are handled correctly. The global max level should be the
11     // maximum of the level filters returned by the two `Subscriber`'s
12     // `max_level_hint` method.
13     //
14     // In this test, we create a subscriber whose max level is `INFO`, and
15     // another whose max level is `DEBUG`. We then add an assertion to both of
16     // those subscribers' `enabled` method that no metadata for `TRACE` spans or
17     // events are filtered, since they are disabled by the global max filter.
18 
19     fn do_events() {
20         tracing::info!("doing a thing that you might care about");
21         tracing::debug!("charging turboencabulator with interocitor");
22         tracing::warn!("extremely serious warning, pay attention");
23         tracing::trace!("interocitor charge level is 10%");
24         tracing::error!("everything is on fire");
25     }
26 
27     let (subscriber1, handle1) = subscriber::mock()
28         .named("subscriber1")
29         .with_max_level_hint(Level::INFO)
30         .with_filter(|meta| {
31             let level = dbg!(meta.level());
32             assert!(
33                 level <= &Level::DEBUG,
34                 "a TRACE event was dynamically filtered by subscriber1"
35             );
36             level <= &Level::INFO
37         })
38         .event(expect::event().at_level(Level::INFO))
39         .event(expect::event().at_level(Level::WARN))
40         .event(expect::event().at_level(Level::ERROR))
41         .only()
42         .run_with_handle();
43     let (subscriber2, handle2) = subscriber::mock()
44         .named("subscriber2")
45         .with_max_level_hint(Level::DEBUG)
46         .with_filter(|meta| {
47             let level = dbg!(meta.level());
48             assert!(
49                 level <= &Level::DEBUG,
50                 "a TRACE event was dynamically filtered by subscriber2"
51             );
52             level <= &Level::DEBUG
53         })
54         .event(expect::event().at_level(Level::INFO))
55         .event(expect::event().at_level(Level::DEBUG))
56         .event(expect::event().at_level(Level::WARN))
57         .event(expect::event().at_level(Level::ERROR))
58         .only()
59         .run_with_handle();
60 
61     let dispatch1 = tracing::Dispatch::new(subscriber1);
62 
63     tracing::dispatcher::with_default(&dispatch1, do_events);
64     handle1.assert_finished();
65 
66     let dispatch2 = tracing::Dispatch::new(subscriber2);
67     tracing::dispatcher::with_default(&dispatch2, do_events);
68     handle2.assert_finished();
69 }
70