diff --git a/crates/bench/src/main.rs b/crates/bench/src/main.rs index 4d0e6373d66..425e482a77c 100644 --- a/crates/bench/src/main.rs +++ b/crates/bench/src/main.rs @@ -1,4 +1,5 @@ mod termcolorful; +use std::str::FromStr; use std::{path::PathBuf, time::Instant}; use mimalloc_rust::GlobalMiMalloc; @@ -6,19 +7,43 @@ use rspack_core::Compiler; use rspack_fs::AsyncNativeFileSystem; use rspack_testing::apply_from_fixture; #[cfg(feature = "tracing")] -use rspack_tracing::enable_tracing_by_env_with_chrome_layer; +use rspack_tracing::{enable_tracing_by_env, enable_tracing_by_env_with_chrome_layer}; use termcolorful::println_string_with_fg_color; #[cfg(all(not(all(target_os = "linux", target_arch = "aarch64", target_env = "musl"))))] #[global_allocator] static GLOBAL: GlobalMiMalloc = GlobalMiMalloc; +#[derive(Default, Clone, Copy)] +enum Layer { + #[default] + Logger, + Chrome, +} + +impl FromStr for Layer { + type Err = (); + + fn from_str(s: &str) -> Result { + Ok(match s { + "chrome" => Self::Chrome, + "logger" => Self::Logger, + _ => unimplemented!("Unknown layer {s}, please use one of `chrome`, `logger` "), + }) + } +} + #[tokio::main] async fn main() { + #[cfg(feature = "tracing")] + let layer = std::env::var("layer") + .ok() + .and_then(|var| Layer::from_str(&var).ok()) + .unwrap_or_default(); let path_list = vec![ // "examples/cjs-tree-shaking-basic", // "examples/basic", - "smoke-examples/basic", + "smoke-example/basic", // "examples/export-star-chain", // "examples/bbb", /* "examples/named-export-decl-with-src-eval", @@ -27,13 +52,21 @@ async fn main() { ]; for p in path_list { println_string_with_fg_color(p, termcolorful::Color::Red); - run(p).await; + run( + p, + #[cfg(feature = "tracing")] + layer, + ) + .await; } } -async fn run(relative_path: &str) { +async fn run(relative_path: &str, #[cfg(feature = "tracing")] layer: Layer) { #[cfg(feature = "tracing")] - let guard = enable_tracing_by_env_with_chrome_layer(); + let guard = match layer { + Layer::Logger => enable_tracing_by_env(), + Layer::Chrome => enable_tracing_by_env_with_chrome_layer(), + }; let manifest_dir = PathBuf::from(env!("CARGO_WORKSPACE_DIR")); // let bundle_dir = manifest_dir.join("tests/fixtures/postcss/pxtorem"); let bundle_dir: PathBuf = manifest_dir.join(relative_path); diff --git a/crates/rspack_tracing/src/lib.rs b/crates/rspack_tracing/src/lib.rs index 57cddbe4723..c9254bd9fd1 100644 --- a/crates/rspack_tracing/src/lib.rs +++ b/crates/rspack_tracing/src/lib.rs @@ -1,8 +1,10 @@ +use std::str::FromStr; use std::sync::atomic::AtomicBool; use tracing::Level; use tracing_chrome::FlushGuard; use tracing_subscriber::{fmt::format::FmtSpan, layer::Filter}; +use tracing_subscriber::{EnvFilter, Layer}; // use tracing_chrome::FlushGuard; static IS_TRACING_ENABLED: AtomicBool = AtomicBool::new(false); @@ -17,13 +19,16 @@ impl Filter for FilterEvent { !meta.is_event() } } -pub fn enable_tracing_by_env() { - let is_enable_tracing = std::env::var("TRACE").map_or(false, |x| { - matches!(x.as_str(), "TRACE" | "DEBUG" | "INFO" | "WARN" | "ERROR") - }); +pub fn enable_tracing_by_env() -> Option { + let trace_var = std::env::var("TRACE").ok(); + let is_enable_tracing = trace_var.is_some(); if is_enable_tracing && !IS_TRACING_ENABLED.swap(true, std::sync::atomic::Ordering::SeqCst) { - use tracing_subscriber::{fmt, prelude::*, EnvFilter}; + use tracing_subscriber::{fmt, prelude::*}; + let layers = generate_common_layers(trace_var); + tracing_subscriber::registry() + // .with(EnvFilter::from_env("TRACE").and_then(rspack_only_layer)) + .with(layers) .with( fmt::layer() .pretty() @@ -31,48 +36,59 @@ pub fn enable_tracing_by_env() { // To keep track of the closing point of spans .with_span_events(FmtSpan::CLOSE), ) - .with( - tracing_subscriber::filter::Targets::new().with_targets(vec![ - ("rspack_core", Level::TRACE), - ("rspack", Level::TRACE), - ("rspack_node", Level::TRACE), - ("rspack_plugin_javascript", Level::TRACE), - ("rspack_plugin_split_chunks", Level::TRACE), - ("rspack_binding_options", Level::TRACE), - ]), - ) - // Using TRACE=[TRACE|DEBUG|INFO|WARN|ERROR] to set max trace level. - .with(EnvFilter::from_env("TRACE")) .init(); tracing::trace!("enable_tracing_by_env"); } + None +} + +fn generate_common_layers( + trace_var: Option, +) -> Vec + Send + Sync>> { + let default_level = trace_var.as_ref().and_then(|var| Level::from_str(var).ok()); + + let mut layers = vec![]; + if let Some(default_level) = default_level { + layers.push( + tracing_subscriber::filter::Targets::new() + .with_targets(vec![ + ("rspack_core", default_level), + ("rspack", default_level), + ("rspack_node", default_level), + ("rspack_plugin_javascript", default_level), + ("rspack_plugin_split_chunks", default_level), + ("rspack_binding_options", default_level), + ]) + .boxed(), + ); + } else { + // SAFETY: we know that trace_var is `Ok(StrinG)` now, + // for the second unwrap, if we can't parse the directive, then the tracing result would be + // unexpected, then panic is reasonable + let env_layer = EnvFilter::builder() + .with_regex(true) + .parse(trace_var.expect("Should not be empty")) + .expect("Parse tracing directive syntax failed,for details about the directive syntax you could refer https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives"); + + layers.push(env_layer.boxed()); + } + layers } pub fn enable_tracing_by_env_with_chrome_layer() -> Option { - let is_enable_tracing = std::env::var("TRACE").map_or(false, |x| { - matches!(x.as_str(), "TRACE" | "DEBUG" | "INFO" | "WARN" | "ERROR") - }); + let trace_var = std::env::var("TRACE").ok(); + let is_enable_tracing = trace_var.is_some(); if is_enable_tracing && !IS_TRACING_ENABLED.swap(true, std::sync::atomic::Ordering::SeqCst) { use tracing_chrome::ChromeLayerBuilder; - use tracing_subscriber::{prelude::*, EnvFilter}; + use tracing_subscriber::prelude::*; let (chrome_layer, guard) = ChromeLayerBuilder::new().include_args(true).build(); + let layers = generate_common_layers(trace_var); // If we don't do this. chrome_layer will collect nothing. // std::mem::forget(guard); tracing_subscriber::registry() + .with(layers) .with(chrome_layer.with_filter(FilterEvent {})) - .with( - tracing_subscriber::filter::Targets::new().with_targets(vec![ - ("rspack_core", Level::TRACE), - ("rspack", Level::TRACE), - ("rspack_node", Level::TRACE), - ("rspack_plugin_javascript", Level::TRACE), - ("rspack_plugin_split_chunks", Level::TRACE), - ("rspack_binding_options", Level::TRACE), - ]), - ) - // Using TRACE=[TRACE|DEBUG|INFO|WARN|ERROR] to set max trace level. - .with(EnvFilter::from_env("TRACE")) .init(); Some(guard) } else {