Download crates/openwa-debugui/src/log.rs from nmarshall/wormz-a: direct link, hf CLI and curl.
- Browser
- Download file 1.08 kB
-
https://huggingface.co/nmarshall/wormz-a/resolve/main/crates/openwa-debugui/src/log.rs
- Command line
-
hf download hf://nmarshall/wormz-a/crates/openwa-debugui/src/log.rs
-
curl -L -o log.rs https://huggingface.co/nmarshall/wormz-a/resolve/main/crates/openwa-debugui/src/log.rs
1.08 kB
| use std::collections::VecDeque; | |
| use std::sync::{Mutex, OnceLock}; | |
| use std::time::Instant; | |
| pub struct LogEntry { | |
| pub ts: Instant, | |
| pub text: String, | |
| } | |
| static LOG_BUF: OnceLock<Mutex<VecDeque<LogEntry>>> = OnceLock::new(); | |
| fn buf() -> &'static Mutex<VecDeque<LogEntry>> { | |
| LOG_BUF.get_or_init(|| Mutex::new(VecDeque::with_capacity(500))) | |
| } | |
| /// Push a message to the debug log ring buffer (max 500 entries). | |
| pub fn push(msg: impl Into<String>) { | |
| if let Ok(mut b) = buf().lock() { | |
| if b.len() >= 500 { | |
| b.pop_front(); | |
| } | |
| b.push_back(LogEntry { | |
| ts: Instant::now(), | |
| text: msg.into(), | |
| }); | |
| } | |
| } | |
| /// Snapshot up to `max` of the most recent entries (cloned). | |
| pub fn snapshot(max: usize) -> Vec<(Instant, String)> { | |
| let Ok(b) = buf().lock() else { | |
| return Vec::new(); | |
| }; | |
| let start = b.len().saturating_sub(max); | |
| b.range(start..).map(|e| (e.ts, e.text.clone())).collect() | |
| } | |
| /// Clear all log entries. | |
| pub fn clear() { | |
| if let Ok(mut b) = buf().lock() { | |
| b.clear(); | |
| } | |
| } | |