Add cli option for tracing-filter (#35370)

Using environment variables is not really an option on ohos/android,
so add a CLI option to configure tracing.
Making it a `pref`, so that we can persist the filter
might also be desirable.

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
This commit is contained in:
Jonathan Schwender 2025-02-08 08:10:12 +01:00 committed by GitHub
parent 39c1e5d5d6
commit 654df4c8b7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 44 additions and 8 deletions

View file

@ -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.
// <https://docs.rs/tracing-subscriber/0.3.18/tracing_subscriber/layer/index.html#global-filtering>
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,