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
This commit is contained in:
Thomas Eizinger 2021-03-01 12:19:05 +11:00
parent cb4e2c041b
commit 8c0df23647
No known key found for this signature in database
GPG key ID: 651AC83A6C6C8B96
2 changed files with 33 additions and 17 deletions

View file

@ -35,7 +35,7 @@ use swap::{
}, },
seed::Seed, seed::Seed,
}; };
use tracing::{debug, error, warn, Level}; use tracing::{debug, error, info, warn, Level};
use tracing_subscriber::FmtSubscriber; use tracing_subscriber::FmtSubscriber;
use uuid::Uuid; use uuid::Uuid;
@ -46,20 +46,33 @@ const MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME: &str = "swap-tool-blockchain-mon
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
let args = Arguments::from_args();
let is_terminal = atty::is(atty::Stream::Stderr); let is_terminal = atty::is(atty::Stream::Stderr);
let subscriber = FmtSubscriber::builder() let base_subscriber = |level| {
.with_env_filter(format!("swap={}", Level::DEBUG)) FmtSubscriber::builder()
.with_writer(std::io::stderr) .with_writer(std::io::stderr)
.with_ansi(is_terminal) .with_ansi(is_terminal)
.with_target(false) .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( .with_timer(tracing_subscriber::fmt::time::ChronoLocal::with_format(
"%F %T".to_owned(), "%F %T".to_owned(),
)) ))
.finish(); .finish();
tracing::subscriber::set_global_default(subscriber)?; tracing::subscriber::set_global_default(subscriber)?;
} else {
let subscriber = base_subscriber(Level::INFO)
.without_time()
.with_level(false)
.finish();
let args = Arguments::from_args(); tracing::subscriber::set_global_default(subscriber)?;
}
let config = match args.config { let config = match args.config {
Some(config_path) => read_config(config_path)??, Some(config_path) => read_config(config_path)??,
@ -108,8 +121,8 @@ async fn main() -> Result<()> {
// TODO: Also wait for more funds if balance < dust // TODO: Also wait for more funds if balance < dust
if bitcoin_wallet.balance().await? == Amount::ZERO { if bitcoin_wallet.balance().await? == Amount::ZERO {
debug!( info!(
"Waiting for BTC at address {}", "Please deposit BTC to {}",
bitcoin_wallet.new_address().await? bitcoin_wallet.new_address().await?
); );
@ -121,7 +134,7 @@ async fn main() -> Result<()> {
debug!("Received {}", bitcoin_wallet.balance().await?); debug!("Received {}", bitcoin_wallet.balance().await?);
} else { } else {
debug!( info!(
"Still got {} left in wallet, swapping ...", "Still got {} left in wallet, swapping ...",
bitcoin_wallet.balance().await? bitcoin_wallet.balance().await?
); );

View file

@ -14,6 +14,9 @@ pub struct Arguments {
)] )]
pub config: Option<PathBuf>, pub config: Option<PathBuf>,
#[structopt(long, help = "Activate debug logging.")]
pub debug: bool,
#[structopt(subcommand)] #[structopt(subcommand)]
pub cmd: Option<Command>, pub cmd: Option<Command>,
} }