From 45fd6fb4e26c8d97d3655585e7a571b9139ee116 Mon Sep 17 00:00:00 2001 From: Binarybaron Date: Tue, 3 Dec 2024 21:22:06 +0100 Subject: [PATCH] add some logging, log everything to verbose file logger and tauri, only our crates to terminal and general logs --- swap/src/asb/network.rs | 8 ++++- swap/src/common/tor.rs | 2 ++ swap/src/common/tracing_util.rs | 54 ++++++++++++++++----------------- 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/swap/src/asb/network.rs b/swap/src/asb/network.rs index c749b9d59..031021469 100644 --- a/swap/src/asb/network.rs +++ b/swap/src/asb/network.rs @@ -68,7 +68,13 @@ pub mod transport { match tor_transport.add_onion_service(onion_service_config, ASB_ONION_SERVICE_PORT) { - Ok(addr) => vec![addr], + Ok(addr) => { + tracing::debug!( + %addr, + "Setting up onion service for libp2p to listen on" + ); + vec![addr] + } Err(err) => { tracing::warn!(error=%err, "Failed to listen on onion address"); vec![] diff --git a/swap/src/common/tor.rs b/swap/src/common/tor.rs index 070669a08..c92090f9e 100644 --- a/swap/src/common/tor.rs +++ b/swap/src/common/tor.rs @@ -21,6 +21,8 @@ pub async fn init_tor_client(data_dir: &Path) -> Result, tauri_handle: Option, ) -> Result<()> { + let ALL_CRATES: Vec<&str> = vec![ + "swap", + "asb", + "libp2p_community_tor", + "unstoppableswap-gui-rs", + "arti", + ]; + let OUR_CRATES: Vec<&str> = vec!["swap", "asb"]; + // General log file for non-verbose logs let file_appender: RollingFileAppender = tracing_appender::rolling::never(&dir, "swap-all.log"); @@ -44,36 +53,30 @@ pub fn init( .expect("initializing rolling file appender failed"); // Layer for writing to the general log file - // We want to log everything from the swap and asb crates at the level to our main persisted log file + // Crates: swap, asb + // Level: Passed in let file_layer = fmt::layer() .with_writer(file_appender) .with_ansi(false) .with_timer(UtcTime::rfc_3339()) .with_target(false) .json() - .with_filter(env_filter(level_filter, vec!["swap", "asb"])?); + .with_filter(env_filter(level_filter, OUR_CRATES.clone())?); // Layer for writing to the verbose log file - // We want to log everything from most crates at TRACE level to our verbose log files + // Crates: swap, asb, libp2p_community_tor, unstoppableswap-gui-rs, arti (all relevant crates) + // Level: TRACE let tracing_file_layer = fmt::layer() .with_writer(tracing_file_appender) .with_ansi(false) .with_timer(UtcTime::rfc_3339()) .with_target(false) .json() - .with_filter(env_filter( - LevelFilter::TRACE, - vec![ - "libp2p_community_tor", - "unstoppableswap-gui-rs", - "swap", - "asb", - "arti", - ], - )?); + .with_filter(env_filter(LevelFilter::TRACE, ALL_CRATES.clone())?); // Layer for writing to the terminal - // We want to log everything from the swap and asb crates at the level to the terminal + // Crates: swap, asb + // Level: Passed in let is_terminal = atty::is(atty::Stream::Stderr); let terminal_layer = fmt::layer() .with_writer(std::io::stdout) @@ -81,26 +84,21 @@ pub fn init( .with_timer(UtcTime::rfc_3339()) .with_target(false); - // Layer for writing to the tauri guest - // We want to log everything from the swap and asb crates (and libp2p_community_tor and unstoppableswap-gui-rs) at the level to the tauri guest + // Layer for writing to the Tauri guest. This will be displayed in the GUI. + // Crates: swap, asb, libp2p_community_tor, unstoppableswap-gui-rs, arti + // Level: Passed in let tauri_layer = fmt::layer() .with_writer(TauriWriter::new(tauri_handle)) .with_ansi(false) .with_timer(UtcTime::rfc_3339()) .with_target(true) .json() - .with_filter(env_filter( - level_filter, - vec![ - "swap", - "asb", - "libp2p_community_tor", - "unstoppableswap-gui-rs", - ], - )?); - - // We only want to log everything from the swap and asb crates at the level to the terminal - let env_filtered = env_filter(level_filter, vec!["swap", "asb", "libp2p_community_tor"])?; + .with_filter(env_filter(level_filter, ALL_CRATES.clone())?); + + // We only log the bare minimum to the terminal + // Crates: swap, asb + // Level: Passed in + let env_filtered = env_filter(level_filter, OUR_CRATES.clone())?; // Apply the environment filter and box the layer for the terminal let final_terminal_layer = match format {