diff --git a/eden/fs/cli_rs/edenfs-commands/src/lib.rs b/eden/fs/cli_rs/edenfs-commands/src/lib.rs index 8cf0c21174..5a12cb0f58 100644 --- a/eden/fs/cli_rs/edenfs-commands/src/lib.rs +++ b/eden/fs/cli_rs/edenfs-commands/src/lib.rs @@ -10,6 +10,7 @@ use std::path::PathBuf; use anyhow::{Context, Result}; use structopt::{clap::AppSettings, StructOpt}; use tokio_compat_02::FutureExt; +use tracing::{event, Level}; use edenfs_client::EdenFsInstance; use util::path::expand_path; @@ -45,6 +46,9 @@ pub struct Command { #[structopt(long, parse(from_str = expand_path))] home_dir: Option, + #[structopt(long)] + pub debug: bool, + #[structopt(subcommand)] subcommand: SubCommand, } @@ -85,6 +89,8 @@ impl Command { /// Execute subcommands. This function returns only a return code since all the error handling /// should be taken care of by each sub-command. async fn dispatch(self) -> ExitCode { + event!(Level::TRACE, cmd = ?self, "Dispatching"); + let instance = self.get_instance(); match self.subcommand { SubCommand::Status(status) => status.run(instance).await, diff --git a/eden/fs/cli_rs/edenfsctl/src/main.rs b/eden/fs/cli_rs/edenfsctl/src/main.rs index a3479783b1..2739535063 100644 --- a/eden/fs/cli_rs/edenfsctl/src/main.rs +++ b/eden/fs/cli_rs/edenfsctl/src/main.rs @@ -11,6 +11,7 @@ use std::process::Command; use anyhow::{anyhow, Context, Result}; use structopt::StructOpt; +use tracing_subscriber::filter::EnvFilter; fn python_fallback() -> Result { if let Ok(args) = std::env::var("EDENFSCTL_REAL") { @@ -53,10 +54,30 @@ fn fallback() -> Result<()> { } } +/// Setup tracing logging. If we are in development mode, we use the fancier logger, otherwise a +/// simple logger for production use. Logs will be printined to stderr when `--debug` flag is +/// passed. +fn setup_logging() { + let subscriber = tracing_subscriber::fmt(); + #[cfg(debug_assertions)] + let subscriber = subscriber.pretty(); + let subscriber = subscriber.with_env_filter(EnvFilter::from_env("EDENFS_LOG")); + + if let Err(e) = subscriber.try_init() { + eprintln!( + "Unable to initialize logger. Logging will be disabled. Cause: {:?}", + e + ); + } +} + fn main() -> Result<()> { if std::env::var("EDENFSCTL_SKIP_RUST").is_ok() { fallback() } else if let Ok(cmd) = edenfs_commands::Command::from_args_safe() { + if cmd.debug { + setup_logging(); + } match cmd.run() { Ok(code) => std::process::exit(code), Err(e) => Err(e),