From 13a0b4f3ccfe29ad299a17d95d2957a1857a5c2e 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 | 15 ++++++--------- src/fmt/writer/termcolor/shim_impl.rs | 14 ++++++-------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/fmt/writer/termcolor/extern_impl.rs b/src/fmt/writer/termcolor/extern_impl.rs index 4324a45b..8f0d974c 100644 --- a/src/fmt/writer/termcolor/extern_impl.rs +++ b/src/fmt/writer/termcolor/extern_impl.rs @@ -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_all(buf.bytes()), + Target::Stdout => io::stdout().write_all(buf.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..cd381188 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_all(&buf.0), + Target::Stdout => io::stdout().write_all(&buf.0), } - - Ok(()) + .map(|_| ()) } }