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