veilid_tracing_wasm/
lib.rs1#![deny(missing_docs)]
2#![doc = r#"
3# `wasm-tracing`
4
5Leverages tracing to profile wasm performance via `console`.
6
7## Usage
8
9For the simplest out of the box set-up, you can simply set `veilid_tracing_wasm` as your default tracing Subscriber in wasm_bindgen(start)
10
11We have this declared in our `./src/lib.rs`
12
13```rust
14use console_error_panic_hook;
15use wasm_bindgen::prelude::*;
16
17#[wasm_bindgen(start)]
18pub fn start() -> Result<(), JsValue> {
19 // print pretty errors in wasm https://github.com/rustwasm/console_error_panic_hook
20 // This is not needed for tracing_wasm to work, but it is a common tool for getting proper error line numbers for panics.
21 console_error_panic_hook::set_once();
22
23 veilid_tracing_wasm::set_as_global_default();
24
25 Ok(())
26}
27```
28
29## Features
30
31| Feature | Function |
32| ------------ | -------- |
33| `mark-with-rayon-thread-index` | Logs the rayon worker thread's index within its current pool. |
34"#]
35
36use tracing::dispatcher::SetGlobalDefaultError;
37use tracing_subscriber::layer::*;
38use tracing_subscriber::registry::*;
39
40use wasm_bindgen::prelude::*;
41
42#[doc(hidden)]
43mod config;
44pub use config::*;
45
46#[doc(hidden)]
47mod layer;
48pub use layer::*;
49pub(crate) mod recorder;
50pub mod prelude {
52 pub use super::{config::WasmLayerConfig, layer::WasmLayer, recorder::WasmFieldFilter};
53}
54
55#[wasm_bindgen]
56extern "C" {
57 #[wasm_bindgen(js_namespace = Date, js_name = now)]
58 fn date_now() -> f64;
59 #[wasm_bindgen(js_namespace = performance)]
60 fn mark(name: &str);
61 #[wasm_bindgen(catch, js_namespace = performance)]
62 fn measure(name: String, startMark: String) -> Result<(), JsValue>;
63 #[wasm_bindgen(js_namespace = console, js_name = log)]
64 fn log1(message: String);
65 #[wasm_bindgen(js_namespace = console, js_name = log)]
66 fn log4(message1: String, message2: &str, message3: &str, message4: &str);
67 #[wasm_bindgen(js_namespace = console, js_name = debug)]
68 fn debug1(message: String);
69 #[wasm_bindgen(js_namespace = console, js_name = debug)]
70 fn debug4(message1: String, message2: &str, message3: &str, message4: &str);
71 #[wasm_bindgen(js_namespace = console, js_name = warn)]
72 fn warn1(message: String);
73 #[wasm_bindgen(js_namespace = console, js_name = warn)]
74 fn warn4(message1: String, message2: &str, message3: &str, message4: &str);
75 #[wasm_bindgen(js_namespace = console, js_name = error)]
76 fn error1(message: String);
77 #[wasm_bindgen(js_namespace = console, js_name = error)]
78 fn error4(message1: String, message2: &str, message3: &str, message4: &str);
79}
80
81#[cfg(not(feature = "mark-with-rayon-thread-index"))]
82#[inline]
83fn thread_display_suffix() -> &'static str {
84 ""
85}
86#[cfg(feature = "mark-with-rayon-thread-index")]
87fn thread_display_suffix() -> String {
88 let mut message = " #".to_string();
89 match rayon::current_thread_index() {
90 Some(idx) => message.push_str(&format!("{idx}")),
91 None => message.push_str("main"),
92 }
93 message
94}
95
96#[cfg(not(feature = "mark-with-rayon-thread-index"))]
97fn mark_name(id: &tracing::Id) -> String {
98 format!("t{:x}", id.into_u64())
99}
100#[cfg(feature = "mark-with-rayon-thread-index")]
101fn mark_name(id: &tracing::Id) -> String {
102 format!(
103 "t{:x}-{}",
104 id.into_u64(),
105 rayon::current_thread_index().unwrap_or(999)
106 )
107}
108
109#[doc = r#"
110Set the global default recorder with [tracing::subscriber::set_global_default]. Panics if the [WasmLayer] cannot be constructed.
111
112Panics if a global default is already set.
113
114## NOTE
115
116It is discouraged by `tracing` for libraries (such at this one) to call [`tracing::subscriber::set_global_default`].
117This function does so, as it is a convenience. If you are a library, please follow the advice of `tracing`.
118
119If you would like to use multiple layers, use this code:
120```rust
121use tracing_subscriber::layer::*;
122use tracing_subscriber::registry::*;
123use veilid_tracing_wasm::prelude::*;
124
125tracing::subscriber::set_global_default(Registry::default().with(WasmLayer::default())).unwrap();
126"#]
127pub fn set_as_global_default() {
128 tracing::subscriber::set_global_default(
129 Registry::default().with(WasmLayer::new(WasmLayerConfig::default())),
130 )
131 .expect("default global");
132}
133
134#[doc = r#"
135Set WASM to be the default layer for a [Registry] via [tracing::subscriber::set_global_default].
136
137
138## Example
139
140```rust
141use console_error_panic_hook;
142use wasm_bindgen::prelude::*;
143
144#[wasm_bindgen(start)]
145pub fn start() -> Result<(), JsValue> {
146 console_error_panic_hook::set_once();
147
148 veilid_tracing_wasm::try_set_as_global_default();
149
150 Ok(())
151}
152```
153
154## NOTE
155
156It is discouraged by `tracing` for libraries (such at this one) to call [`tracing::subscriber::set_global_default`].
157This function does so, as it is a convenience. If you are a library, please follow the advice of `tracing`.
158
159If you would like to use multiple layers, use this code:
160```rust
161use tracing_subscriber::layer::*;
162use tracing_subscriber::registry::*;
163use veilid_tracing_wasm::prelude::*;
164
165tracing::subscriber::set_global_default(Registry::default().with(WasmLayer::default())).unwrap();
166"#]
167pub fn try_set_as_global_default() -> Result<(), SetGlobalDefaultError> {
168 tracing::subscriber::set_global_default(
169 Registry::default().with(WasmLayer::new(WasmLayerConfig::default())),
170 )
171}
172
173#[doc = r#"
174Given a [`WasmLayerConfig`], set WASM to be the default layer for a [Registry].
175
176
177## Example
178
179```rust
180use console_error_panic_hook;
181use wasm_bindgen::prelude::*;
182use veilid_tracing_wasm::prelude::*;
183use tracing::Level;
184
185#[wasm_bindgen(start)]
186pub fn start() -> Result<(), JsValue> {
187 console_error_panic_hook::set_once();
188
189 let config = WasmLayerConfig::new().remove_timings().with_max_level(Level::ERROR);
190
191 let _ = veilid_tracing_wasm::set_as_global_default_with_config(config);
192
193 Ok(())
194}
195```
196
197## NOTE
198
199It is discouraged by `tracing` for libraries (such at this one) to call [`tracing::subscriber::set_global_default`].
200This function does so, as it is a convenience. If you are a library, please follow the advice of `tracing`.
201
202If you would like to use multiple layers, use this code:
203```rust
204use tracing_subscriber::layer::*;
205use tracing_subscriber::registry::*;
206use veilid_tracing_wasm::prelude::*;
207
208tracing::subscriber::set_global_default(Registry::default().with(WasmLayer::default())).unwrap();
209```
210"#]
211pub fn set_as_global_default_with_config(
212 config: WasmLayerConfig,
213) -> Result<(), SetGlobalDefaultError> {
214 tracing::subscriber::set_global_default(Registry::default().with(WasmLayer::new(config)))
215}