Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toggle to config controlling whether origin (file + line number) is included in output #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ mod test {
report_logs_in_console: true,
use_console_color: true,
max_level: tracing::Level::TRACE,
include_origin: true,
}
)
}
Expand Down Expand Up @@ -109,6 +110,16 @@ mod test {

assert_eq!(config.max_level, tracing::Level::WARN);
}

#[test]
fn test_set_include_origin() {
let mut builder = WASMLayerConfigBuilder::new();
builder.set_include_origin(false);

let config = builder.build();

assert_eq!(config.include_origin, false);
}
}

pub enum ConsoleConfig {
Expand All @@ -126,6 +137,8 @@ pub struct WASMLayerConfigBuilder {
use_console_color: bool,
/// Log events will be reported from this level -- Default is ALL (TRACE)
max_level: tracing::Level,
/// Log events will include the source file and line number -- Default is true
include_origin: bool,
}

impl WASMLayerConfigBuilder {
Expand All @@ -148,6 +161,12 @@ impl WASMLayerConfigBuilder {
self
}

// Set whether logs should include the source file and line number
pub fn set_include_origin(&mut self, include_origin: bool) -> &mut WASMLayerConfigBuilder {
self.include_origin = include_origin;
self
}

/// Set if and how events should be displayed in the browser console
pub fn set_console_config(
&mut self,
Expand Down Expand Up @@ -178,6 +197,7 @@ impl WASMLayerConfigBuilder {
report_logs_in_console: self.report_logs_in_console,
use_console_color: self.use_console_color,
max_level: self.max_level,
include_origin: self.include_origin,
}
}
}
Expand All @@ -189,6 +209,7 @@ impl Default for WASMLayerConfigBuilder {
report_logs_in_console: true,
use_console_color: true,
max_level: tracing::Level::TRACE,
include_origin: true,
}
}
}
Expand All @@ -199,6 +220,7 @@ pub struct WASMLayerConfig {
report_logs_in_console: bool,
use_console_color: bool,
max_level: tracing::Level,
include_origin: bool,
}

impl core::default::Default for WASMLayerConfig {
Expand All @@ -208,6 +230,7 @@ impl core::default::Default for WASMLayerConfig {
report_logs_in_console: true,
use_console_color: true,
max_level: tracing::Level::TRACE,
include_origin: true,
}
}
}
Expand Down Expand Up @@ -302,10 +325,13 @@ impl<S: Subscriber + for<'a> LookupSpan<'a>> Layer<S> for WASMLayer {
let meta = event.metadata();
let level = meta.level();
if self.config.report_logs_in_console {
let origin = meta
.file()
.and_then(|file| meta.line().map(|ln| format!("{}:{}", file, ln)))
.unwrap_or_default();
let origin = if self.config.include_origin {
meta.file()
.and_then(|file| meta.line().map(|ln| format!("{}:{}", file, ln)))
.unwrap_or_default()
} else {
String::new()
};

if self.config.use_console_color {
log4(
Expand Down