Skip to content

Commit ec5ce0e

Browse files
committed
refactor(cli/setup-mode): extract update_console_logger()
1 parent 7c96264 commit ec5ce0e

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

src/cli/common.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::{cmp, env};
1313
use anyhow::{anyhow, Context, Result};
1414
use git_testament::{git_testament, render_testament};
1515
use tracing::{debug, error, info, trace, warn};
16+
use tracing_subscriber::{reload::Handle, EnvFilter, Registry};
1617

1718
use super::self_update;
1819
use crate::{
@@ -652,3 +653,31 @@ pub(crate) fn warn_if_host_is_emulated(process: &Process) {
652653
warn!("For best compatibility and performance you should reinstall rustup for your native CPU.");
653654
}
654655
}
656+
657+
/// Updates the console logger level according to whether `quiet` or `verbose` is set to `true`.
658+
///
659+
/// Does nothing if at least one of the following conditions is met:
660+
/// - The `RUSTUP_LOG` environment variable is set.
661+
/// - Both `quiet` and `verbose` are set to `true`.
662+
pub(super) fn update_console_filter(
663+
process: &Process,
664+
filter: &Handle<EnvFilter, Registry>,
665+
quiet: bool,
666+
verbose: bool,
667+
) {
668+
if process.var("RUSTUP_LOG").is_ok() {
669+
return;
670+
}
671+
672+
let maybe_directives = match (quiet, verbose) {
673+
(true, _) => Some("rustup=WARN"),
674+
(_, true) => Some("rustup=DEBUG"),
675+
(_, _) => None,
676+
};
677+
678+
if let Some(directives) = maybe_directives {
679+
filter
680+
.modify(|it| *it = EnvFilter::new(directives))
681+
.expect("error reloading `EnvFilter` for console_logger");
682+
}
683+
}

src/cli/setup_mode.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use tracing_subscriber::{reload::Handle, EnvFilter, Registry};
77

88
use crate::{
99
cli::{
10-
common,
10+
common::{self, update_console_filter},
1111
self_update::{self, InstallOpts},
1212
},
1313
dist::Profile,
@@ -115,18 +115,7 @@ pub async fn main(
115115
warn!("{}", common::WARN_COMPLETE_PROFILE);
116116
}
117117

118-
if process.var("RUSTUP_LOG").is_err() {
119-
if quiet {
120-
console_filter
121-
.modify(|it| *it = EnvFilter::new("rustup=WARN"))
122-
.expect("error reloading `EnvFilter` for console_logger");
123-
}
124-
if verbose {
125-
console_filter
126-
.modify(|it| *it = EnvFilter::new("rustup=DEBUG"))
127-
.expect("error reloading `EnvFilter` for console_logger");
128-
}
129-
}
118+
update_console_filter(process, &console_filter, quiet, verbose);
130119

131120
let opts = InstallOpts {
132121
default_host_triple: default_host,

0 commit comments

Comments
 (0)