1#![deny(missing_docs)]
2#![doc = r#"
3# `wasm-tracing`
4
5Leverages tracing to proilfe wasm performance via `console`.
6
7## Usage
8
9For the simplest out of the box set-up, you can simply set `wasm_tracing` 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 wasm_tracing::set_as_global_default();
24
25 Ok(())
26}
27```
28"#]
29
30use tracing::dispatcher::SetGlobalDefaultError;
31use tracing_subscriber::layer::*;
32use tracing_subscriber::registry::*;
33
34use wasm_bindgen::prelude::*;
35
36#[doc(hidden)]
37mod config;
38pub use config::*;
39
40#[doc(hidden)]
41mod layer;
42pub use layer::*;
43pub(crate) mod recorder;
44pub mod prelude {
46 pub use super::{
47 config::{ConsoleConfig, WasmLayerConfig},
48 layer::WasmLayer,
49 };
50}
51
52#[wasm_bindgen]
53extern "C" {
54 #[wasm_bindgen(js_namespace = performance)]
55 fn mark(a: &str);
56 #[wasm_bindgen(catch, js_namespace = performance)]
57 fn measure(name: String, startMark: String) -> Result<(), JsValue>;
58 #[wasm_bindgen(js_namespace = console, js_name = log)]
59 fn log1(message: String);
60 #[wasm_bindgen(js_namespace = console, js_name = log)]
61 fn log2(message1: &str, message2: &str);
62 #[wasm_bindgen(js_namespace = console, js_name = log)]
63 fn log3(message1: &str, message2: &str, message3: &str);
64 #[wasm_bindgen(js_namespace = console, js_name = log)]
65 fn log4(message1: String, message2: &str, message3: &str, message4: &str);
66}
67
68#[cfg(not(feature = "mark-with-rayon-thread-index"))]
69#[inline]
70fn thread_display_suffix() -> &'static str {
71 ""
72}
73#[cfg(feature = "mark-with-rayon-thread-index")]
74fn thread_display_suffix() -> String {
75 let mut message = " #".to_string();
76 match rayon::current_thread_index() {
77 Some(idx) => message.push_str(&format!("{idx}")),
78 None => message.push_str("main"),
79 }
80 message
81}
82
83#[cfg(not(feature = "mark-with-rayon-thread-index"))]
84fn mark_name(id: &tracing::Id) -> String {
85 format!("t{:x}", id.into_u64())
86}
87#[cfg(feature = "mark-with-rayon-thread-index")]
88fn mark_name(id: &tracing::Id) -> String {
89 format!(
90 "t{:x}-{}",
91 id.into_u64(),
92 rayon::current_thread_index().unwrap_or(999)
93 )
94}
95
96#[doc = r#"
97 Set the global default recorder with [tracing::subscriber::set_global_default]. Panics if the [WasmLayer] cannot be constructed.
98
99 Panics if a global default is already set.
100"#]
101pub fn set_as_global_default() {
102 tracing::subscriber::set_global_default(
103 Registry::default().with(WasmLayer::new(WasmLayerConfig::default())),
104 )
105 .expect("default global");
106}
107
108#[doc = r#"
109Set WASM to be the default layer for a [Registry] via [tracing::subscriber::set_global_default].
110
111
112## Example
113
114```rust
115use console_error_panic_hook;
116use wasm_bindgen::prelude::*;
117
118#[wasm_bindgen(start)]
119pub fn start() -> Result<(), JsValue> {
120 console_error_panic_hook::set_once();
121
122 wasm_tracing::try_set_as_global_default();
123
124 Ok(())
125}
126```
127"#]
128pub fn try_set_as_global_default() -> Result<(), SetGlobalDefaultError> {
129 tracing::subscriber::set_global_default(
130 Registry::default().with(WasmLayer::new(WasmLayerConfig::default())),
131 )
132}
133
134#[doc = r#"
135Given a [`WasmLayerConfig`], set WASM to be the default layer for a [Registry].
136
137## Example
138
139```rust
140use console_error_panic_hook;
141use wasm_bindgen::prelude::*;
142use wasm_tracing::prelude::*;
143use tracing::Level;
144
145#[wasm_bindgen(start)]
146pub fn start() -> Result<(), JsValue> {
147 console_error_panic_hook::set_once();
148
149 let config = WasmLayerConfig::new().set_report_logs_in_timings(true).set_max_level(Level::ERROR).to_owned();
150
151 let _ = wasm_tracing::set_as_global_default_with_config(config);
152
153 Ok(())
154}
155```
156"#]
157pub fn set_as_global_default_with_config(
158 config: WasmLayerConfig,
159) -> Result<(), SetGlobalDefaultError> {
160 tracing::subscriber::set_global_default(Registry::default().with(WasmLayer::new(config)))
161}