diff options
Diffstat (limited to 'crates/mozart')
| -rw-r--r-- | crates/mozart/Cargo.toml | 2 | ||||
| -rw-r--r-- | crates/mozart/src/commands.rs | 41 | ||||
| -rw-r--r-- | crates/mozart/src/main.rs | 36 |
3 files changed, 79 insertions, 0 deletions
diff --git a/crates/mozart/Cargo.toml b/crates/mozart/Cargo.toml index 8a803fa..ec35e24 100644 --- a/crates/mozart/Cargo.toml +++ b/crates/mozart/Cargo.toml @@ -20,6 +20,8 @@ serde.workspace = true serde_json.workspace = true sha1.workspace = true tempfile.workspace = true +tracing.workspace = true +tracing-subscriber.workspace = true [dev-dependencies] assert_cmd.workspace = true diff --git a/crates/mozart/src/commands.rs b/crates/mozart/src/commands.rs index a745b3a..7b94f4d 100644 --- a/crates/mozart/src/commands.rs +++ b/crates/mozart/src/commands.rs @@ -197,6 +197,47 @@ pub enum Commands { Validate(validate::ValidateArgs), } +impl Commands { + pub fn name(&self) -> &'static str { + match self { + Commands::About(_) => "about", + Commands::Archive(_) => "archive", + Commands::Audit(_) => "audit", + Commands::Browse(_) => "browse", + Commands::Bump(_) => "bump", + Commands::CheckPlatformReqs(_) => "check-platform-reqs", + Commands::ClearCache(_) => "clear-cache", + Commands::Completion(_) => "completion", + Commands::Config(_) => "config", + Commands::CreateProject(_) => "create-project", + Commands::Depends(_) => "depends", + Commands::Diagnose(_) => "diagnose", + Commands::DumpAutoload(_) => "dump-autoload", + Commands::Exec(_) => "exec", + Commands::Fund(_) => "fund", + Commands::Global(_) => "global", + Commands::Init(_) => "init", + Commands::Install(_) => "install", + Commands::Licenses(_) => "licenses", + Commands::Outdated(_) => "outdated", + Commands::Prohibits(_) => "prohibits", + Commands::Reinstall(_) => "reinstall", + Commands::Remove(_) => "remove", + Commands::Repository(_) => "repository", + Commands::Require(_) => "require", + Commands::RunScript(_) => "run-script", + Commands::Search(_) => "search", + Commands::SelfUpdate(_) => "self-update", + Commands::Show(_) => "show", + Commands::Status(_) => "status", + Commands::Suggests(_) => "suggests", + Commands::Update(_) => "update", + Commands::Validate(_) => "validate", + } + } +} + +#[tracing::instrument(skip(cli), fields(command = cli.command.name()))] pub fn execute(cli: &Cli) -> anyhow::Result<()> { let console = mozart_core::console::Console::new( cli.verbose, 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) => { |
