bitwarden_logging/
sink.rs1use std::sync::{Arc, RwLock};
4
5use crate::{CircularBuffer, FlightRecorderEvent};
6
7pub trait FlightRecorderSink: Send + Sync {
12 fn push(&self, event: FlightRecorderEvent);
14}
15
16pub struct LocalBufferSink {
18 buffer: Arc<CircularBuffer<FlightRecorderEvent>>,
19}
20
21impl LocalBufferSink {
22 pub fn new(buffer: Arc<CircularBuffer<FlightRecorderEvent>>) -> Self {
24 Self { buffer }
25 }
26}
27
28impl FlightRecorderSink for LocalBufferSink {
29 fn push(&self, event: FlightRecorderEvent) {
30 self.buffer.push(event);
31 }
32}
33
34#[derive(Clone)]
36pub(crate) struct SinkSlot(Arc<RwLock<Arc<dyn FlightRecorderSink>>>);
37
38impl SinkSlot {
39 pub(crate) fn new(sink: Arc<dyn FlightRecorderSink>) -> Self {
40 Self(Arc::new(RwLock::new(sink)))
41 }
42
43 pub(crate) fn push(&self, event: FlightRecorderEvent) {
44 self.0
45 .read()
46 .expect("FlightRecorder sink lock poisoned")
47 .push(event);
48 }
49
50 pub(crate) fn set(&self, sink: Arc<dyn FlightRecorderSink>) {
51 *self.0.write().expect("FlightRecorder sink lock poisoned") = sink;
52 }
53}
54
55impl std::fmt::Debug for SinkSlot {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 f.debug_struct("SinkSlot").finish_non_exhaustive()
58 }
59}