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 eprint! and eprintln! #39228

Closed
zackw opened this issue Jan 21, 2017 · 3 comments
Closed

Add eprint! and eprintln! #39228

zackw opened this issue Jan 21, 2017 · 3 comments

Comments

@zackw
Copy link
Contributor

zackw commented Jan 21, 2017

From https://internals.rust-lang.org/t/extremely-pre-rfc-eprintln/4635

if you are writing command-line programs: the easiest way to emit non-fatal error messages is println!, but that sends them to stdout, which is not where error messages should go. panic! does the right thing (and thus also unwrap, expect, assert!, etc) but those are all (a) fatal and (b) properly used only for conditions that indicate a bug. The most straightforward way to write diagnostics to stderr is

writeln!(io::stderr(), "out of cheese error").unwrap();

which is significantly more typing in itself, requires you to use std::io::Write, and doesn't have println!'s defensive measures against being called at an awkward moment.

The simplest thing we could possibly add to fix this problem would be an eprintln! macro, which would be exactly the same as println! except that the output goes to stderr instead of stdout.

All of the documentation should also be updated to make it clear that it is inappropriate to use println! for error messages.

… which met with general approval and two people saying they already were adding eprintln! to all their own crates, so I'm just gonna go ahead and submit a PR. eprint! is included for completeness and symmetry; I'm not sure it's useful, but I think people would be surprised to find it missing.

zackw added a commit to zackw/rust that referenced this issue Jan 21, 2017
These are exactly the same as `print!` and `println!` except that
they write to stderr instead of stdout.  Issue rust-lang#39228.
@petrochenkov
Copy link
Contributor

I'd suggest to just implement this and send a PR (in the worst case you'll need to write an RFC as well).
This was suggested many times in various places, but nobody pushed hard enough and took responsibility to actually get this implemented and merged. I encourage you to do it.
The number of likes this suggestion gathered on internals in one day shows that the feature is clearly wanted.

@steveklabnik
Copy link
Member

This does require an RFC, as I mentioned in #39229, so I'm going to give this a close; this would be tracked by the RFC process. Thanks!

zackw added a commit to zackw/rust that referenced this issue May 10, 2017
These are exactly the same as `print!` and `println!` except that
they write to stderr instead of stdout.  Issue rust-lang#39228.
frewsxcv added a commit to frewsxcv/rust that referenced this issue May 11, 2017
Add `eprint!` and `eprintln!` macros to the prelude.

These are exactly the same as `print!` and `println!` except that they write to stderr instead of stdout.  Issues rust-lang#39228 and rust-lang#40528; previous PR rust-lang#39229; accepted RFC rust-lang/rfcs#1869; proposed revision to The Book rust-lang/book#615.

I have _not_ revised this any since the original submission; I will do that later this week.  I wanted to get this PR in place since it's been quite a while since the RFC was merged.

Known outstanding review comments:

* [x] @steveklabnik requested a new chapter for the unstable version of The Book -- please see if the proposed revisions to the second edition cover it.
* [x] @nodakai asked if it were possible to merge the internal methods `_print` and `_eprint` - not completely, since they both refer to different internal globals which we don't want to expose, but I will see if some duplication can be factored out.

Please let me know if I missed anything.
frewsxcv added a commit to frewsxcv/rust that referenced this issue May 11, 2017
Add `eprint!` and `eprintln!` macros to the prelude.

These are exactly the same as `print!` and `println!` except that they write to stderr instead of stdout.  Issues rust-lang#39228 and rust-lang#40528; previous PR rust-lang#39229; accepted RFC rust-lang/rfcs#1869; proposed revision to The Book rust-lang/book#615.

I have _not_ revised this any since the original submission; I will do that later this week.  I wanted to get this PR in place since it's been quite a while since the RFC was merged.

Known outstanding review comments:

* [x] @steveklabnik requested a new chapter for the unstable version of The Book -- please see if the proposed revisions to the second edition cover it.
* [x] @nodakai asked if it were possible to merge the internal methods `_print` and `_eprint` - not completely, since they both refer to different internal globals which we don't want to expose, but I will see if some duplication can be factored out.

Please let me know if I missed anything.
@musically-ut
Copy link

If you are still running into this error, update Rust to version >= 1.21.0.


I spent a while googling this error and wondering which package providing eprintln! was old before realizing that this PR had eventually been merged in.

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

No branches or pull requests

4 participants