use std::sync::{Mutex, Once, OnceLock};
pub use crate::subscriber::{get_subscriber, MockWriter};
pub static INITIALIZED: Once = Once::new();
#[doc(hidden)]
pub fn global_buf() -> &'static Mutex<Vec<u8>> {
static GLOBAL_BUF: OnceLock<Mutex<Vec<u8>>> = OnceLock::new();
GLOBAL_BUF.get_or_init(|| Mutex::new(vec![]))
}
pub fn logs_with_scope_contain(scope: &str, val: &str) -> bool {
let logs = String::from_utf8(global_buf().lock().unwrap().to_vec()).unwrap();
for line in logs.split('\n') {
if line.contains(&format!(" {}:", scope)) && line.contains(val) {
return true;
}
}
false
}
pub fn logs_assert<F>(scope: &str, f: F) -> std::result::Result<(), String>
where
F: Fn(&[&str]) -> std::result::Result<(), String>,
{
let buf = global_buf().lock().unwrap();
let logs: Vec<&str> = std::str::from_utf8(&buf)
.expect("Logs contain invalid UTF8")
.lines()
.filter(|line| line.contains(&format!(" {}:", scope)))
.collect();
f(&logs)
}