Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a -s/--silent option to silence user-facing messages #362

Merged
merged 2 commits into from
Jan 15, 2025

Conversation

tgross35
Copy link
Contributor

@tgross35 tgross35 commented Jan 14, 2025

I have a usecase where it would be nice to use cargo-show-asm programmatically, since it does a nicer job of filtering and demangling than most builtin utilities. However, UI messages currently print to stdout, meaning they need to be manually filtered:

$ cargo asm -p libm --lib 2>/dev/null | cat
Try one of those by name or a sequence number
  0 "<f32 as libm::math::support::float_traits::Float>::abs" [9]
  1 "<f32 as libm::math::support::float_traits::Float>::copysign" [18]
  2 "<f32 as libm::math::support::float_traits::Float>::normalize" [21]
  3 "<f64 as libm::math::support::float_traits::Float>::abs" [9]

Introduce a -s/--silent flag that reduces the verbosity level, effectively being the the opposite of -v:

$ path/cargo-asm asm -p libm --lib -s 2>/dev/null | cat
  0 "<f32 as libm::math::support::float_traits::Float>::abs" [9]
  1 "<f32 as libm::math::support::float_traits::Float>::copysign" [18]
  2 "<f32 as libm::math::support::float_traits::Float>::normalize" [21]
  3 "<f64 as libm::math::support::float_traits::Float>::abs" [9]

-s was chosen to mirror Curl, since -q is already used as the flag to quieten Cargo's output.

@pacak
Copy link
Owner

pacak commented Jan 14, 2025

Hmmm... I intentionally made them stdout so piping to less or to a file produces something readable. For programmatic usage I'd rather add --quiet that would suppress anything non-essential. What do you think?

For that we'd change the default verbosity value to be 1, and make all those prints conditional when verbosity is at or above 1. New verbosity parser can look like this:

fn verbosity() -> impl Parser<usize> {
    let verbose = short('v')
        .long("verbose")
        .help("more verbose output, can be specified multiple times")
        .req_flag(())
        .count();
    let quiet = short('q')
        .long("quiet")
        .help("pls think of something reasonable")
        .req_flag(())
        .count();
    construct!(verbose, quiet).map(|(v, q)| (v + 1).saturating_sub(q))
}

@tgross35
Copy link
Contributor Author

Sounds reasonable to me 👍 I can do that

@tgross35
Copy link
Contributor Author

tgross35 commented Jan 14, 2025

Actually, any thoughts about having -q still print these messages but route them to stderr rather than stdout? They can still be useful, especially errors, so it would be nice for them to show up in logs but just not the tool-captured output. And then if somebody doesn't want these messages at all, they can just discard stderr.

@pacak
Copy link
Owner

pacak commented Jan 14, 2025

I think currently all the errors go to stderr - if not - that's a bug and -q shouldn't affect it.

@pacak
Copy link
Owner

pacak commented Jan 14, 2025

Some of the stuff that shows up with verbosity greater than zero are warnings and I think it's okay to keep them in stdout - to make debugging easier.

@tgross35 tgross35 force-pushed the diagnostics-to-stderr branch from 12b0af9 to 0d32d3b Compare January 14, 2025 23:48
@tgross35 tgross35 changed the title Print to stderr for user-facing messages Add a -s/--silent option to silence user-facing messages Jan 14, 2025
@tgross35 tgross35 force-pushed the diagnostics-to-stderr branch from 0d32d3b to 3a4ea46 Compare January 14, 2025 23:48
@tgross35
Copy link
Contributor Author

Alright, updated with a new flag to reduce verbosity. -q is already taken as the flag that quiets Cargo's output, so I picked -s like Curl uses. Let me know if you would prefer a different letter or if I should combine them somehow.

I think currently all the errors go to stderr - if not - that's a bug and -q shouldn't affect it.

There were a couple instances where this didn't seem to be the case, so added a patch that updates this.

