mirror of
https://github.com/facebook/sapling.git
synced 2024-10-09 16:31:02 +03:00
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:
parent
bd321ea0e3
commit
c601c6a46d
@ -10,6 +10,7 @@ use std::path::PathBuf;
|
|||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use structopt::{clap::AppSettings, StructOpt};
|
use structopt::{clap::AppSettings, StructOpt};
|
||||||
use tokio_compat_02::FutureExt;
|
use tokio_compat_02::FutureExt;
|
||||||
|
use tracing::{event, Level};
|
||||||
|
|
||||||
use edenfs_client::EdenFsInstance;
|
use edenfs_client::EdenFsInstance;
|
||||||
use util::path::expand_path;
|
use util::path::expand_path;
|
||||||
@ -45,6 +46,9 @@ pub struct Command {
|
|||||||
#[structopt(long, parse(from_str = expand_path))]
|
#[structopt(long, parse(from_str = expand_path))]
|
||||||
home_dir: Option<PathBuf>,
|
home_dir: Option<PathBuf>,
|
||||||
|
|
||||||
|
#[structopt(long)]
|
||||||
|
pub debug: bool,
|
||||||
|
|
||||||
#[structopt(subcommand)]
|
#[structopt(subcommand)]
|
||||||
subcommand: SubCommand,
|
subcommand: SubCommand,
|
||||||
}
|
}
|
||||||
@ -85,6 +89,8 @@ impl Command {
|
|||||||
/// Execute subcommands. This function returns only a return code since all the error handling
|
/// Execute subcommands. This function returns only a return code since all the error handling
|
||||||
/// should be taken care of by each sub-command.
|
/// should be taken care of by each sub-command.
|
||||||
async fn dispatch(self) -> ExitCode {
|
async fn dispatch(self) -> ExitCode {
|
||||||
|
event!(Level::TRACE, cmd = ?self, "Dispatching");
|
||||||
|
|
||||||
let instance = self.get_instance();
|
let instance = self.get_instance();
|
||||||
match self.subcommand {
|
match self.subcommand {
|
||||||
SubCommand::Status(status) => status.run(instance).await,
|
SubCommand::Status(status) => status.run(instance).await,
|
||||||
|
@ -11,6 +11,7 @@ use std::process::Command;
|
|||||||
|
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
use tracing_subscriber::filter::EnvFilter;
|
||||||
|
|
||||||
fn python_fallback() -> Result<Command> {
|
fn python_fallback() -> Result<Command> {
|
||||||
if let Ok(args) = std::env::var("EDENFSCTL_REAL") {
|
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<()> {
|
fn main() -> Result<()> {
|
||||||
if std::env::var("EDENFSCTL_SKIP_RUST").is_ok() {
|
if std::env::var("EDENFSCTL_SKIP_RUST").is_ok() {
|
||||||
fallback()
|
fallback()
|
||||||
} else if let Ok(cmd) = edenfs_commands::Command::from_args_safe() {
|
} else if let Ok(cmd) = edenfs_commands::Command::from_args_safe() {
|
||||||
|
if cmd.debug {
|
||||||
|
setup_logging();
|
||||||
|
}
|
||||||
match cmd.run() {
|
match cmd.run() {
|
||||||
Ok(code) => std::process::exit(code),
|
Ok(code) => std::process::exit(code),
|
||||||
Err(e) => Err(e),
|
Err(e) => Err(e),
|
||||||
|
Loading…
Reference in New Issue
Block a user