Skip to content

Commit

Permalink
add some logging, log everything to verbose file logger and tauri, on…
Browse files Browse the repository at this point in the history
…ly our crates to terminal and general logs
  • Loading branch information
binarybaron committed Dec 3, 2024
1 parent 3677d20 commit 45fd6fb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
8 changes: 7 additions & 1 deletion swap/src/asb/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![]
Expand Down
2 changes: 2 additions & 0 deletions swap/src/common/tor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub async fn init_tor_client(data_dir: &Path) -> Result<Arc<TorClient<TokioRustl
// It uses cached information when possible.)
let runtime = TokioRustlsRuntime::current().expect("We are always running with tokio");

tracing::debug!("Bootstrapping Tor client");

let tor_client = TorClient::with_runtime(runtime)
.config(config)
.create_bootstrapped()
Expand Down
54 changes: 26 additions & 28 deletions swap/src/common/tracing_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ pub fn init(
dir: impl AsRef<Path>,
tauri_handle: Option<TauriHandle>,
) -> 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");

Expand All @@ -44,63 +53,52 @@ 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)
.with_ansi(is_terminal)
.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 {
Expand Down

0 comments on commit 45fd6fb

Please sign in to comment.