use std::sync::Once;
use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitExt};
static INIT: Once = Once::new();
pub fn init_test_logging() {
INIT.call_once(|| {
let filter = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("dcg=debug,destructive_command_guard=debug"));
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
.with_test_writer()
.with_ansi(true)
.with_level(true)
.with_target(true)
.with_file(true)
.with_line_number(true)
.compact(),
)
.with(filter)
.init();
});
}
#[macro_export]
macro_rules! test_log {
($($arg:tt)*) => {
tracing::info!(target: "test", $($arg)*)
};
}
#[macro_export]
macro_rules! test_debug {
($($arg:tt)*) => {
tracing::debug!(target: "test", $($arg)*)
};
}
#[macro_export]
macro_rules! test_warn {
($($arg:tt)*) => {
tracing::warn!(target: "test", $($arg)*)
};
}
#[macro_export]
macro_rules! test_error {
($($arg:tt)*) => {
tracing::error!(target: "test", $($arg)*)
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_init_logging_is_idempotent() {
init_test_logging();
init_test_logging();
init_test_logging();
}
}