bitwarden_wasm_internal/
flight_recorder.rs1use bitwarden_ipc::{Endpoint, SubscribeError, wasm::JsIpcClient};
4use bitwarden_logging::{
5 FlightRecorderEvent, flight_recorder_count,
6 ipc::{FlightRecorderCollectorRole, read_flight_recorder_aggregated},
7 write_flight_recorder,
8};
9use bitwarden_threading::cancellation_token::wasm::{AbortSignal, AbortSignalExt};
10
11use crate::init::{LogLevel, convert_level};
12
13#[bitwarden_ffi::wasm_object]
18pub struct FlightRecorderClient;
19
20#[bitwarden_ffi::wasm_export]
21impl FlightRecorderClient {
22 #[wasm_bindgen(constructor)]
24 pub fn new() -> Self {
25 Self
26 }
27
28 pub async fn read(&self, abort_signal: Option<AbortSignal>) -> Vec<FlightRecorderEvent> {
35 read_flight_recorder_aggregated(abort_signal.map(|s| s.to_cancellation_token())).await
36 }
37
38 pub fn count(&self) -> usize {
40 flight_recorder_count()
41 }
42
43 pub fn write(&self, timestamp: f64, level: LogLevel, target: String, message: String) {
49 let level = convert_level(level);
50 let event = FlightRecorderEvent {
51 timestamp: timestamp as i64,
52 level: level.to_string(),
53 target,
54 message,
55 fields: Default::default(),
56 process: None,
57 };
58 write_flight_recorder(event, level);
59 }
60}
61
62#[bitwarden_ffi::wasm_export(js_name = enableFlightRecorderForwarding)]
66pub fn enable_flight_recorder_forwarding(ipc_client: &JsIpcClient, destination: Endpoint) {
67 bitwarden_logging::ipc::enable_flight_recorder_forwarding(
68 ipc_client.client.clone(),
69 destination,
70 );
71}
72
73#[bitwarden_ffi::wasm_export(js_name = enableFlightRecorderCollection)]
76pub async fn enable_flight_recorder_collection(
77 ipc_client: &JsIpcClient,
78 role: FlightRecorderCollectorRole,
79) -> Result<(), SubscribeError> {
80 bitwarden_logging::ipc::enable_flight_recorder_collection(ipc_client.client.clone(), role).await
81}
82
83impl Default for FlightRecorderClient {
84 fn default() -> Self {
85 Self::new()
86 }
87}