pub struct LogBuffer { /* private fields */ }Expand description
Thread-safe, bounded ring buffer of formatted log lines.
Why: shared between the tracing Layer (writer) and the HTTP handler
(reader); both hold cheap Arc clones of the same underlying deque.
What: wraps Arc<Mutex<VecDeque<String>>>. push appends and evicts the
oldest line past capacity; tail snapshots the most recent N lines.
Test: capacity_evicts_oldest, tail_returns_last_n.
Implementations§
Source§impl LogBuffer
impl LogBuffer
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Create an empty buffer with the given line capacity.
Why: callers (daemon startup) choose the cap; tests use a tiny one.
What: allocates a VecDeque with capacity.max(1) reserved slots so a
zero capacity is treated as one (a zero-cap ring is useless and
would panic on the eviction arithmetic).
Test: capacity_evicts_oldest.
Sourcepub fn push(&self, line: String)
pub fn push(&self, line: String)
Append a line, evicting the oldest entry when at capacity.
Why: a tracing Layer calls this on every event; it must never panic
or block long. A poisoned mutex (a prior panic while logging) is
recovered via into_inner so logging itself never cascades a
panic into the daemon.
What: pushes line to the back; if length now exceeds capacity,
pops the front.
Test: capacity_evicts_oldest.
Sourcepub fn tail(&self, n: usize) -> Vec<String>
pub fn tail(&self, n: usize) -> Vec<String>
Snapshot the most recent n lines (or all, when n exceeds the
current length).
Why: the /logs/tail handler returns these as a JSON array. Cloning
under the lock keeps the critical section short and lets the
caller serialise without holding the mutex.
What: returns a Vec<String> of at most n lines, oldest-first.
Test: tail_returns_last_n, tail_all_when_n_exceeds_len.