cli_rs: set up debug logging

Summary: This diff sets up debug logging for EdenFS CLI with tracing.

Reviewed By: chadaustin

Differential Revision: D26354205

fbshipit-source-id: bcc89fe3eaf4c7ae7642b8c20fd74fd3ea6dd4ee
This commit is contained in:
Zeyi (Rice) Fan 2021-02-12 12:30:49 -08:00 committed by Facebook GitHub Bot
parent bd321ea0e3
commit c601c6a46d
2 changed files with 27 additions and 0 deletions

View File

@ -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<PathBuf>,
#[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,

View File

@ -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<Command> {
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),