Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions crates/hiroz-union/src/core/logger.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
use tracing_subscriber::{EnvFilter, fmt};

/// Default filter directives when `RUST_LOG` is unset.
///
/// `hu` must be listed explicitly: the binary target is named `hu`
/// (`[[bin]] name = "hu"`), so every `tracing::` call in this crate emits under
/// the `hu` target. A filter naming only `hiroz` and `zenoh` drops all of them,
/// which is why the host's schema-discovery and decode warnings were emitted and
/// then discarded before reaching a terminal.
pub(crate) fn default_filter(debug: bool) -> &'static str {
if debug {
"hu=debug,hiroz=debug,zenoh=debug"
} else {
"hu=info,hiroz=info,zenoh=warn"
}
}

pub fn init_logger(json_mode: bool, debug: bool) {
// Build filter from RUST_LOG environment variable or default
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
if debug {
EnvFilter::new("hiroz=debug,zenoh=debug")
} else {
EnvFilter::new("hiroz=info,zenoh=warn")
}
});
let filter =
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(default_filter(debug)));

if json_mode {
// Structured JSON logs to stderr (for real-time visibility)
Expand All @@ -30,3 +40,24 @@ pub fn init_logger(json_mode: bool, debug: bool) {
.init();
}
}

#[cfg(test)]
mod tests {
use super::default_filter;
use tracing_subscriber::EnvFilter;

// The whole of the `hu` target fix is one string literal, and it is exactly
// the kind of line a later edit drops without noticing.
#[test]
fn defaults_name_the_hu_target_and_parse() {
for debug in [false, true] {
let directives = default_filter(debug);
assert!(
directives.contains("hu="),
"default filter for debug={debug} does not name the `hu` target: {directives}"
);
EnvFilter::try_new(directives)
.unwrap_or_else(|e| panic!("default filter {directives:?} does not parse: {e}"));
}
}
}
Loading