Skip to main content

bitwarden_wasm_internal/
flight_recorder.rs

1//! WASM bindings for the Flight Recorder.
2
3use bitwarden_logging::{
4    FlightRecorderEvent, flight_recorder_count, read_flight_recorder, write_flight_recorder,
5};
6use wasm_bindgen::prelude::*;
7
8use crate::init::{LogLevel, convert_level};
9
10/// WASM client for reading Flight Recorder logs.
11///
12/// The underlying buffer is global (initialized in [`init_sdk`](crate::init_sdk)),
13/// so this client is a stateless handle for WASM access.
14#[wasm_bindgen]
15pub struct FlightRecorderClient;
16
17#[wasm_bindgen]
18impl FlightRecorderClient {
19    /// Create a new `FlightRecorderClient`.
20    #[wasm_bindgen(constructor)]
21    pub fn new() -> Self {
22        Self
23    }
24
25    /// Read all events currently in the Flight Recorder buffer.
26    pub fn read(&self) -> Vec<FlightRecorderEvent> {
27        read_flight_recorder()
28    }
29
30    /// Get the current event count without reading event contents.
31    pub fn count(&self) -> usize {
32        flight_recorder_count()
33    }
34
35    /// Ingest a single TypeScript-originated log event into the global buffer,
36    /// honoring the configured level floor.
37    ///
38    /// `timestamp` is milliseconds since the Unix epoch, supplied by the caller
39    /// (`Date.now()`).
40    pub fn write(&self, timestamp: f64, level: LogLevel, target: String, message: String) {
41        let level = convert_level(level);
42        let event = FlightRecorderEvent {
43            timestamp: timestamp as i64,
44            level: level.to_string(),
45            target,
46            message,
47            fields: Default::default(),
48        };
49        write_flight_recorder(event, level);
50    }
51}
52
53impl Default for FlightRecorderClient {
54    fn default() -> Self {
55        Self::new()
56    }
57}