diff --git a/ports/servoshell/desktop/cli.rs b/ports/servoshell/desktop/cli.rs index 588a2f04a9d..a05cafb5e59 100644 --- a/ports/servoshell/desktop/cli.rs +++ b/ports/servoshell/desktop/cli.rs @@ -11,7 +11,6 @@ use crate::prefs::{parse_command_line_arguments, ArgumentParsingResult}; pub fn main() { crate::crash_handler::install(); - crate::init_tracing(); crate::init_crypto(); crate::resources::init(); @@ -27,6 +26,8 @@ pub fn main() { }, }; + crate::init_tracing(servoshell_preferences.tracing_filter.as_deref()); + let clean_shutdown = servoshell_preferences.clean_shutdown; let event_loop = EventsLoop::new(servoshell_preferences.headless, opts.output_file.is_some()) .expect("Failed to create events loop"); diff --git a/ports/servoshell/egl/android/simpleservo.rs b/ports/servoshell/egl/android/simpleservo.rs index 04df03fb983..048b2e84e0e 100644 --- a/ports/servoshell/egl/android/simpleservo.rs +++ b/ports/servoshell/egl/android/simpleservo.rs @@ -52,7 +52,6 @@ pub fn init( waker: Box, callbacks: Box, ) -> Result<(), &'static str> { - crate::init_tracing(); crate::init_crypto(); resources::set(Box::new(ResourceReaderInstance::new())); @@ -69,6 +68,8 @@ pub fn init( }, }; + crate::init_tracing(servoshell_preferences.tracing_filter.as_deref()); + // Initialize surfman let connection = Connection::new().or(Err("Failed to create connection"))?; let adapter = connection diff --git a/ports/servoshell/egl/ohos/simpleservo.rs b/ports/servoshell/egl/ohos/simpleservo.rs index 847664ece0d..723cd4c6105 100644 --- a/ports/servoshell/egl/ohos/simpleservo.rs +++ b/ports/servoshell/egl/ohos/simpleservo.rs @@ -36,7 +36,6 @@ pub fn init( callbacks: Box, ) -> Result, &'static str> { info!("Entered simpleservo init function"); - crate::init_tracing(); crate::init_crypto(); let resource_dir = PathBuf::from(&options.resource_dir).join("servo"); resources::set(Box::new(ResourceReaderInstance::new(resource_dir))); @@ -61,6 +60,8 @@ pub fn init( }, }; + crate::init_tracing(servoshell_preferences.tracing_filter.as_deref()); + // Initialize surfman let connection = Connection::new().or(Err("Failed to create connection"))?; let adapter = connection diff --git a/ports/servoshell/lib.rs b/ports/servoshell/lib.rs index 4c624c8535c..e9cad7ca480 100644 --- a/ports/servoshell/lib.rs +++ b/ports/servoshell/lib.rs @@ -44,7 +44,13 @@ pub fn init_crypto() { .expect("Error initializing crypto provider"); } -pub fn init_tracing() { +pub fn init_tracing(filter_directives: Option<&str>) { + #[cfg(not(feature = "tracing"))] + { + if filter_directives.is_some() { + log::debug!("The tracing feature was not selected - ignoring trace filter directives"); + } + } #[cfg(feature = "tracing")] { use tracing_subscriber::layer::SubscriberExt; @@ -69,10 +75,16 @@ pub fn init_tracing() { // Filter events and spans by the directives in SERVO_TRACING, using EnvFilter as a global filter. // - let filter = tracing_subscriber::EnvFilter::builder() - .with_default_directive(tracing::level_filters::LevelFilter::OFF.into()) - .with_env_var("SERVO_TRACING") - .from_env_lossy(); + let filter_builder = tracing_subscriber::EnvFilter::builder() + .with_default_directive(tracing::level_filters::LevelFilter::OFF.into()); + let filter = if let Some(filters) = &filter_directives { + filter_builder.parse_lossy(filters) + } else { + filter_builder + .with_env_var("SERVO_TRACING") + .from_env_lossy() + }; + let subscriber = subscriber.with(filter); // Same as SubscriberInitExt::init, but avoids initialising the tracing-log compat layer, diff --git a/ports/servoshell/prefs.rs b/ports/servoshell/prefs.rs index 38829bf4e96..7826937c517 100644 --- a/ports/servoshell/prefs.rs +++ b/ports/servoshell/prefs.rs @@ -37,6 +37,11 @@ pub(crate) struct ServoShellPreferences { /// Whether or not to run servoshell in headless mode. While running in headless /// mode, image output is supported. pub headless: bool, + /// Filter directives for our tracing implementation. + /// + /// Overrides directives specified via `SERVO_TRACING` if set. + /// See: + pub tracing_filter: Option, } impl Default for ServoShellPreferences { @@ -50,6 +55,7 @@ impl Default for ServoShellPreferences { no_native_titlebar: true, searchpage: "https://duckduckgo.com/html/?q=%s".into(), headless: false, + tracing_filter: None, } } } @@ -311,6 +317,13 @@ pub(crate) fn parse_command_line_arguments(args: Vec) -> ArgumentParsing "Set custom user agent string (or ios / android / desktop for platform default)", "NCSA Mosaic/1.0 (X11;SunOS 4.1.4 sun4m)", ); + opts.optmulti( + "", + "tracing-filter", + "Define a custom filter for traces. Overrides `SERVO_TRACING` if set.", + "FILTER", + ); + opts.optmulti( "", "pref", @@ -353,6 +366,13 @@ pub(crate) fn parse_command_line_arguments(args: Vec) -> ArgumentParsing if let Some(content_process) = opt_match.opt_str("content-process") { return ArgumentParsingResult::ContentProcess(content_process); } + // Env-Filter directives are comma seperated. + let filters = opt_match.opt_strs("tracing-filter").join(","); + let tracing_filter = if filters.is_empty() { + None + } else { + Some(filters) + }; let mut debug_options = DebugOptions::default(); for debug_string in opt_match.opt_strs("Z") { @@ -544,6 +564,7 @@ pub(crate) fn parse_command_line_arguments(args: Vec) -> ArgumentParsing device_pixel_ratio_override, clean_shutdown: opt_match.opt_present("clean-shutdown"), headless: opt_match.opt_present("z"), + tracing_filter, ..Default::default() };