use std::sync::{Mutex, Once};
use lazy_static::lazy_static;
pub use crate::subscriber::{get_subscriber, MockWriter};
pub static INITIALIZED: Once = Once::new();
lazy_static! {
#[doc(hidden)]
pub static ref GLOBAL_BUF: Mutex<Vec<u8>> = 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)
}