Skip to main content

matrix_sdk_test_utils/
lib.rs

1/// Initialize a tracing subscriber if the target architecture is not WASM.
2///
3/// Uses a sensible default filter that can be overridden through the `RUST_LOG`
4/// environment variable and runs once before all tests by using the [`ctor`]
5/// crate.
6///
7/// Invoke this macro once per compilation unit (`lib.rs`, `tests/*.rs`,
8/// `tests/*/main.rs`).
9#[macro_export]
10macro_rules! init_tracing_for_tests {
11    () => {
12        #[cfg(not(target_family = "wasm"))]
13        $crate::__macro_support::ctor! {
14            #[ctor(unsafe)]
15            fn init_logging() {
16                use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
17                use $crate::__macro_support::tracing_subscriber;
18
19                tracing_subscriber::registry()
20                    .with(tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
21                        // Output is only printed for failing tests, but still we shouldn't overload
22                        // the output with unnecessary info. When debugging a specific test, it's easy
23                        // to override this default by setting the `RUST_LOG` environment variable.
24                        //
25                        // Since tracing_subscriber does prefix matching, the `matrix_sdk=` directive
26                        // takes effect for all the main crates (`matrix_sdk_base`, `matrix_sdk_crypto`
27                        // and so on).
28                        "info,matrix_sdk=debug".into()
29                    }))
30                    .with(tracing_subscriber::fmt::layer().with_test_writer())
31                    .init();
32            }
33        }
34    };
35}
36
37#[doc(hidden)]
38pub mod __macro_support {
39    #[cfg(not(target_family = "wasm"))]
40    pub use ctor::declarative::ctor;
41    #[cfg(not(target_family = "wasm"))]
42    pub use tracing_subscriber;
43}