#[derive(Debug, PartialEq, Eq, Clone)]
pub struct WasmLayerConfig {
pub enabled: bool,
pub report_logs_in_timings: bool,
pub report_logs_in_console: bool,
pub color: bool,
pub use_console_methods: bool,
pub max_level: tracing::Level,
pub show_fields: bool,
pub show_origin: bool,
pub show_timestamp: bool,
pub origin_base_url: Option<String>,
}
impl Default for WasmLayerConfig {
fn default() -> Self {
WasmLayerConfig {
enabled: true,
report_logs_in_timings: true,
report_logs_in_console: true,
use_console_methods: false,
color: true,
max_level: tracing::Level::TRACE,
show_fields: true,
show_origin: true,
show_timestamp: true,
origin_base_url: None,
}
}
}
impl WasmLayerConfig {
#[must_use]
pub fn new() -> WasmLayerConfig {
WasmLayerConfig::default()
}
#[must_use]
pub fn disable(mut self) -> Self {
self.enabled = false;
self
}
#[must_use]
pub fn remove_timings(mut self) -> Self {
self.report_logs_in_timings = false;
self
}
#[must_use]
pub fn remove_console(mut self) -> Self {
self.report_logs_in_console = false;
self
}
#[must_use]
pub fn remove_color(mut self) -> Self {
self.color = false;
self
}
#[must_use]
pub fn with_max_level(mut self, max_level: tracing::Level) -> Self {
self.max_level = max_level;
self
}
#[must_use]
pub fn remove_origin(mut self) -> Self {
self.show_origin = false;
self
}
#[must_use]
pub fn remove_fields(mut self) -> Self {
self.show_fields = false;
self
}
#[must_use]
pub fn remove_timestamp(mut self) -> Self {
self.show_timestamp = false;
self
}
#[must_use]
pub fn with_origin_base_url(mut self, origin_base_url: impl ToString) -> Self {
self.origin_base_url = Some(origin_base_url.to_string());
self
}
}
#[test]
fn test_default_built_config() {
let config = WasmLayerConfig::new();
assert_eq!(
config,
WasmLayerConfig {
enabled: true,
report_logs_in_timings: true,
report_logs_in_console: true,
color: true,
use_console_methods: false,
max_level: tracing::Level::TRACE,
show_fields: true,
show_origin: true,
show_timestamp: true,
origin_base_url: None
}
)
}
#[test]
fn test_set_no_reporting() {
let config = WasmLayerConfig::new().disable();
assert!(!config.enabled);
}
#[test]
fn test_set_report_logs_in_timings() {
let config = WasmLayerConfig::new().remove_timings();
assert!(!config.report_logs_in_timings);
}
#[test]
fn test_set_report_logs_in_console() {
let config = WasmLayerConfig::new().remove_console();
assert!(!config.report_logs_in_console);
}
#[test]
fn test_set_console_config_without_color() {
let config = WasmLayerConfig::new().remove_color();
assert!(!config.color);
}
#[test]
fn test_set_config_log_level_warn() {
let config = WasmLayerConfig::new().with_max_level(tracing::Level::WARN);
assert_eq!(config.max_level, tracing::Level::WARN);
}