From fffab9c3bfe39a7f8205e9ed6df3a5d8a9626114 Mon Sep 17 00:00:00 2001 From: Ultraxime Date: Wed, 31 Jul 2024 19:51:20 +0200 Subject: [PATCH] Allowing cargo test to work --- README.md | 11 ++++++++++- src/lib.rs | 4 +++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b326ce6..af672f9 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,15 @@ **log_tester** is a crate that takes care of capturing log messages produced by the [`log`](https://docs.rs/log) crate during test, and then perform checks on them. +## Warnings + +This crate is made to capture all logs, including in multithreaded case. +Hence, it does not work well with `cargo test`, the logs from all test will be captured. + +It is better to use `cargo nextest` instead. + +Using `cargo test` will not fail but additional logs will be captured. In that way the test may not be right. + ## Usage This crate is intend to be used in conjunction with the [`log`](https://docs.rs/log) @@ -19,7 +28,7 @@ log = "0.4" log_tester = "0.1" ``` -```rust, ignore +```rust use log_tester::LogTester; use log::Level; diff --git a/src/lib.rs b/src/lib.rs index 3c52f81..f70720f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,6 +23,8 @@ use log::{Level, Log, Record}; /// The list of captured logs. static LOGS: RwLock> = RwLock::new(Vec::new()); +static INIT: std::sync::Once = std::sync::Once::new(); + /// The logger pub struct LogTester; @@ -49,7 +51,7 @@ impl LogTester { /// LogTester::start(); /// ``` pub fn start() { - log::set_logger(&LogTester).expect("Failed to start the logger"); + INIT.call_once(|| log::set_logger(&LogTester).expect("Failed to start the logger")); log::set_max_level(log::LevelFilter::Trace); }