From 26f8147812c84a92b862d154db41d0697abf73ea Mon Sep 17 00:00:00 2001 From: Binarybaron Date: Tue, 24 Jun 2025 00:07:42 +0200 Subject: [PATCH] feat: log monero_rpc_pool only at >= INFO level --- swap/src/common/tracing_util.rs | 75 ++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 5 deletions(-) diff --git a/swap/src/common/tracing_util.rs b/swap/src/common/tracing_util.rs index 75dcf9a6..81ece2a1 100644 --- a/swap/src/common/tracing_util.rs +++ b/swap/src/common/tracing_util.rs @@ -72,6 +72,9 @@ pub fn init( "asb", "monero_sys", "unstoppableswap-gui-rs", + ]; + + let INFO_LEVEL_CRATES: Vec<&str> = vec![ "monero_rpc_pool", ]; @@ -98,7 +101,7 @@ pub fn init( .with_file(true) .with_line_number(true) .json() - .with_filter(env_filter(level_filter, OUR_CRATES.clone())?); + .with_filter(env_filter_with_info_crates(level_filter, OUR_CRATES.clone(), INFO_LEVEL_CRATES.clone())?); // Layer for writing to the verbose log file // Crates: All crates with different levels (libp2p at INFO+, others at TRACE) @@ -111,11 +114,12 @@ pub fn init( .with_file(true) .with_line_number(true) .json() - .with_filter(env_filter_with_libp2p_info( + .with_filter(env_filter_with_all_crates( LevelFilter::TRACE, OUR_CRATES.clone(), LIBP2P_CRATES.clone(), TOR_CRATES.clone(), + INFO_LEVEL_CRATES.clone(), )?); // Layer for writing to the terminal @@ -141,23 +145,25 @@ pub fn init( .with_file(true) .with_line_number(true) .json() - .with_filter(env_filter_with_libp2p_info( + .with_filter(env_filter_with_all_crates( level_filter, OUR_CRATES.clone(), LIBP2P_CRATES.clone(), TOR_CRATES.clone(), + INFO_LEVEL_CRATES.clone(), )?); // If trace_stdout is true, we log all messages to the terminal // Otherwise, we only log the bare minimum let terminal_layer_env_filter = match trace_stdout { - true => env_filter_with_libp2p_info( + true => env_filter_with_all_crates( LevelFilter::TRACE, OUR_CRATES.clone(), LIBP2P_CRATES.clone(), TOR_CRATES.clone(), + INFO_LEVEL_CRATES.clone(), )?, - false => env_filter(level_filter, OUR_CRATES.clone())?, + false => env_filter_with_info_crates(level_filter, OUR_CRATES.clone(), INFO_LEVEL_CRATES.clone())?, }; let final_terminal_layer = match format { @@ -199,6 +205,29 @@ fn env_filter(level_filter: LevelFilter, crates: Vec<&str>) -> Result Ok(filter) } +/// This function controls which crate's logs actually get logged and from which level, with info-level crates at INFO level or higher. +fn env_filter_with_info_crates( + level_filter: LevelFilter, + our_crates: Vec<&str>, + info_level_crates: Vec<&str>, +) -> Result { + let mut filter = EnvFilter::from_default_env(); + + // Add directives for each crate in the provided list + for crate_name in our_crates { + filter = filter.add_directive(Directive::from_str(&format!( + "{}={}", + crate_name, &level_filter + ))?); + } + + for crate_name in info_level_crates { + filter = filter.add_directive(Directive::from_str(&format!("{}=INFO", crate_name))?); + } + + Ok(filter) +} + /// This function controls which crate's logs actually get logged and from which level, with libp2p crates at INFO level or higher. fn env_filter_with_libp2p_info( level_filter: LevelFilter, @@ -230,6 +259,42 @@ fn env_filter_with_libp2p_info( Ok(filter) } +/// This function controls which crate's logs actually get logged and from which level, including all crate categories. +fn env_filter_with_all_crates( + level_filter: LevelFilter, + our_crates: Vec<&str>, + libp2p_crates: Vec<&str>, + tor_crates: Vec<&str>, + info_level_crates: Vec<&str>, +) -> Result { + let mut filter = EnvFilter::from_default_env(); + + // Add directives for each crate in the provided list + for crate_name in our_crates { + filter = filter.add_directive(Directive::from_str(&format!( + "{}={}", + crate_name, &level_filter + ))?); + } + + for crate_name in libp2p_crates { + filter = filter.add_directive(Directive::from_str(&format!("{}=INFO", crate_name))?); + } + + for crate_name in tor_crates { + filter = filter.add_directive(Directive::from_str(&format!( + "{}={}", + crate_name, &level_filter + ))?); + } + + for crate_name in info_level_crates { + filter = filter.add_directive(Directive::from_str(&format!("{}=INFO", crate_name))?); + } + + Ok(filter) +} + /// A writer that forwards tracing log messages to the tauri guest. #[derive(Clone)] pub struct TauriWriter {