#[cfg(feature = "test-helpers")]
use std::sync::Arc;
#[cfg(feature = "test-helpers")]
use std::sync::Mutex;
#[cfg(feature = "test-helpers")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogLevel {
Error,
Warn,
Status,
Heartbeat,
Verbose,
Debug,
}
#[cfg(feature = "test-helpers")]
#[derive(Clone, Default)]
pub struct LogCapture {
inner: Arc<Mutex<Vec<(LogLevel, String)>>>,
}
#[cfg(feature = "test-helpers")]
impl LogCapture {
pub fn new() -> Self {
Self::default()
}
pub(crate) fn record(&self, level: LogLevel, msg: impl Into<String>) {
if let Ok(mut guard) = self.inner.lock() {
guard.push((level, msg.into()));
}
}
pub fn status_count(&self) -> usize {
self.count(LogLevel::Status)
}
pub fn debug_count(&self) -> usize {
self.count(LogLevel::Debug)
}
pub fn verbose_count(&self) -> usize {
self.count(LogLevel::Verbose)
}
pub fn warn_count(&self) -> usize {
self.count(LogLevel::Warn)
}
pub fn heartbeat_count(&self) -> usize {
self.count(LogLevel::Heartbeat)
}
pub fn error_count(&self) -> usize {
self.count(LogLevel::Error)
}
pub fn total_count(&self) -> usize {
self.inner.lock().map(|g| g.len()).unwrap_or(0)
}
fn count(&self, level: LogLevel) -> usize {
self.inner
.lock()
.map(|g| g.iter().filter(|(l, _)| *l == level).count())
.unwrap_or(0)
}
pub fn all_messages(&self) -> Vec<(LogLevel, String)> {
self.inner.lock().map(|g| g.clone()).unwrap_or_default()
}
pub fn warn_messages(&self) -> Vec<String> {
self.inner
.lock()
.map(|g| {
g.iter()
.filter(|(lvl, _)| *lvl == LogLevel::Warn)
.map(|(_, m)| m.clone())
.collect()
})
.unwrap_or_default()
}
}