From ca6f552e79bc3789b5387091d5fc05ea7a529c07 Mon Sep 17 00:00:00 2001 From: rami3l Date: Wed, 26 Jun 2024 20:26:49 +0800 Subject: [PATCH] feat(rustup-init): set log level to `WARN` on `-q` if `RUSTUP_LOG` is unset --- rustup-init.sh | 2 +- src/bin/rustup-init.rs | 19 ++++--- src/cli/log.rs | 51 +++++++++++-------- src/cli/setup_mode.rs | 15 +++++- src/currentprocess.rs | 2 +- .../rustup-init_help_flag_stdout.toml | 2 +- .../rustup-init_sh_help_flag_stdout.toml | 2 +- 7 files changed, 61 insertions(+), 32 deletions(-) diff --git a/rustup-init.sh b/rustup-init.sh index 995d5360ae..59eae2b675 100755 --- a/rustup-init.sh +++ b/rustup-init.sh @@ -41,7 +41,7 @@ Options: -v, --verbose Enable verbose output -q, --quiet - Disable progress output + Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is unset -y Disable confirmation prompt --default-host diff --git a/src/bin/rustup-init.rs b/src/bin/rustup-init.rs index f60c5221d3..c3c3426bfb 100644 --- a/src/bin/rustup-init.rs +++ b/src/bin/rustup-init.rs @@ -21,6 +21,7 @@ use cfg_if::cfg_if; use rs_tracing::{ close_trace_file, close_trace_file_internal, open_trace_file, trace_to_file_internal, }; +use tracing_subscriber::{reload::Handle, EnvFilter, Registry}; use rustup::cli::common; use rustup::cli::proxy_mode; @@ -44,9 +45,9 @@ async fn main() -> Result { opentelemetry::global::set_text_map_propagator( opentelemetry_sdk::propagation::TraceContextPropagator::new(), ); - let subscriber = rustup::cli::log::tracing_subscriber(&process); + let (subscriber, console_filter) = rustup::cli::log::tracing_subscriber(&process); tracing::subscriber::set_global_default(subscriber)?; - let result = run_rustup(&process).await; + let result = run_rustup(&process, console_filter).await; // We're tracing, so block until all spans are exported. #[cfg(feature = "otel")] opentelemetry::global::shutdown_tracer_provider(); @@ -61,11 +62,14 @@ async fn main() -> Result { } #[cfg_attr(feature = "otel", tracing::instrument)] -async fn run_rustup(process: &Process) -> Result { +async fn run_rustup( + process: &Process, + console_filter: Handle, +) -> Result { if let Ok(dir) = process.var("RUSTUP_TRACE_DIR") { open_trace_file!(dir)?; } - let result = run_rustup_inner(process).await; + let result = run_rustup_inner(process, console_filter).await; if process.var("RUSTUP_TRACE_DIR").is_ok() { close_trace_file!(); } @@ -73,7 +77,10 @@ async fn run_rustup(process: &Process) -> Result { } #[cfg_attr(feature = "otel", tracing::instrument(err))] -async fn run_rustup_inner(process: &Process) -> Result { +async fn run_rustup_inner( + process: &Process, + console_filter: Handle, +) -> Result { // Guard against infinite proxy recursion. This mostly happens due to // bugs in rustup. do_recursion_guard(process)?; @@ -92,7 +99,7 @@ async fn run_rustup_inner(process: &Process) -> Result { // name. Browsers rename duplicates to // e.g. rustup-setup(2), and this allows all variations // to work. - setup_mode::main(current_dir, process).await + setup_mode::main(current_dir, process, console_filter).await } Some(n) if n.starts_with("rustup-gc-") => { // This is the final uninstallation stage on windows where diff --git a/src/cli/log.rs b/src/cli/log.rs index 4bb7fc227f..724ea4af1e 100644 --- a/src/cli/log.rs +++ b/src/cli/log.rs @@ -1,5 +1,7 @@ use std::{fmt, io::Write}; +#[cfg(feature = "otel")] +use opentelemetry_sdk::trace::Tracer; use termcolor::{Color, ColorSpec, WriteColor}; use tracing::{level_filters::LevelFilter, Event, Subscriber}; use tracing_subscriber::{ @@ -7,28 +9,32 @@ use tracing_subscriber::{ format::{self, FormatEvent, FormatFields}, FmtContext, }, + layer::SubscriberExt, registry::LookupSpan, - EnvFilter, Layer, + reload, EnvFilter, Layer, Registry, }; -#[cfg(feature = "otel")] -use opentelemetry_sdk::trace::Tracer; - use crate::{currentprocess::Process, utils::notify::NotificationLevel}; -pub fn tracing_subscriber(process: &Process) -> impl tracing::Subscriber { - use tracing_subscriber::{layer::SubscriberExt, Registry}; - +pub fn tracing_subscriber( + process: &Process, +) -> ( + impl tracing::Subscriber, + reload::Handle, +) { #[cfg(feature = "otel")] let telemetry = telemetry(process); - let console_logger = console_logger(process); + let (console_logger, console_filter) = console_logger(process); #[cfg(feature = "otel")] { - Registry::default().with(console_logger).with(telemetry) + ( + Registry::default().with(console_logger).with(telemetry), + console_filter, + ) } #[cfg(not(feature = "otel"))] { - Registry::default().with(console_logger) + (Registry::default().with(console_logger), console_filter) } } @@ -38,7 +44,7 @@ pub fn tracing_subscriber(process: &Process) -> impl tracing::Subscriber { /// When the `RUSTUP_LOG` environment variable is present, a standard [`tracing_subscriber`] /// formatter will be used according to the filtering directives set in its value. /// Otherwise, this logger will use [`EventFormatter`] to mimic "classic" Rustup `stderr` output. -fn console_logger(process: &Process) -> impl Layer +fn console_logger(process: &Process) -> (impl Layer, reload::Handle) where S: Subscriber + for<'span> LookupSpan<'span>, { @@ -55,17 +61,22 @@ where .with_writer(move || process.stderr()) .with_ansi(has_ansi); if let Ok(directives) = maybe_rustup_log_directives { - let env_filter = EnvFilter::builder() - .with_default_directive(LevelFilter::INFO.into()) - .parse_lossy(directives); - logger.compact().with_filter(env_filter).boxed() + let (env_filter, handle) = reload::Layer::new( + EnvFilter::builder() + .with_default_directive(LevelFilter::INFO.into()) + .parse_lossy(directives), + ); + (logger.compact().with_filter(env_filter).boxed(), handle) } else { // Receive log lines from Rustup only. - let env_filter = EnvFilter::new("rustup=DEBUG"); - logger - .event_format(EventFormatter) - .with_filter(env_filter) - .boxed() + let (env_filter, handle) = reload::Layer::new(EnvFilter::new("rustup=DEBUG")); + ( + logger + .event_format(EventFormatter) + .with_filter(env_filter) + .boxed(), + handle, + ) } } diff --git a/src/cli/setup_mode.rs b/src/cli/setup_mode.rs index a381b829aa..3911f7d59a 100644 --- a/src/cli/setup_mode.rs +++ b/src/cli/setup_mode.rs @@ -3,6 +3,7 @@ use std::path::PathBuf; use anyhow::Result; use clap::Parser; use tracing::warn; +use tracing_subscriber::{reload::Handle, EnvFilter, Registry}; use crate::{ cli::{ @@ -28,7 +29,7 @@ struct RustupInit { #[arg(short, long)] verbose: bool, - /// Disable progress output + /// Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is unset #[arg(short, long)] quiet: bool, @@ -73,7 +74,11 @@ struct RustupInit { } #[cfg_attr(feature = "otel", tracing::instrument)] -pub async fn main(current_dir: PathBuf, process: &Process) -> Result { +pub async fn main( + current_dir: PathBuf, + process: &Process, + console_filter: Handle, +) -> Result { use clap::error::ErrorKind; let RustupInit { @@ -111,6 +116,12 @@ pub async fn main(current_dir: PathBuf, process: &Process) -> Result for TestProcess { fn from(inner: TestContext) -> Self { let inner = Process::TestProcess(inner); - let guard = crate::cli::log::tracing_subscriber(&inner).set_default(); + let guard = crate::cli::log::tracing_subscriber(&inner).0.set_default(); Self { process: inner, _guard: guard, diff --git a/tests/suite/cli-ui/rustup-init/rustup-init_help_flag_stdout.toml b/tests/suite/cli-ui/rustup-init/rustup-init_help_flag_stdout.toml index f080fc9e5c..c51af98b68 100644 --- a/tests/suite/cli-ui/rustup-init/rustup-init_help_flag_stdout.toml +++ b/tests/suite/cli-ui/rustup-init/rustup-init_help_flag_stdout.toml @@ -12,7 +12,7 @@ Options: -v, --verbose Enable verbose output -q, --quiet - Disable progress output + Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is unset -y Disable confirmation prompt --default-host diff --git a/tests/suite/cli-ui/rustup-init/rustup-init_sh_help_flag_stdout.toml b/tests/suite/cli-ui/rustup-init/rustup-init_sh_help_flag_stdout.toml index 8646af73af..1866e86f4e 100644 --- a/tests/suite/cli-ui/rustup-init/rustup-init_sh_help_flag_stdout.toml +++ b/tests/suite/cli-ui/rustup-init/rustup-init_sh_help_flag_stdout.toml @@ -12,7 +12,7 @@ Options: -v, --verbose Enable verbose output -q, --quiet - Disable progress output + Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is unset -y Disable confirmation prompt --default-host