Skip to main content

trusty_code/logging/
mod.rs

1//! Tracing and logging initialisation for tcode.
2//!
3//! Why: All tcode binaries and the library's test harness need consistent
4//! tracing initialisation that writes to stderr (never stdout — stdout is the
5//! API transport channel). Centralising it here prevents duplicated setup and
6//! ensures the `try_init` pattern is used everywhere so test binaries remain
7//! idempotent.
8//! What: `init_tracing` initialises the global tracing subscriber from the
9//! `RUST_LOG` env var. `init_tracing_for_test` is a lightweight variant for
10//! test binaries that silently drops duplicate init errors.
11//! Test: `init_tracing_for_test_is_idempotent` calls the function twice and
12//! asserts no panic.
13
14use tracing_subscriber::EnvFilter;
15
16/// Initialise the global tracing subscriber.
17///
18/// Why: All daemons and CLI entry points call this once at startup to ensure
19/// log output is consistently routed to stderr with the `RUST_LOG` filter.
20/// What: Installs a stderr-bound fmt subscriber with `EnvFilter::from_default_env()`.
21/// Panics if called twice (use `init_tracing_for_test` in test binaries).
22/// Test: Called in `main.rs`; correctness is verified by observing log output.
23pub fn init_tracing() {
24    tracing_subscriber::fmt()
25        .with_env_filter(EnvFilter::from_default_env())
26        .with_writer(std::io::stderr)
27        .init();
28}
29
30/// Initialise the global tracing subscriber, silently ignoring duplicate inits.
31///
32/// Why: Test binaries may link multiple test modules that all call setup code.
33/// `try_init` returns an error on the second call instead of panicking, so
34/// test runs remain stable regardless of execution order.
35/// What: Calls `try_init()`; swallows the error if already initialised.
36/// Test: `init_tracing_for_test_is_idempotent`.
37pub fn init_tracing_for_test() {
38    let _ = tracing_subscriber::fmt()
39        .with_env_filter(EnvFilter::from_default_env())
40        .with_writer(std::io::stderr)
41        .try_init();
42}
43
44/// Log level used when no `RUST_LOG` env var is set.
45///
46/// Why: Documents and centralises the default so operators know what to expect
47/// without reading source.
48/// What: A static string literal; the default filter used by `EnvFilter::from_default_env()`.
49/// Test: Not directly tested — the value is advisory documentation.
50pub const DEFAULT_LOG_LEVEL: &str = "info";
51
52// ── Tests ─────────────────────────────────────────────────────────────────────
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    /// `init_tracing_for_test` must be idempotent across multiple calls.
59    ///
60    /// Why: Test binaries are often multi-crate and may call init from several
61    /// setup functions; a panic on re-init would break the test suite.
62    /// What: Calls `init_tracing_for_test` twice; asserts no panic.
63    /// Test: This test.
64    #[test]
65    fn init_tracing_for_test_is_idempotent() {
66        init_tracing_for_test();
67        init_tracing_for_test(); // second call must not panic
68    }
69
70    /// The default log level constant is non-empty.
71    ///
72    /// Why: Guard against accidental empty string.
73    /// What: Asserts `DEFAULT_LOG_LEVEL` is non-empty.
74    /// Test: This test.
75    #[test]
76    fn default_log_level_is_non_empty() {
77        assert!(!DEFAULT_LOG_LEVEL.is_empty());
78    }
79}