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,
};
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?
);

View File

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