use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Instant;
static ENABLED: AtomicBool = AtomicBool::new(false);
pub(crate) fn enable_from(flag: bool) {
let on = flag || std::env::var_os("RQ_LOG").is_some();
ENABLED.store(on, Ordering::Relaxed);
}
pub(crate) fn enabled() -> bool {
ENABLED.load(Ordering::Relaxed)
}
pub(crate) fn abbrev(path: &std::path::Path) -> String {
let s = path.display().to_string();
match std::env::var_os("HOME") {
Some(home) if !home.is_empty() => match s.strip_prefix(&*home.to_string_lossy()) {
Some(rest) => format!("~{rest}"),
None => s,
},
_ => s,
}
}
#[macro_export]
macro_rules! trace {
($($arg:tt)*) => {
if $crate::trace::enabled() {
eprintln!("rq: {}", format_args!($($arg)*));
}
};
}
pub(crate) struct Timer {
label: &'static str,
start: Option<Instant>,
}
impl Timer {
pub(crate) fn start(label: &'static str) -> Self {
Self {
label,
start: enabled().then(Instant::now),
}
}
}
impl Drop for Timer {
fn drop(&mut self) {
if let Some(start) = self.start {
eprintln!("rq: {} ({} ms)", self.label, start.elapsed().as_millis());
}
}
}