Lines
80 %
Functions
85.71 %
Branches
100 %
//! Capturing what the tracing subscriber writes, for tests asserting log lines.
use std::sync::{Arc, Mutex};
/// A tracing sink that keeps what the subscriber wrote.
#[derive(Clone, Default)]
pub struct Captured(Arc<Mutex<Vec<u8>>>);
impl std::io::Write for Captured {
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
if let Ok(mut buf) = self.0.lock() {
buf.extend_from_slice(bytes);
}
Ok(bytes.len())
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
impl Captured {
pub fn error_lines(&self) -> usize {
let buf = self.0.lock().map(|buf| buf.clone()).unwrap_or_default();
String::from_utf8_lossy(&buf)
.lines()
.filter(|line| line.contains("ERROR"))
.count()
pub fn subscriber(&self) -> impl tracing::Subscriber + Send + Sync + 'static {
let writer = self.clone();
tracing_subscriber::fmt()
.with_writer(move || writer.clone())
.finish()