1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! What a unit test logs, for tests of warnings: `let (logs, _guard) =
//! crate::test_logs::capture();` then `logs.has(&[…])`. The subscriber is
//! this thread's (`#[tokio::test]` runs on one), so tests don't mix.
use std::sync::{Arc, Mutex};
/// The captured lines.
#[derive(Clone, Default)]
pub struct Logs(Arc<Mutex<Vec<u8>>>);
impl Logs {
/// Everything logged so far.
pub fn text(&self) -> String {
String::from_utf8_lossy(&self.0.lock().unwrap()).into_owned()
}
/// Whether a line holds every one of `parts`.
pub fn has(&self, parts: &[&str]) -> bool {
self.text()
.lines()
.any(|line| parts.iter().all(|part| line.contains(part)))
}
}
impl std::io::Write for Logs {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.0.lock().unwrap().extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for Logs {
type Writer = Logs;
fn make_writer(&'a self) -> Self::Writer {
self.clone()
}
}
/// Starts capturing this thread's logs (debug and up) until the guard drops.
pub fn capture() -> (Logs, tracing::subscriber::DefaultGuard) {
let logs = Logs::default();
let subscriber = tracing_subscriber::fmt()
.with_writer(logs.clone())
.with_ansi(false)
.with_max_level(tracing::Level::DEBUG)
.finish();
(logs, tracing::subscriber::set_default(subscriber))
}