From b060c75cc7f66db5a60470cc95adb5d7a86c4706 Mon Sep 17 00:00:00 2001 From: Einliterflasche Date: Tue, 20 Aug 2024 13:02:33 +0200 Subject: [PATCH] fix: hoist state --- swap/src/common/mod.rs | 30 ++++++++++++++++++++++-------- swap/src/common/tracing_util.rs | 4 +++- swap/src/database/sqlite.rs | 6 ++++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/swap/src/common/mod.rs b/swap/src/common/mod.rs index a1a8f13a..fd1d0fd5 100644 --- a/swap/src/common/mod.rs +++ b/swap/src/common/mod.rs @@ -72,12 +72,14 @@ macro_rules! regex_find_placeholders { /// /// If specified, filter by swap id or redact addresses. pub async fn get_logs(logs_dir: PathBuf, swap_id: Option, redact_addresses: bool) -> anyhow::Result> { - tracing::debug!(logs_dir=%logs_dir.display(), "reading logfiles from"); + tracing::debug!("reading logfiles from {}", logs_dir.display()); // get all files in the directory let mut log_files = read_dir(&logs_dir).await?; let mut log_messages = Vec::new(); + // when we redact we need to store the placeholder + let mut placeholders = HashMap::new(); // print all lines from every log file. TODO: sort files by date? while let Some(entry) = log_files.next_entry().await? { @@ -94,20 +96,30 @@ pub async fn get_logs(logs_dir: PathBuf, swap_id: Option, redact_addresses continue; } + // use BufReader to stay easy on memory and then read line by line let buf_reader = BufReader::new(File::open(&file_path).await?); let mut lines = buf_reader.lines(); // print each line, redacted if the flag is set while let Some(line) = lines.next_line().await? { - // check if we should filter this line + // if we should filter by swap id, check if the line contains it if let Some(swap_id) = swap_id { - if line.contains(&swap_id.to_string()) { + if !line.contains(&swap_id.to_string()) { continue; } } + if line.contains(r#""level":"TRACE""#) { + continue; + } + + // skip debug logs + if line.contains(r#""level":"DEBUG""#) { + continue; + } + // redact if necessary - let line = if redact_addresses { redact(&line) } else { line }; + let line = if redact_addresses { redact_with(&line, &mut placeholders) } else { line }; // save redacted message log_messages.push(line); } @@ -127,11 +139,14 @@ pub async fn get_logs(logs_dir: PathBuf, swap_id: Option, redact_addresses /// assert_eq!(redacted, ""); /// ``` pub fn redact(input: &str) -> String { - // Use a hashmap to keep track of which address we replace with which placeholder - let mut replacements: HashMap = HashMap::new(); + let mut replacements = HashMap::new(); + redact_with(input, &mut replacements) +} +/// Same as [`redact`] but retrieves palceholders from and stores them +/// in a specified hashmap. +pub fn redact_with(input: &str, replacements: &mut HashMap) -> String { // TODO: verify regex patterns - const MONERO_ADDR_REGEX: &str = r#"[48][1-9A-HJ-NP-Za-km-z]{94}"#; const BITCOIN_ADDR_REGEX: &str = r#"\b[13][a-km-zA-HJ-NP-Z1-9]{25,34}\b"#; // Both XMR and BTC transactions have @@ -173,7 +188,6 @@ pub fn redact(input: &str) -> String { // Finally we go through the input string and replace each occurance of an // address we want to redact with the corresponding placeholder for (address, placeholder) in replacements.iter() { - println!("replacing `{address}` with `{placeholder}`"); redacted = redacted.replace(address, placeholder); } diff --git a/swap/src/common/tracing_util.rs b/swap/src/common/tracing_util.rs index 43086432..a3ddff73 100644 --- a/swap/src/common/tracing_util.rs +++ b/swap/src/common/tracing_util.rs @@ -1,6 +1,7 @@ use std::path::Path; use anyhow::Result; +use tracing::Level; use tracing_subscriber::filter::LevelFilter; use tracing_subscriber::fmt::time::UtcTime; use tracing_subscriber::layer::SubscriberExt; @@ -36,7 +37,8 @@ pub fn init( .with_ansi(false) .with_timer(UtcTime::rfc_3339()) .with_target(false) - .json(); + .json() + .with_filter(LevelFilter::from_level(Level::DEBUG)); // terminal logger let is_terminal = atty::is(atty::Stream::Stderr); diff --git a/swap/src/database/sqlite.rs b/swap/src/database/sqlite.rs index eb013f66..d35ffa4d 100644 --- a/swap/src/database/sqlite.rs +++ b/swap/src/database/sqlite.rs @@ -5,7 +5,7 @@ use anyhow::{anyhow, Context, Result}; use async_trait::async_trait; use libp2p::{Multiaddr, PeerId}; use sqlx::sqlite::{Sqlite, SqliteConnectOptions}; -use sqlx::{Pool, SqlitePool}; +use sqlx::{ConnectOptions, Pool, SqlitePool}; use std::collections::HashMap; use std::path::Path; use std::str::FromStr; @@ -26,7 +26,9 @@ impl SqliteDatabase { let read_only = matches!(access_mode, AccessMode::ReadOnly); let path_str = format!("sqlite:{}", path.as_ref().display()); - let options = SqliteConnectOptions::from_str(&path_str)?.read_only(read_only); + let mut options = SqliteConnectOptions::from_str(&path_str)? + .read_only(read_only); + options.disable_statement_logging(); let pool = SqlitePool::connect_with(options).await?; let mut sqlite = Self { pool };