Skip to content

Commit 7b04b6d

Browse files
Merge #968
968: introduce verbosity levels r=Alexhuszagh a=Emilgardis this will make it so rustup doesn't show verbose unless `-vv` or `-v` and `CROSS_DEBUG` is used. Co-authored-by: Emil Gardström <emil.gardstrom@gmail.com>
2 parents 8728c84 + 931bcbe commit 7b04b6d

File tree

11 files changed

+61
-35
lines changed

11 files changed

+61
-35
lines changed

.changes/968.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"description": "only print rustup --verbose if `-vv` or `CROSS_VERBOSE=1` is used",
3+
"type": "fixed"
4+
}

src/bin/cross.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ pub fn main() -> cross::Result<()> {
1818
let target_list = rustc::target_list(&mut Verbosity::Quiet.into())?;
1919
let args = cli::parse(&target_list)?;
2020
let subcommand = args.subcommand;
21-
let mut msg_info = shell::MessageInfo::create(args.verbose, args.quiet, args.color.as_deref())?;
21+
let mut msg_info = shell::MessageInfo::create(
22+
args.verbose + cross::config::bool_from_envvar("CROSS_DEBUG") as u8,
23+
args.quiet,
24+
args.color.as_deref(),
25+
)?;
2226
let status = match cross::run(args, target_list, &mut msg_info)? {
2327
Some(status) => status,
2428
None => {

src/cli.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct Args {
1818
pub target_dir: Option<PathBuf>,
1919
pub manifest_path: Option<PathBuf>,
2020
pub version: bool,
21-
pub verbose: bool,
21+
pub verbose: u8,
2222
pub quiet: bool,
2323
pub color: Option<String>,
2424
}
@@ -61,16 +61,22 @@ pub fn fmt_subcommands(stdout: &str, msg_info: &mut MessageInfo) -> Result<()> {
6161
Ok(())
6262
}
6363

64-
fn is_verbose(arg: &str) -> bool {
64+
fn is_verbose(arg: &str) -> u8 {
6565
match arg {
66-
"--verbose" => true,
66+
"--verbose" => 1,
6767
// cargo can handle any number of "v"s
6868
a => {
69-
a.starts_with('-')
69+
if a.starts_with('-')
7070
&& a.len() >= 2
7171
&& a.get(1..)
7272
.map(|a| a.chars().all(|x| x == 'v'))
7373
.unwrap_or_default()
74+
{
75+
// string must be of form `-v[v]*` here
76+
a.len() as u8 - 1
77+
} else {
78+
0
79+
}
7480
}
7581
}
7682
}
@@ -160,7 +166,7 @@ pub fn parse(target_list: &TargetList) -> Result<Args> {
160166
let mut all: Vec<String> = Vec::new();
161167
let mut version = false;
162168
let mut quiet = false;
163-
let mut verbose = false;
169+
let mut verbose = 0;
164170
let mut color = None;
165171

166172
{
@@ -169,8 +175,8 @@ pub fn parse(target_list: &TargetList) -> Result<Args> {
169175
if arg.is_empty() {
170176
continue;
171177
}
172-
if is_verbose(arg.as_str()) {
173-
verbose = true;
178+
if let v @ 1.. = is_verbose(arg.as_str()) {
179+
verbose += v;
174180
all.push(arg);
175181
} else if matches!(arg.as_str(), "--version" | "-V") {
176182
version = true;
@@ -275,13 +281,13 @@ mod tests {
275281

276282
#[test]
277283
fn is_verbose_test() {
278-
assert!(!is_verbose("b"));
279-
assert!(!is_verbose("x"));
280-
assert!(!is_verbose("-"));
281-
assert!(!is_verbose("-V"));
282-
assert!(is_verbose("-v"));
283-
assert!(is_verbose("--verbose"));
284-
assert!(is_verbose("-vvvv"));
285-
assert!(!is_verbose("-version"));
284+
assert!(is_verbose("b") == 0);
285+
assert!(is_verbose("x") == 0);
286+
assert!(is_verbose("-") == 0);
287+
assert!(is_verbose("-V") == 0);
288+
assert!(is_verbose("-v") == 1);
289+
assert!(is_verbose("--verbose") == 1);
290+
assert!(is_verbose("-vvvv") == 4);
291+
assert!(is_verbose("-version") == 0);
286292
}
287293
}

src/rustc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ mod tests {
367367
"bisector-nightly-2022-04-26-x86_64-unknown-linux-gnu",
368368
"/tmp/cross/sysroot".as_ref(),
369369
&crate::config::Config::new(None),
370-
&mut MessageInfo::create(true, false, None).unwrap(),
370+
&mut MessageInfo::create(2, false, None).unwrap(),
371371
)
372372
.unwrap();
373373
}

src/rustup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn rustup_command(msg_info: &mut MessageInfo, no_flags: bool) -> Command {
3939
Verbosity::Quiet => {
4040
cmd.arg("--quiet");
4141
}
42-
Verbosity::Verbose => {
42+
Verbosity::Verbose(2..) => {
4343
cmd.arg("--verbose");
4444
}
4545
_ => (),

src/shell.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,33 @@ macro_rules! status {
6969
pub enum Verbosity {
7070
Quiet,
7171
Normal,
72-
Verbose,
72+
Verbose(u8),
7373
}
7474

7575
impl Verbosity {
7676
pub fn verbose(self) -> bool {
7777
match self {
78-
Self::Verbose => true,
78+
Self::Verbose(..) => true,
7979
Self::Normal | Self::Quiet => false,
8080
}
8181
}
8282

83-
fn create(color_choice: ColorChoice, verbose: bool, quiet: bool) -> Option<Self> {
84-
match (verbose, quiet) {
85-
(true, true) => {
83+
#[must_use]
84+
pub fn level(&self) -> u8 {
85+
match &self {
86+
Verbosity::Verbose(v) => *v,
87+
_ => 0,
88+
}
89+
}
90+
91+
fn create(color_choice: ColorChoice, verbose: impl Into<u8>, quiet: bool) -> Option<Self> {
92+
match (verbose.into(), quiet) {
93+
(1.., true) => {
8694
MessageInfo::from(color_choice).fatal("cannot set both --verbose and --quiet", 101)
8795
}
88-
(true, false) => Some(Verbosity::Verbose),
89-
(false, true) => Some(Verbosity::Quiet),
90-
(false, false) => None,
96+
(v @ 1.., false) => Some(Verbosity::Verbose(v)),
97+
(0, true) => Some(Verbosity::Quiet),
98+
(0, false) => None,
9199
}
92100
}
93101
}
@@ -143,7 +151,7 @@ impl MessageInfo {
143151
}
144152
}
145153

146-
pub fn create(verbose: bool, quiet: bool, color: Option<&str>) -> Result<MessageInfo> {
154+
pub fn create(verbose: impl Into<u8>, quiet: bool, color: Option<&str>) -> Result<MessageInfo> {
147155
let color_choice = get_color_choice(color)?;
148156
let verbosity = get_verbosity(color_choice, verbose, quiet)?;
149157

@@ -178,7 +186,7 @@ impl MessageInfo {
178186
}
179187

180188
pub fn as_verbose<T, C: Fn(&mut MessageInfo) -> T>(&mut self, call: C) -> T {
181-
self.as_verbosity(call, Verbosity::Verbose)
189+
self.as_verbosity(call, Verbosity::Verbose(2))
182190
}
183191

184192
fn erase_line<S: Stream + Write>(&mut self, stream: &mut S) -> Result<()> {
@@ -397,7 +405,11 @@ fn get_color_choice(color: Option<&str>) -> Result<ColorChoice> {
397405
})
398406
}
399407

400-
fn get_verbosity(color_choice: ColorChoice, verbose: bool, quiet: bool) -> Result<Verbosity> {
408+
fn get_verbosity(
409+
color_choice: ColorChoice,
410+
verbose: impl Into<u8>,
411+
quiet: bool,
412+
) -> Result<Verbosity> {
401413
// cargo always checks the value of these variables.
402414
let env_verbose = cargo_envvar_bool("CARGO_TERM_VERBOSE")?;
403415
let env_quiet = cargo_envvar_bool("CARGO_TERM_QUIET")?;

src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static WORKSPACE: OnceCell<PathBuf> = OnceCell::new();
1515
/// Returns the cargo workspace for the manifest
1616
pub fn get_cargo_workspace() -> &'static Path {
1717
let manifest_dir = env!("CARGO_MANIFEST_DIR");
18-
let mut msg_info = crate::shell::Verbosity::Verbose.into();
18+
let mut msg_info = crate::shell::Verbosity::Verbose(2).into();
1919
#[allow(clippy::unwrap_used)]
2020
WORKSPACE.get_or_init(|| {
2121
crate::cargo_metadata_with_args(Some(manifest_dir.as_ref()), None, &mut msg_info)

xtask/src/build_docker_image.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub fn build_docker_image(
118118
msg_info: &mut MessageInfo,
119119
) -> cross::Result<()> {
120120
let verbose = match verbose {
121-
0 => msg_info.is_verbose() as u8,
121+
0 => msg_info.verbosity.level(),
122122
v => v,
123123
};
124124
let metadata = cargo_metadata(msg_info)?;

xtask/src/ci.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub fn ci(args: CiJob, metadata: CargoMetadata) -> cross::Result<()> {
9090
let search = cargo_command()
9191
.args(&["search", "--limit", "1"])
9292
.arg("cross")
93-
.run_and_get_stdout(&mut Verbosity::Verbose.into())?;
93+
.run_and_get_stdout(&mut Verbosity::Verbose(2).into())?;
9494
let (cross, rest) = search
9595
.split_once(" = ")
9696
.ok_or_else(|| eyre::eyre!("cargo search failed"))?;

xtask/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub fn main() -> cross::Result<()> {
115115
hooks::test(cli.toolchain.as_deref(), &mut msg_info)?;
116116
}
117117
Commands::CiJob(args) => {
118-
let metadata = cargo_metadata(&mut Verbosity::Verbose.into())?;
118+
let metadata = cargo_metadata(&mut Verbosity::Verbose(2).into())?;
119119
ci::ci(args, metadata)?;
120120
}
121121
Commands::ConfigureCrosstool(args) => {

0 commit comments

Comments
 (0)