wasm_tracing/config/
mod.rs

1#[doc(hidden)]
2mod console;
3pub use console::*;
4
5///Configuration parameters for the [WasmLayer](crate::prelude::WasmLayer).
6#[derive(Debug, PartialEq, Eq, Clone)]
7pub struct WasmLayerConfig {
8    /// In dev-tools, report timings of traces
9    pub report_logs_in_timings: bool,
10    /// See [ConsoleConfig]
11    pub console: ConsoleConfig,
12    /// Maximum log level
13    pub max_level: tracing::Level,
14    /// Show/hide fields of types
15    pub show_fields: bool,
16    /// Show origin (line number, source)
17    pub show_origin: bool,
18    /// Optional URL to prepend to origins. E.g. to allow for showing full file paths that can be navigated when logged in the browser console.
19    pub origin_base_url: Option<String>,
20}
21
22impl Default for WasmLayerConfig {
23    fn default() -> Self {
24        WasmLayerConfig {
25            report_logs_in_timings: true,
26            console: ConsoleConfig::ReportWithConsoleColor,
27            max_level: tracing::Level::TRACE,
28            show_fields: true,
29            show_origin: true,
30            origin_base_url: None,
31        }
32    }
33}
34
35impl WasmLayerConfig {
36    /// Create a default [WasmLayerConfig]
37    pub fn new() -> WasmLayerConfig {
38        WasmLayerConfig::default()
39    }
40
41    /// Set whether events should appear in performance Timings
42    pub fn set_report_logs_in_timings(&mut self, report_logs_in_timings: bool) -> &mut Self {
43        self.report_logs_in_timings = report_logs_in_timings;
44        self
45    }
46
47    /// Set the maximal level on which events should be displayed
48    pub fn set_max_level(&mut self, max_level: tracing::Level) -> &mut Self {
49        self.max_level = max_level;
50        self
51    }
52
53    /// Set if and how events should be displayed in the browser console
54    pub fn set_console_config(&mut self, console_config: ConsoleConfig) -> &mut Self {
55        self.console = console_config;
56        self
57    }
58
59    pub fn set_show_origin(&mut self, show_origin: bool) -> &mut Self {
60        self.show_origin = show_origin;
61        self
62    }
63
64    /// Set if events will show additional fields, usually the file or line.
65    pub fn set_show_fields(&mut self, show_fields: bool) -> &mut Self {
66        self.show_fields = show_fields;
67        self
68    }
69
70    /// Set the base URL for origins. This can be used to show full file paths in the browser console.
71    pub fn set_origin_base_url(&mut self, origin_base_url: impl ToString) -> &mut Self {
72        self.origin_base_url = Some(origin_base_url.to_string());
73        self
74    }
75
76    /// True if the console reporting spans
77    pub fn console_enabled(&self) -> bool {
78        self.console.reporting_enabled()
79    }
80}
81
82#[test]
83fn test_default_built_config() {
84    let config = WasmLayerConfig::new();
85
86    assert_eq!(
87        config,
88        WasmLayerConfig {
89            report_logs_in_timings: true,
90            console: ConsoleConfig::ReportWithConsoleColor,
91            max_level: tracing::Level::TRACE,
92            show_fields: true,
93            show_origin: true,
94            origin_base_url: None
95        }
96    )
97}
98
99#[test]
100fn test_set_report_logs_in_timings() {
101    let mut config = WasmLayerConfig::new();
102    config.set_report_logs_in_timings(false);
103
104    assert!(!config.report_logs_in_timings);
105}
106
107#[test]
108fn test_set_console_config_no_reporting() {
109    let mut config = WasmLayerConfig::new();
110    config.set_console_config(ConsoleConfig::NoReporting);
111
112    assert!(!config.console.reporting_enabled());
113}
114
115#[test]
116fn test_set_console_config_without_color() {
117    let mut config = WasmLayerConfig::new();
118    config.set_console_config(ConsoleConfig::ReportWithoutConsoleColor);
119
120    assert_eq!(config.console, ConsoleConfig::ReportWithoutConsoleColor);
121}
122
123#[test]
124fn test_set_console_config_with_color() {
125    let mut config = WasmLayerConfig::new();
126    config.set_console_config(ConsoleConfig::ReportWithConsoleColor);
127
128    assert_eq!(config.console, ConsoleConfig::ReportWithConsoleColor);
129}
130
131#[test]
132fn test_set_config_log_level_warn() {
133    let mut config = WasmLayerConfig::new();
134    config.set_max_level(tracing::Level::WARN);
135
136    assert_eq!(config.max_level, tracing::Level::WARN);
137}