thruput_testkit/logging.rs
1//! Optional stderr tracing for forwarded `hog-devnode` logs.
2//!
3//! The supervisor forwards `hog-devnode`'s logs through the `tracing` framework
4//! (under the `hogd_dev` target, with its own diagnostics under
5//! `thruput_node_supervisor`). These helpers install a stderr subscriber so
6//! those events actually print, mirroring what `thruput node` does.
7
8use thruput_node_supervisor::LogLevel;
9
10/// Install a stderr `tracing` subscriber that prints forwarded `hog-devnode`
11/// logs and supervisor diagnostics.
12///
13/// Idempotent and non-clobbering: it uses `try_init`, so it never replaces a
14/// subscriber a test already installed, and it honors `RUST_LOG` when set. A
15/// `local` target with a configured `log_level` installs this automatically;
16/// call it directly to print logs without setting a specific devnode level.
17pub fn init_tracing() {
18 install("hogd_dev=info,thruput_node_supervisor=info");
19}
20
21/// Install a subscriber whose default filter matches the requested devnode
22/// `level`, so logs emitted at that verbosity print.
23pub(crate) fn install_for_level(level: LogLevel) {
24 let level = match level {
25 LogLevel::Trace => "trace",
26 LogLevel::Debug => "debug",
27 LogLevel::Info => "info",
28 LogLevel::Warn => "warn",
29 LogLevel::Error | LogLevel::Critical => "error",
30 };
31 install(&format!("hogd_dev={level},thruput_node_supervisor=info"));
32}
33
34fn install(default_filter: &str) {
35 use tracing_subscriber::EnvFilter;
36
37 let _ = tracing_subscriber::fmt()
38 .with_env_filter(
39 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(default_filter)),
40 )
41 .with_writer(std::io::stderr)
42 .with_target(false)
43 .without_time()
44 .try_init();
45}