use std::io::Write;
use std::sync::mpsc;
use std::sync::OnceLock;
const CHANNEL_CAPACITY: usize = 65_536;
static LOGGER: OnceLock<mpsc::SyncSender<String>> = OnceLock::new();
#[allow(dead_code)]
pub fn sender() -> Option<&'static mpsc::SyncSender<String>> {
Some(LOGGER.get_or_init(|| {
let (tx, rx) = mpsc::sync_channel(CHANNEL_CAPACITY);
std::thread::Builder::new()
.name("profile-log".into())
.spawn(move || {
for msg in rx {
let _ = writeln!(std::io::stderr(), "{msg}");
}
})
.expect("Failed to spawn profile log thread");
tx
}))
}
#[macro_export]
macro_rules! profile_log {
($($arg:tt)*) => {{
#[cfg(feature = "profile")]
{
if let Some(tx) = $crate::profile_log::sender() {
let _ = tx.try_send(format!($($arg)*));
}
}
}};
}