use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
fn compute_enabled() -> bool {
match std::env::var("TSAFE_TUI_DEBUG") {
Ok(v) => {
let t = v.trim();
!t.is_empty() && t != "0" && !t.eq_ignore_ascii_case("false")
}
Err(_) => false,
}
}
pub fn enabled() -> bool {
compute_enabled()
}
pub fn log_file_path() -> PathBuf {
tsafe_core::profile::app_state_dir().join("tui-debug.log")
}
pub fn log(line: impl AsRef<str>) {
if !enabled() {
return;
}
let path = log_file_path();
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
let Ok(mut f) = OpenOptions::new().create(true).append(true).open(&path) else {
return;
};
let ts = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%S%.3fZ");
let _ = writeln!(f, "[{ts}] {}", line.as_ref());
let _ = f.flush();
}
pub fn announce_if_enabled() {
if enabled() {
eprintln!(
"tsafe TUI debug: appending to {} (unset TSAFE_TUI_DEBUG to disable)",
log_file_path().display()
);
}
}