File size: 1,082 Bytes
229ceef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04e939e
 
 
 
229ceef
 
 
 
 
04e939e
 
 
229ceef
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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();
    }
}