diff --git a/src/currentprocess.rs b/src/currentprocess.rs index cb54fed2ea..e7d9e553a8 100644 --- a/src/currentprocess.rs +++ b/src/currentprocess.rs @@ -32,6 +32,8 @@ use cwdsource::*; use filesource::*; use varsource::*; +use crate::currentprocess; + /// An abstraction for the current process. /// /// This acts as a clonable proxy to the global state provided by some key OS @@ -136,6 +138,8 @@ pub fn with(process: Process, f: F) -> R where F: FnOnce() -> R, { + use tracing_subscriber::util::SubscriberInitExt; + ensure_hook(); PROCESS.with(|p| { @@ -143,6 +147,7 @@ where panic!("current process already set {old_p:?}"); } *p.borrow_mut() = Some(process); + let _guard = console_logger().set_default(); let result = f(); *p.borrow_mut() = None; result @@ -159,6 +164,35 @@ fn ensure_hook() { }); } +fn console_logger() -> impl tracing::Subscriber { + use tracing_subscriber::{ + filter::{EnvFilter, LevelFilter}, + layer::SubscriberExt, + Layer, Registry, + }; + + let curr_process = currentprocess::process(); + let maybe_directives = curr_process.var_os("RUST_LOG").clone(); + let logger = tracing_subscriber::fmt::layer() + .with_writer(move || curr_process.stderr()) + .with_ansi(false); + let console_logger = if let Some(directives) = maybe_directives { + let env_filter = EnvFilter::builder() + .with_default_directive(LevelFilter::INFO.into()) + .parse_lossy(directives.to_string_lossy()); + logger.compact().with_filter(env_filter).boxed() + } else { + // Receive log lines from Rustup only. + let env_filter = EnvFilter::new("rustup=DEBUG"); + logger + .event_format(crate::cli::log::EventFormatter) + .with_filter(env_filter) + .boxed() + }; + // TODO: What about the original otel logger? + Registry::default().with(console_logger) +} + /// Run a function in the context of a process definition and a tokio runtime. /// /// The process state is injected into a thread-local in every work thread of diff --git a/src/test.rs b/src/test.rs index 7ee76e7b19..e01100eb52 100644 --- a/src/test.rs +++ b/src/test.rs @@ -239,10 +239,7 @@ type Tracer = (); /// A tracer for the tests. static TRACER: Lazy = Lazy::new(|| { - use crate::currentprocess::{filesource::StderrSource, varsource::VarSource}; - use tokio::runtime::Handle; - use tracing_subscriber::{fmt, layer::SubscriberExt, EnvFilter, Layer, Registry}; // Use the current runtime, or the sync test runtime otherwise. let handle = match Handle::try_current() { @@ -251,9 +248,6 @@ static TRACER: Lazy = Lazy::new(|| { }; let _guard = handle.enter(); - // TODO: However this will not be set before running the actual test. What now? - let curr_process = currentprocess::process(); - let has_ansi = curr_process.stderr().is_a_tty(); #[cfg(feature = "otel")] let tracer = { @@ -284,6 +278,7 @@ static TRACER: Lazy = Lazy::new(|| { let telemetry = { use opentelemetry::global; use opentelemetry_sdk::propagation::TraceContextPropagator; + use tracing_subscriber::{EnvFilter, Layer}; global::set_text_map_propagator(TraceContextPropagator::new()); @@ -292,32 +287,16 @@ static TRACER: Lazy = Lazy::new(|| { .with_tracer(tracer.clone()) .with_filter(env_filter) }; - let console_logger = { - let is_verbose = curr_process.var_os("RUST_LOG").is_some(); - let logger = fmt::layer() - .with_writer(move || curr_process.stderr()) - .with_ansi(has_ansi); - if is_verbose { - let env_filter = - EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("INFO")); - logger.compact().with_filter(env_filter).boxed() - } else { - // Receive log lines from Rustup only. - let env_filter = EnvFilter::new("rustup=DEBUG"); - logger - .event_format(crate::cli::log::EventFormatter) - .with_filter(env_filter) - .boxed() - } - }; let subscriber = { + use tracing_subscriber::Registry; #[cfg(feature = "otel")] { - Registry::default().with(console_logger).with(telemetry) + use tracing_subscriber::layer::SubscriberExt; + Registry::default().with(telemetry) } #[cfg(not(feature = "otel"))] { - Registry::default().with(console_logger) + Registry::default() } }; tracing::subscriber::set_global_default(subscriber).unwrap();