Skip to main content

xil_web/
activity.rs

1//! The optional session activity log (`xil gui --output FILE`).
2
3use std::fs::{File, OpenOptions};
4use std::io::Write;
5use std::path::Path;
6use std::sync::Mutex;
7
8static LOG: Mutex<Option<File>> = Mutex::new(None);
9
10/// Open (append) the activity log. Later calls replace the file.
11pub fn open(path: &Path) -> std::io::Result<()> {
12    let f = OpenOptions::new().append(true).create(true).open(path)?;
13    if let Ok(mut g) = LOG.lock() {
14        *g = Some(f);
15    }
16    Ok(())
17}
18
19/// `_log_activity(msg)`: one `[YYYY-MM-DDTHH:MM:SS] msg` line, local time.
20/// A no-op when no log is open.
21pub fn log(msg: &str) {
22    if let Ok(mut g) = LOG.lock() {
23        if let Some(f) = g.as_mut() {
24            let ts = chrono::Local::now().format("%Y-%m-%dT%H:%M:%S");
25            let _ = writeln!(f, "[{ts}] {msg}");
26            let _ = f.flush();
27        }
28    }
29}