From 8c0df23647e536c8ae651d3e25994d28ca097d3a Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 1 Mar 2021 12:19:05 +1100 Subject: [PATCH] Only show _log_ output if the user passes `--debug` If the user doesn't pass `--debug`, we only show `INFO` logs but without time and level to make it clearer that it is meant to be read by the user. Without `--debug`, the user sees: Still got 0.00009235 BTC left in wallet, swapping ... With `--debug`, they see: 2021-03-01 12:21:07 DEBUG Database and seed will be stored in /home/thomas/.local/share/xmr-btc-swap 2021-03-01 12:21:07 DEBUG Starting monero-wallet-rpc on port 40779 2021-03-01 12:21:11 INFO Still got 0.00009235 BTC left in wallet, swapping ... 2021-03-01 12:21:11 DEBUG Dialing alice at 12D3KooWCdMKjesXMJz1SiZ7HgotrxuqhQJbP5sgBm2BwP1cqThi 2021-03-01 12:21:12 DEBUG Requesting quote for 0.00008795 BTC --- swap/src/bin/swap_cli.rs | 47 +++++++++++++++++++++++++--------------- swap/src/cli/command.rs | 3 +++ 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/swap/src/bin/swap_cli.rs b/swap/src/bin/swap_cli.rs index 83b56e3a..2c23503a 100644 --- a/swap/src/bin/swap_cli.rs +++ b/swap/src/bin/swap_cli.rs @@ -35,7 +35,7 @@ use swap::{ }, seed::Seed, }; -use tracing::{debug, error, warn, Level}; +use tracing::{debug, error, info, warn, Level}; use tracing_subscriber::FmtSubscriber; use uuid::Uuid; @@ -46,21 +46,34 @@ const MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME: &str = "swap-tool-blockchain-mon #[tokio::main] async fn main() -> Result<()> { - let is_terminal = atty::is(atty::Stream::Stderr); - let subscriber = FmtSubscriber::builder() - .with_env_filter(format!("swap={}", Level::DEBUG)) - .with_writer(std::io::stderr) - .with_ansi(is_terminal) - .with_target(false) - .with_timer(tracing_subscriber::fmt::time::ChronoLocal::with_format( - "%F %T".to_owned(), - )) - .finish(); - - tracing::subscriber::set_global_default(subscriber)?; - let args = Arguments::from_args(); + let is_terminal = atty::is(atty::Stream::Stderr); + let base_subscriber = |level| { + FmtSubscriber::builder() + .with_writer(std::io::stderr) + .with_ansi(is_terminal) + .with_target(false) + .with_env_filter(format!("swap={}", level)) + }; + + if args.debug { + let subscriber = base_subscriber(Level::DEBUG) + .with_timer(tracing_subscriber::fmt::time::ChronoLocal::with_format( + "%F %T".to_owned(), + )) + .finish(); + + tracing::subscriber::set_global_default(subscriber)?; + } else { + let subscriber = base_subscriber(Level::INFO) + .without_time() + .with_level(false) + .finish(); + + tracing::subscriber::set_global_default(subscriber)?; + } + let config = match args.config { Some(config_path) => read_config(config_path)??, None => Config::testnet(), @@ -108,8 +121,8 @@ async fn main() -> Result<()> { // TODO: Also wait for more funds if balance < dust if bitcoin_wallet.balance().await? == Amount::ZERO { - debug!( - "Waiting for BTC at address {}", + info!( + "Please deposit BTC to {}", bitcoin_wallet.new_address().await? ); @@ -121,7 +134,7 @@ async fn main() -> Result<()> { debug!("Received {}", bitcoin_wallet.balance().await?); } else { - debug!( + info!( "Still got {} left in wallet, swapping ...", bitcoin_wallet.balance().await? ); diff --git a/swap/src/cli/command.rs b/swap/src/cli/command.rs index ae859cf1..ff61d692 100644 --- a/swap/src/cli/command.rs +++ b/swap/src/cli/command.rs @@ -14,6 +14,9 @@ pub struct Arguments { )] pub config: Option, + #[structopt(long, help = "Activate debug logging.")] + pub debug: bool, + #[structopt(subcommand)] pub cmd: Option, }