llkv_test_utils/
lib.rs

1//! Shared helpers for test binaries, including tracing initialization.
2
3use std::sync::Once;
4
5static INIT: Once = Once::new();
6
7/// Initialize tracing for test binaries. Safe to call multiple times.
8pub fn init_tracing_for_tests() {
9    INIT.call_once(|| {
10        use tracing_subscriber::filter::EnvFilter;
11        use tracing_subscriber::fmt;
12        let env = std::env::var("RUST_LOG").ok();
13        let filter = match env {
14            Some(_) => EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
15            None => EnvFilter::new("info"),
16        };
17        fmt().with_env_filter(filter).with_target(false).init();
18    });
19}
20
21#[cfg(feature = "auto-init")]
22mod auto {
23    // Use ctor to run at binary init time to avoid having to call init in every test.
24    use ctor::ctor;
25
26    #[ctor]
27    fn init() {
28        super::init_tracing_for_tests();
29    }
30}