Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
rayon = {version = "1.5", optional = true}
tracing = {version = "0.1", features = ["attributes"], default-features = false}
tracing-log = { version = "0.1", default-features = false, optional = true }
tracing-subscriber = {version = "0.3", features = ["registry"], default-features = false}
wasm-bindgen = {version = "0.2"}

Expand Down
39 changes: 32 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use tracing::{
dispatcher::SetGlobalDefaultError,
field::{Field, Visit},
};
#[cfg(feature = "tracing-log")]
use tracing_log::NormalizeEvent;
use tracing_subscriber::layer::*;
use tracing_subscriber::registry::*;

Expand Down Expand Up @@ -44,6 +46,7 @@ mod test {
report_logs_in_console: true,
use_console_color: true,
max_level: tracing::Level::TRACE,
show_fields: true,
}
)
}
Expand Down Expand Up @@ -126,6 +129,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 show additional fields, usually the file or line.
show_fields: bool,
}

impl WASMLayerConfigBuilder {
Expand Down Expand Up @@ -171,13 +176,20 @@ impl WASMLayerConfigBuilder {
self
}

/// Set if events will show additional fields, usually the file or line.
pub fn set_show_fields(&mut self, show_fields: bool) -> &mut WASMLayerConfigBuilder {
self.show_fields = show_fields;
self
}

/// Build the WASMLayerConfig
pub fn build(&self) -> WASMLayerConfig {
WASMLayerConfig {
report_logs_in_timings: self.report_logs_in_timings,
report_logs_in_console: self.report_logs_in_console,
use_console_color: self.use_console_color,
max_level: self.max_level,
show_fields: self.show_fields,
}
}
}
Expand All @@ -189,6 +201,7 @@ impl Default for WASMLayerConfigBuilder {
report_logs_in_console: true,
use_console_color: true,
max_level: tracing::Level::TRACE,
show_fields: true,
}
}
}
Expand All @@ -199,6 +212,7 @@ pub struct WASMLayerConfig {
report_logs_in_console: bool,
use_console_color: bool,
max_level: tracing::Level,
show_fields: bool,
}

impl core::default::Default for WASMLayerConfig {
Expand All @@ -208,6 +222,7 @@ impl core::default::Default for WASMLayerConfig {
report_logs_in_console: true,
use_console_color: true,
max_level: tracing::Level::TRACE,
show_fields: true,
}
}
}
Expand Down Expand Up @@ -273,7 +288,7 @@ impl<S: Subscriber + for<'a> LookupSpan<'a>> Layer<S> for WASMLayer {
id: &tracing::Id,
ctx: Context<'_, S>,
) {
let mut new_debug_record = StringRecorder::new();
let mut new_debug_record = StringRecorder::new(self.config.show_fields);
attrs.record(&mut new_debug_record);

if let Some(span_ref) = ctx.span(id) {
Expand All @@ -297,8 +312,13 @@ impl<S: Subscriber + for<'a> LookupSpan<'a>> Layer<S> for WASMLayer {
/// doc: Notifies this layer that an event has occurred.
fn on_event(&self, event: &tracing::Event<'_>, _ctx: Context<'_, S>) {
if self.config.report_logs_in_timings || self.config.report_logs_in_console {
let mut recorder = StringRecorder::new();
let mut recorder = StringRecorder::new(self.config.show_fields);
event.record(&mut recorder);
#[cfg(feature = "tracing-log")]
let normalized_meta = event.normalized_metadata();
#[cfg(feature = "tracing-log")]
let meta = normalized_meta.as_ref().unwrap_or_else(|| event.metadata());
#[cfg(not(feature = "tracing-log"))]
let meta = event.metadata();
let level = meta.level();
if self.config.report_logs_in_console {
Expand Down Expand Up @@ -421,12 +441,13 @@ pub fn set_as_global_default_with_config(config: WASMLayerConfig) {
struct StringRecorder {
display: String,
is_following_args: bool,
show_fields: bool,
}
impl StringRecorder {
fn new() -> Self {
fn new(show_fields: bool) -> Self {
StringRecorder {
display: String::new(),
is_following_args: false,
show_fields,
..Default::default()
}
}
}
Expand All @@ -439,7 +460,7 @@ impl Visit for StringRecorder {
} else {
self.display = format!("{:?}", value)
}
} else {
} else if self.show_fields {
if self.is_following_args {
// following args
writeln!(self.display).unwrap();
Expand All @@ -465,6 +486,10 @@ impl core::fmt::Display for StringRecorder {

impl core::default::Default for StringRecorder {
fn default() -> Self {
StringRecorder::new()
Self {
display: String::new(),
is_following_args: false,
show_fields: true,
}
}
}