From 2c243a3cb814939bbe40fda1608781825ab0d77d Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 22 Feb 2026 10:29:19 +0900 Subject: feat(tracing): add performance profiling via tracing crate Wire up the existing --profile flag with tracing-subscriber to emit span timings on stderr. Supports MOZART_LOG env var override and verbose-level-aware filtering. No subscriber is installed when --profile is off, keeping tracing macros zero-cost. Co-Authored-By: Claude Opus 4.6 --- crates/mozart/src/main.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'crates/mozart/src/main.rs') diff --git a/crates/mozart/src/main.rs b/crates/mozart/src/main.rs index 59ad392..ebe84e5 100644 --- a/crates/mozart/src/main.rs +++ b/crates/mozart/src/main.rs @@ -1,9 +1,45 @@ use clap::Parser; use mozart::commands; use mozart_core::exit_code; +use tracing_subscriber::{fmt, prelude::*, EnvFilter}; + +fn init_tracing(profile: bool, verbose: u8, quiet: bool) { + // MOZART_LOG environment variable takes highest priority. + if let Ok(env_filter) = EnvFilter::try_from_env("MOZART_LOG") { + tracing_subscriber::registry() + .with(fmt::layer().with_writer(std::io::stderr)) + .with(env_filter) + .init(); + return; + } + + if profile { + let filter = match verbose { + 0 => "mozart=info", + 1 | 2 => "mozart=debug", + _ => "mozart=trace", + }; + tracing_subscriber::registry() + .with( + fmt::layer() + .with_writer(std::io::stderr) + .with_timer(fmt::time::uptime()) + .with_span_events(fmt::format::FmtSpan::CLOSE), + ) + .with(EnvFilter::new(filter)) + .init(); + } else if verbose >= 3 && !quiet { + tracing_subscriber::registry() + .with(fmt::layer().with_writer(std::io::stderr).with_target(false)) + .with(EnvFilter::new("mozart=debug")) + .init(); + } + // Otherwise: no subscriber installed → tracing macros are effectively zero-cost no-ops. +} fn main() { let cli = commands::Cli::parse(); + init_tracing(cli.profile, cli.verbose, cli.quiet); match commands::execute(&cli) { Ok(()) => {} Err(e) => { -- cgit v1.3.1