Copy link
Owner

@pacak pacak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to drop one of the unused imports, but looks good other than that.

src/disasm.rs Outdated Show resolved Hide resolved
Replace uses of `safeprintln!` with `esafeprintln!` whenever a message
is printed before exiting.
@tgross35 tgross35 force-pushed the diagnostics-to-stderr branch from 3a4ea46 to 9539c01 Compare January 15, 2025 00:00
@tgross35
Copy link
Contributor Author

Some of the stuff that shows up with verbosity greater than zero are warnings and I think it's okay to keep them in stdout - to make debugging easier.

For what it's worth, this is somewhat less convenient for use via tools - if running cargo asm ... | grep some_symbol were to print warnings to stdout, they are lost to grep or, with -s, just never get emitted. So there isn't really a good way to be notified of warnings that might be related to the output. On the flip side, it is easier to combine stdout+stderr with cargo asm ... 2>&1 | less if you want to see everything in one place.

That being said, I understand this is primarily a user-friendly utility and use via scripting isn't really a common usecase, so the changes here work for me 👍 (I just want to be able to have CI automatically generate a diff of changed assembly for specific symbols).

@pacak
Copy link
Owner

pacak commented Jan 15, 2025

You need to run tests locally and commit changes to the readme

@tgross35 tgross35 force-pushed the diagnostics-to-stderr branch from 9539c01 to 594e89b Compare January 15, 2025 00:13
@tgross35
Copy link
Contributor Author

You need to run tests locally and commit changes to the readme

Sorry, thought I did that. Done.

@pacak
Copy link
Owner

pacak commented Jan 15, 2025

.args(std::iter::repeat("-v").take(format.verbosity))

Need to reduce this by 1 with saturating sub

I have a usecase where it would be nice to use cargo-show-asm
programmatically, since it does a nicer job of filtering and demangling
than most builtin utilities. However, UI messages currently print to
stdout, meaning they need to be manually filtered:

    $ cargo asm -p libm --lib 2>/dev/null | cat
    Try one of those by name or a sequence number
      0 "<f32 as libm::math::support::float_traits::Float>::abs" [9]
      1 "<f32 as libm::math::support::float_traits::Float>::copysign" [18]
      2 "<f32 as libm::math::support::float_traits::Float>::normalize" [21]
      3 "<f64 as libm::math::support::float_traits::Float>::abs" [9]

Introduce a `-s`/`--silent` flag that reduces the verbosity level,
effectively being the the opposite of `-v`:

    $ path/cargo-asm asm -p libm --lib -s 2>/dev/null | cat
      0 "<f32 as libm::math::support::float_traits::Float>::abs" [9]
      1 "<f32 as libm::math::support::float_traits::Float>::copysign" [18]
      2 "<f32 as libm::math::support::float_traits::Float>::normalize" [21]
      3 "<f64 as libm::math::support::float_traits::Float>::abs" [9]

`-s` was chosen to mirror Curl, since `-q` is already used as the flag
to quieten Cargo's output.
@tgross35 tgross35 force-pushed the diagnostics-to-stderr branch from 594e89b to a5b7922 Compare January 15, 2025 00:24
@tgross35
Copy link
Contributor Author

Makes sense, done.

@pacak pacak merged commit 2fb6a15 into pacak:master Jan 15, 2025
6 checks passed
@tgross35 tgross35 deleted the diagnostics-to-stderr branch January 15, 2025 00:44
@tgross35
Copy link
Contributor Author

Thank you for the quick review!

@pacak
Copy link
Owner

pacak commented Jan 15, 2025

Looks good, thank you for your contribution ❤️

I'll try to make a new release shortly.

While this tool started as a user facing - I'm not opposed to making it easier to use it from scripts, just need to decide what and how...

@pacak
Copy link
Owner

pacak commented Jan 15, 2025

0.2.46 is out

@tgross35
Copy link
Contributor Author

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants