Skip to content

Commit

Permalink
add origin_base_url to WasmLayerConfig (#15)
Browse files Browse the repository at this point in the history
This can be a url that gets prependend to origins (file locations). When printed
into a browser console with debug symbols enabled (e.g. via
rustwasm/wasm-bindgen#2389), it becomes possible to
navigate to sources.

This is a breaking change as it requires `WasmLayerConfig` to not be `Copy`.
  • Loading branch information
rksm authored Jan 9, 2025
1 parent 8e20e55 commit 66a11ad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use console::*;
pub type WASMLayerConfig = WasmLayerConfig;

///Configuration parameters for the [WasmLayer](crate::prelude::WasmLayer).
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct WasmLayerConfig {
/// In dev-tools, report timings of traces
pub report_logs_in_timings: bool,
Expand All @@ -18,6 +18,8 @@ pub struct WasmLayerConfig {
pub show_fields: bool,
/// Show origin (line number, source)
pub show_origin: bool,
/// 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.
pub origin_base_url: Option<String>,
}

impl Default for WasmLayerConfig {
Expand All @@ -28,6 +30,7 @@ impl Default for WasmLayerConfig {
max_level: tracing::Level::TRACE,
show_fields: true,
show_origin: true,
origin_base_url: None,
}
}
}
Expand Down Expand Up @@ -67,6 +70,12 @@ impl WasmLayerConfig {
self
}

/// Set the base URL for origins. This can be used to show full file paths in the browser console.
pub fn set_origin_base_url(&mut self, origin_base_url: impl ToString) -> &mut Self {
self.origin_base_url = Some(origin_base_url.to_string());
self
}

/// True if the console reporting spans
pub fn console_enabled(&self) -> bool {
self.console.reporting_enabled()
Expand All @@ -84,7 +93,8 @@ fn test_default_built_config() {
console: ConsoleConfig::ReportWithConsoleColor,
max_level: tracing::Level::TRACE,
show_fields: true,
show_origin: true
show_origin: true,
origin_base_url: None
}
)
}
Expand Down
11 changes: 10 additions & 1 deletion src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,16 @@ impl<S: Subscriber + for<'a> LookupSpan<'a>> Layer<S> for WasmLayer {
}
let origin = if self.config.show_origin {
meta.file()
.and_then(|file| meta.line().map(|ln| format!("{}:{}", file, ln)))
.and_then(|file| {
meta.line().map(|ln| {
format!(
"{}{}:{}",
self.config.origin_base_url.as_deref().unwrap_or_default(),
file,
ln
)
})
})
.unwrap_or_default()
} else {
String::new()
Expand Down

0 comments on commit 66a11ad

Please sign in to comment.