Skip to content

Commit ca6f552

Browse files
committed
feat(rustup-init): set log level to WARN on -q if RUSTUP_LOG is unset
1 parent f48df22 commit ca6f552

File tree

7 files changed

+61
-32
lines changed

7 files changed

+61
-32
lines changed

rustup-init.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Options:
4141
-v, --verbose
4242
Enable verbose output
4343
-q, --quiet
44-
Disable progress output
44+
Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is unset
4545
-y
4646
Disable confirmation prompt
4747
--default-host <DEFAULT_HOST>

src/bin/rustup-init.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use cfg_if::cfg_if;
2121
use rs_tracing::{
2222
close_trace_file, close_trace_file_internal, open_trace_file, trace_to_file_internal,
2323
};
24+
use tracing_subscriber::{reload::Handle, EnvFilter, Registry};
2425

2526
use rustup::cli::common;
2627
use rustup::cli::proxy_mode;
@@ -44,9 +45,9 @@ async fn main() -> Result<ExitCode> {
4445
opentelemetry::global::set_text_map_propagator(
4546
opentelemetry_sdk::propagation::TraceContextPropagator::new(),
4647
);
47-
let subscriber = rustup::cli::log::tracing_subscriber(&process);
48+
let (subscriber, console_filter) = rustup::cli::log::tracing_subscriber(&process);
4849
tracing::subscriber::set_global_default(subscriber)?;
49-
let result = run_rustup(&process).await;
50+
let result = run_rustup(&process, console_filter).await;
5051
// We're tracing, so block until all spans are exported.
5152
#[cfg(feature = "otel")]
5253
opentelemetry::global::shutdown_tracer_provider();
@@ -61,19 +62,25 @@ async fn main() -> Result<ExitCode> {
6162
}
6263

6364
#[cfg_attr(feature = "otel", tracing::instrument)]
64-
async fn run_rustup(process: &Process) -> Result<utils::ExitCode> {
65+
async fn run_rustup(
66+
process: &Process,
67+
console_filter: Handle<EnvFilter, Registry>,
68+
) -> Result<utils::ExitCode> {
6569
if let Ok(dir) = process.var("RUSTUP_TRACE_DIR") {
6670
open_trace_file!(dir)?;
6771
}
68-
let result = run_rustup_inner(process).await;
72+
let result = run_rustup_inner(process, console_filter).await;
6973
if process.var("RUSTUP_TRACE_DIR").is_ok() {
7074
close_trace_file!();
7175
}
7276
result
7377
}
7478

7579
#[cfg_attr(feature = "otel", tracing::instrument(err))]
76-
async fn run_rustup_inner(process: &Process) -> Result<utils::ExitCode> {
80+
async fn run_rustup_inner(
81+
process: &Process,
82+
console_filter: Handle<EnvFilter, Registry>,
83+
) -> Result<utils::ExitCode> {
7784
// Guard against infinite proxy recursion. This mostly happens due to
7885
// bugs in rustup.
7986
do_recursion_guard(process)?;
@@ -92,7 +99,7 @@ async fn run_rustup_inner(process: &Process) -> Result<utils::ExitCode> {
9299
// name. Browsers rename duplicates to
93100
// e.g. rustup-setup(2), and this allows all variations
94101
// to work.
95-
setup_mode::main(current_dir, process).await
102+
setup_mode::main(current_dir, process, console_filter).await
96103
}
97104
Some(n) if n.starts_with("rustup-gc-") => {
98105
// This is the final uninstallation stage on windows where

src/cli/log.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
11
use std::{fmt, io::Write};
22

3+
#[cfg(feature = "otel")]
4+
use opentelemetry_sdk::trace::Tracer;
35
use termcolor::{Color, ColorSpec, WriteColor};
46
use tracing::{level_filters::LevelFilter, Event, Subscriber};
57
use tracing_subscriber::{
68
fmt::{
79
format::{self, FormatEvent, FormatFields},
810
FmtContext,
911
},
12+
layer::SubscriberExt,
1013
registry::LookupSpan,
11-
EnvFilter, Layer,
14+
reload, EnvFilter, Layer, Registry,
1215
};
1316

14-
#[cfg(feature = "otel")]
15-
use opentelemetry_sdk::trace::Tracer;
16-
1717
use crate::{currentprocess::Process, utils::notify::NotificationLevel};
1818

19-
pub fn tracing_subscriber(process: &Process) -> impl tracing::Subscriber {
20-
use tracing_subscriber::{layer::SubscriberExt, Registry};
21-
19+
pub fn tracing_subscriber(
20+
process: &Process,
21+
) -> (
22+
impl tracing::Subscriber,
23+
reload::Handle<EnvFilter, Registry>,
24+
) {
2225
#[cfg(feature = "otel")]
2326
let telemetry = telemetry(process);
24-
let console_logger = console_logger(process);
27+
let (console_logger, console_filter) = console_logger(process);
2528
#[cfg(feature = "otel")]
2629
{
27-
Registry::default().with(console_logger).with(telemetry)
30+
(
31+
Registry::default().with(console_logger).with(telemetry),
32+
console_filter,
33+
)
2834
}
2935
#[cfg(not(feature = "otel"))]
3036
{
31-
Registry::default().with(console_logger)
37+
(Registry::default().with(console_logger), console_filter)
3238
}
3339
}
3440

@@ -38,7 +44,7 @@ pub fn tracing_subscriber(process: &Process) -> impl tracing::Subscriber {
3844
/// When the `RUSTUP_LOG` environment variable is present, a standard [`tracing_subscriber`]
3945
/// formatter will be used according to the filtering directives set in its value.
4046
/// Otherwise, this logger will use [`EventFormatter`] to mimic "classic" Rustup `stderr` output.
41-
fn console_logger<S>(process: &Process) -> impl Layer<S>
47+
fn console_logger<S>(process: &Process) -> (impl Layer<S>, reload::Handle<EnvFilter, S>)
4248
where
4349
S: Subscriber + for<'span> LookupSpan<'span>,
4450
{
@@ -55,17 +61,22 @@ where
5561
.with_writer(move || process.stderr())
5662
.with_ansi(has_ansi);
5763
if let Ok(directives) = maybe_rustup_log_directives {
58-
let env_filter = EnvFilter::builder()
59-
.with_default_directive(LevelFilter::INFO.into())
60-
.parse_lossy(directives);
61-
logger.compact().with_filter(env_filter).boxed()
64+
let (env_filter, handle) = reload::Layer::new(
65+
EnvFilter::builder()
66+
.with_default_directive(LevelFilter::INFO.into())
67+
.parse_lossy(directives),
68+
);
69+
(logger.compact().with_filter(env_filter).boxed(), handle)
6270
} else {
6371
// Receive log lines from Rustup only.
64-
let env_filter = EnvFilter::new("rustup=DEBUG");
65-
logger
66-
.event_format(EventFormatter)
67-
.with_filter(env_filter)
68-
.boxed()
72+
let (env_filter, handle) = reload::Layer::new(EnvFilter::new("rustup=DEBUG"));
73+
(
74+
logger
75+
.event_format(EventFormatter)
76+
.with_filter(env_filter)
77+
.boxed(),
78+
handle,
79+
)
6980
}
7081
}
7182

src/cli/setup_mode.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::path::PathBuf;
33
use anyhow::Result;
44
use clap::Parser;
55
use tracing::warn;
6+
use tracing_subscriber::{reload::Handle, EnvFilter, Registry};
67

78
use crate::{
89
cli::{
@@ -28,7 +29,7 @@ struct RustupInit {
2829
#[arg(short, long)]
2930
verbose: bool,
3031

31-
/// Disable progress output
32+
/// Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is unset
3233
#[arg(short, long)]
3334
quiet: bool,
3435

@@ -73,7 +74,11 @@ struct RustupInit {
7374
}
7475

7576
#[cfg_attr(feature = "otel", tracing::instrument)]
76-
pub async fn main(current_dir: PathBuf, process: &Process) -> Result<utils::ExitCode> {
77+
pub async fn main(
78+
current_dir: PathBuf,
79+
process: &Process,
80+
console_filter: Handle<EnvFilter, Registry>,
81+
) -> Result<utils::ExitCode> {
7782
use clap::error::ErrorKind;
7883

7984
let RustupInit {
@@ -111,6 +116,12 @@ pub async fn main(current_dir: PathBuf, process: &Process) -> Result<utils::Exit
111116
warn!("{}", common::WARN_COMPLETE_PROFILE);
112117
}
113118

119+
if quiet && process.var("RUSTUP_LOG").is_err() {
120+
console_filter
121+
.modify(|it| *it = EnvFilter::new("rustup=WARN"))
122+
.expect("error reloading `EnvFilter` for console_logger");
123+
}
124+
114125
let opts = InstallOpts {
115126
default_host_triple: default_host,
116127
default_toolchain,

src/currentprocess.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl TestProcess {
234234
impl From<TestContext> for TestProcess {
235235
fn from(inner: TestContext) -> Self {
236236
let inner = Process::TestProcess(inner);
237-
let guard = crate::cli::log::tracing_subscriber(&inner).set_default();
237+
let guard = crate::cli::log::tracing_subscriber(&inner).0.set_default();
238238
Self {
239239
process: inner,
240240
_guard: guard,

tests/suite/cli-ui/rustup-init/rustup-init_help_flag_stdout.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Options:
1212
-v, --verbose
1313
Enable verbose output
1414
-q, --quiet
15-
Disable progress output
15+
Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is unset
1616
-y
1717
Disable confirmation prompt
1818
--default-host <DEFAULT_HOST>

tests/suite/cli-ui/rustup-init/rustup-init_sh_help_flag_stdout.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Options:
1212
-v, --verbose
1313
Enable verbose output
1414
-q, --quiet
15-
Disable progress output
15+
Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is unset
1616
-y
1717
Disable confirmation prompt
1818
--default-host <DEFAULT_HOST>

0 commit comments

Comments
 (0)