From 2eb7fab0c32008be8b8a51f49aabfe89ba0ef0db Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 8 Jul 2021 12:28:04 +1000 Subject: [PATCH] Make `capture_logs` available for the whole crate --- swap/src/bin/swap.rs | 78 ++++++----------------------------------- swap/src/lib.rs | 1 + swap/src/tracing_ext.rs | 65 ++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 68 deletions(-) create mode 100644 swap/src/tracing_ext.rs diff --git a/swap/src/bin/swap.rs b/swap/src/bin/swap.rs index 1e591f0d..29955ba1 100644 --- a/swap/src/bin/swap.rs +++ b/swap/src/bin/swap.rs @@ -484,14 +484,13 @@ mod tests { use super::*; use crate::determine_btc_to_swap; use ::bitcoin::Amount; - use std::io; use std::sync::Mutex; - use tracing::subscriber; - use tracing_subscriber::fmt::MakeWriter; + use swap::tracing_ext::capture_logs; + use tracing::level_filters::LevelFilter; #[tokio::test] async fn given_no_balance_and_transfers_less_than_max_swaps_max_giveable() { - let writer = capture_logs(); + let writer = capture_logs(LevelFilter::INFO); let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![ Amount::ZERO, Amount::from_btc(0.0009).unwrap(), @@ -526,7 +525,7 @@ mod tests { #[tokio::test] async fn given_no_balance_and_transfers_more_then_swaps_max_quantity_from_quote() { - let writer = capture_logs(); + let writer = capture_logs(LevelFilter::INFO); let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![ Amount::ZERO, Amount::from_btc(0.1).unwrap(), @@ -561,7 +560,7 @@ mod tests { #[tokio::test] async fn given_initial_balance_below_max_quantity_swaps_max_givable() { - let writer = capture_logs(); + let writer = capture_logs(LevelFilter::INFO); let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![ Amount::from_btc(0.0049).unwrap(), Amount::from_btc(99.9).unwrap(), @@ -594,7 +593,7 @@ mod tests { #[tokio::test] async fn given_initial_balance_above_max_quantity_swaps_max_quantity() { - let writer = capture_logs(); + let writer = capture_logs(LevelFilter::INFO); let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![ Amount::from_btc(0.1).unwrap(), Amount::from_btc(99.9).unwrap(), @@ -627,7 +626,7 @@ mod tests { #[tokio::test] async fn given_no_initial_balance_then_min_wait_for_sufficient_deposit() { - let writer = capture_logs(); + let writer = capture_logs(LevelFilter::INFO); let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![ Amount::ZERO, Amount::from_btc(0.01).unwrap(), @@ -662,7 +661,7 @@ mod tests { #[tokio::test] async fn given_balance_less_then_min_wait_for_sufficient_deposit() { - let writer = capture_logs(); + let writer = capture_logs(LevelFilter::INFO); let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![ Amount::from_btc(0.0001).unwrap(), Amount::from_btc(0.01).unwrap(), @@ -697,7 +696,7 @@ mod tests { #[tokio::test] async fn given_no_initial_balance_and_transfers_less_than_min_keep_waiting() { - let writer = capture_logs(); + let writer = capture_logs(LevelFilter::INFO); let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![ Amount::ZERO, Amount::from_btc(0.01).unwrap(), @@ -737,7 +736,7 @@ mod tests { #[tokio::test] async fn given_longer_delay_until_deposit_should_not_spam_user() { - let writer = capture_logs(); + let writer = capture_logs(LevelFilter::INFO); let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![ Amount::ZERO, Amount::ZERO, @@ -779,63 +778,6 @@ mod tests { ); } - /// Setup tracing with a capturing writer, allowing assertions on the log - /// messages. - /// - /// Time and ANSI are disabled to make the output more predictable and - /// readable. - fn capture_logs() -> MakeCapturingWriter { - let make_writer = MakeCapturingWriter::default(); - - let guard = subscriber::set_default( - tracing_subscriber::fmt() - .with_ansi(false) - .without_time() - .with_writer(make_writer.clone()) - .finish(), - ); - // don't clean up guard we stay initialized - std::mem::forget(guard); - - make_writer - } - - #[derive(Default, Clone)] - struct MakeCapturingWriter { - writer: CapturingWriter, - } - - impl MakeCapturingWriter { - fn captured(&self) -> String { - let captured = &self.writer.captured; - let cursor = captured.lock().unwrap(); - String::from_utf8(cursor.clone().into_inner()).unwrap() - } - } - - impl MakeWriter for MakeCapturingWriter { - type Writer = CapturingWriter; - - fn make_writer(&self) -> Self::Writer { - self.writer.clone() - } - } - - #[derive(Default, Clone)] - struct CapturingWriter { - captured: Arc>>>, - } - - impl io::Write for CapturingWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.captured.lock().unwrap().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - Ok(()) - } - } - struct MaxGiveable { amounts: Vec, call_counter: usize, diff --git a/swap/src/lib.rs b/swap/src/lib.rs index 9308b571..64c8ffe9 100644 --- a/swap/src/lib.rs +++ b/swap/src/lib.rs @@ -29,5 +29,6 @@ pub mod network; pub mod protocol; pub mod seed; pub mod tor; +pub mod tracing_ext; mod monero_ext; diff --git a/swap/src/tracing_ext.rs b/swap/src/tracing_ext.rs new file mode 100644 index 00000000..912187fd --- /dev/null +++ b/swap/src/tracing_ext.rs @@ -0,0 +1,65 @@ +#![allow(clippy::unwrap_used)] // This is only meant to be used in tests. + +use std::io; +use std::sync::{Arc, Mutex}; +use tracing::subscriber; +use tracing_subscriber::filter::LevelFilter; +use tracing_subscriber::fmt::MakeWriter; + +/// Setup tracing with a capturing writer, allowing assertions on the log +/// messages. +/// +/// Time and ANSI are disabled to make the output more predictable and +/// readable. +pub fn capture_logs(min_level: LevelFilter) -> MakeCapturingWriter { + let make_writer = MakeCapturingWriter::default(); + + let guard = subscriber::set_default( + tracing_subscriber::fmt() + .with_ansi(false) + .without_time() + .with_writer(make_writer.clone()) + .with_env_filter(format!("{}", min_level)) + .finish(), + ); + // don't clean up guard we stay initialized + std::mem::forget(guard); + + make_writer +} + +#[derive(Default, Clone)] +pub struct MakeCapturingWriter { + writer: CapturingWriter, +} + +impl MakeCapturingWriter { + pub fn captured(&self) -> String { + let captured = &self.writer.captured; + let cursor = captured.lock().unwrap(); + String::from_utf8(cursor.clone().into_inner()).unwrap() + } +} + +impl MakeWriter for MakeCapturingWriter { + type Writer = CapturingWriter; + + fn make_writer(&self) -> Self::Writer { + self.writer.clone() + } +} + +#[derive(Default, Clone)] +pub struct CapturingWriter { + captured: Arc>>>, +} + +impl io::Write for CapturingWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.captured.lock().unwrap().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +}