-
Notifications
You must be signed in to change notification settings - Fork 13k
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
io::Stderr should be line-buffered by default, not unbuffered #64413
Comments
FWIW, C++ #include <iostream>
using namespace std;
int main() {
cerr << "partial ";
cerr << "line" << endl;
cerr << "a one and a two and a " << 3 << " and a " << 4.0 << endl;
return 0;
}
The only difference is that In C, it does appear to buffer the format string before writing: #include <stdio.h>
int main() {
fprintf(stderr, "partial ");
fprintf(stderr, "line\n");
fprintf(stderr, "a one and a two and a %d and a %f\n", 3, 4.0);
return 0;
}
|
@cuviper Yeah, I guess what I'm saying is I think the C behavior is preferable to the C++ behavior. |
Since `stderr` is unbuffered, writing out json messages actually take up about ~10%/0.1s of the runtime of the `inflate` benchmark. cc rust-lang#64413
perf: Buffer stderr when writing json errors/warnings Since `stderr` is unbuffered, writing out json messages actually take up about ~10%/0.1s of the runtime of the `inflate` benchmark as it generates a fair number of warnings. cc #64413
It's worth noting, though, that C++ has both |
|
stderr is unbuffered in C:
|
std::io::stderr
does no buffering at all by default, which means that a Rust program that uses the most obvious way to print error messages (eprintln!
) will make individualwrite
syscalls not only for each call to the macro, but for each subcomponent of the formatted string:This behavior is undesirable in any context where multiple programs might be emitting error messages to the same terminal, logfile, or whatever at the same time (for instance,
make -j
) because partial lines from different programs can get mixed up with each other. If stderr instead buffered up a full line and wrote it to the OS all at once, then different programs' output could only get mixed together on a line-by-line basis, which is usually much less confusing for a person reading the logs.This behavior is also troublesome for programs that incrementally parse stderr output; for instance, it may be the reason why emacs' compilation-mode occasionally doesn't detect all of the diagnostics in
cargo build
output (I don't know if there's an existing bug report for this).(There is a strong case for having
eprint!
flush out the partial line that it generates, but that could be handled inside ofeprint!
.)(This is closely related to, but not the same as, #60673, which is about stdout. Block buffering is not normally appropriate for
stderr
, even when writing to a file.)The text was updated successfully, but these errors were encountered: