46 lines
1.4 KiB
Rust
46 lines
1.4 KiB
Rust
//! In-app activity log (round 15 #13). A small, process-global ring buffer of
|
|
//! timestamped messages -- scans starting/finishing, operations applied, errors --
|
|
//! shown in the Settings > Logs page. Thread-safe so background workers can append.
|
|
|
|
use std::sync::Mutex;
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
const MAX_ENTRIES: usize = 2000;
|
|
|
|
static LOG: Mutex<Vec<String>> = Mutex::new(Vec::new());
|
|
|
|
/// Local wall-clock HH:MM:SS for a log line. Uses the system clock offset from
|
|
/// UTC via the TZ-less epoch; good enough for an activity log timestamp.
|
|
fn stamp() -> String {
|
|
let secs = SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.map(|d| d.as_secs())
|
|
.unwrap_or(0);
|
|
let t = secs % 86_400;
|
|
format!("{:02}:{:02}:{:02}", t / 3600, (t % 3600) / 60, t % 60)
|
|
}
|
|
|
|
/// Append a message to the activity log (timestamped).
|
|
pub fn log(msg: impl Into<String>) {
|
|
let line = format!("{} {}", stamp(), msg.into());
|
|
if let Ok(mut v) = LOG.lock() {
|
|
v.push(line);
|
|
let len = v.len();
|
|
if len > MAX_ENTRIES {
|
|
v.drain(0..len - MAX_ENTRIES);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The whole log as one string, newest last (for the Logs page text box).
|
|
pub fn dump() -> String {
|
|
LOG.lock().map(|v| v.join("\n")).unwrap_or_default()
|
|
}
|
|
|
|
/// Clear the log.
|
|
pub fn clear() {
|
|
if let Ok(mut v) = LOG.lock() {
|
|
v.clear();
|
|
}
|
|
}
|