use crate::util::UnwrapPoison;
static PRE_TRACING_DIAGNOSTICS: std::sync::Mutex<Vec<String>> = std::sync::Mutex::new(Vec::new());
static TRACING_INITIALIZED: std::sync::atomic::AtomicBool =
std::sync::atomic::AtomicBool::new(false);
pub(crate) fn timestamped_stderr(message: &str) {
let ts = chrono::Local::now().format("%Y-%m-%d %H:%M:%S");
eprintln!("[mahbot] [{ts}] {message}");
}
pub(crate) fn boot_diagnostic(message: String) {
timestamped_stderr(&message);
if TRACING_INITIALIZED.load(std::sync::atomic::Ordering::Acquire) {
tracing::warn!("{message}");
} else {
PRE_TRACING_DIAGNOSTICS.lock().unwrap_poison().push(message);
}
}
pub(crate) fn mark_tracing_initialized() {
TRACING_INITIALIZED.store(true, std::sync::atomic::Ordering::Release);
}
pub(crate) fn clear_boot_diagnostics() {
PRE_TRACING_DIAGNOSTICS.lock().unwrap_poison().clear();
}
pub(crate) fn replay_boot_diagnostics() {
let messages = {
let mut buf = PRE_TRACING_DIAGNOSTICS.lock().unwrap_poison();
std::mem::take(&mut *buf)
};
for m in messages {
tracing::warn!("{m}");
}
}