From a8d3949db837319a56494aea76c90e943e03fc5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 13 Nov 2020 11:41:43 +0100 Subject: [PATCH] Don't use eprint from logger implementations. eprint! panics on io errors, which might cause unnecessary crashes like https://bugzilla.mozilla.org/show_bug.cgi?id=1676882. --- src/fmt/writer/termcolor/extern_impl.rs | 13 ++++++------- src/fmt/writer/termcolor/shim_impl.rs | 14 ++++++-------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/fmt/writer/termcolor/extern_impl.rs b/src/fmt/writer/termcolor/extern_impl.rs index 4324a45b..bf46630b 100644 --- a/src/fmt/writer/termcolor/extern_impl.rs +++ b/src/fmt/writer/termcolor/extern_impl.rs @@ -102,17 +102,16 @@ 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` + // This impl writes directly to stderr / stdout instead of using + // `termcolor`'s buffer. This is so their output can be captured by + // `cargo test` let log = String::from_utf8_lossy(buf.bytes()); match target { - Target::Stderr => eprint!("{}", log), - Target::Stdout => print!("{}", log), + Target::Stderr => io::stderr().write(log.as_bytes()), + Target::Stdout => io::stdout().write(log.as_bytes()), } - - Ok(()) + .map(|_| ()) } else { self.inner.print(&buf.inner) } diff --git a/src/fmt/writer/termcolor/shim_impl.rs b/src/fmt/writer/termcolor/shim_impl.rs index 563f8ad4..aac385ad 100644 --- a/src/fmt/writer/termcolor/shim_impl.rs +++ b/src/fmt/writer/termcolor/shim_impl.rs @@ -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(|_| ()) } }