Skip to content

Commit

Permalink
Don't use eprint from logger implementations.
Browse files Browse the repository at this point in the history
eprint! panics on io errors, which might cause unnecessary crashes like
https://bugzilla.mozilla.org/show_bug.cgi?id=1676882.
  • Loading branch information
emilio committed Nov 13, 2020
1 parent 26e821b commit 8378447
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
15 changes: 6 additions & 9 deletions src/fmt/writer/termcolor/extern_impl.rs
Expand Up @@ -102,17 +102,14 @@ impl BufferWriter {

pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
if let Some(target) = self.test_target {
// This impl uses the `eprint` and `print` macros
// instead of `termcolor`'s buffer.
// This is so their output can be captured by `cargo test`
let log = String::from_utf8_lossy(buf.bytes());

// This impl writes directly to stderr / stdout instead of using
// `termcolor`'s buffer. This is so their output can be captured by
// `cargo test`
match target {
Target::Stderr => eprint!("{}", log),
Target::Stdout => print!("{}", log),
Target::Stderr => io::stderr().write(buf.bytes()),
Target::Stdout => io::stdout().write(buf.bytes()),
}

Ok(())
.map(|_| ())
} else {
self.inner.print(&buf.inner)
}
Expand Down
14 changes: 6 additions & 8 deletions src/fmt/writer/termcolor/shim_impl.rs
Expand Up @@ -28,17 +28,15 @@ impl BufferWriter {
}

pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
// This impl uses the `eprint` and `print` macros
// instead of using the streams directly.
// This is so their output can be captured by `cargo test`
let log = String::from_utf8_lossy(&buf.0);
use std::io::Write;

// This impl writes to stdout / stderr instead of using the streams
// directly. This is so their output can be captured by `cargo test`
match self.target {
Target::Stderr => eprint!("{}", log),
Target::Stdout => print!("{}", log),
Target::Stderr => io::stderr().write(&buf.0),
Target::Stdout => io::stdout().write(&buf.0),
}

Ok(())
.map(|_| ())
}
}

Expand Down

0 comments on commit 8378447

Please sign in to comment.