diff options
author | Joel Galenson <jgalenson@google.com> | 2020-06-30 12:49:57 -0700 |
---|---|---|
committer | Joel Galenson <jgalenson@google.com> | 2020-06-30 12:49:57 -0700 |
commit | 02ba2071272341f87f05e71251842c538d6fe31c (patch) | |
tree | ec2e548fb58c92f28f54353c6e049c10bb1ab864 /src/fmt/writer/termcolor/shim_impl.rs | |
parent | 6c54a2c93829eae2422cb6215fbdc3eca9efb48d (diff) | |
download | env_logger-02ba2071272341f87f05e71251842c538d6fe31c.tar.gz |
Import env_logger-0.7.1
Change-Id: I26daac7e33bfb65bcb57e1eee8b61f76e5e84dea
Diffstat (limited to 'src/fmt/writer/termcolor/shim_impl.rs')
-rw-r--r-- | src/fmt/writer/termcolor/shim_impl.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/fmt/writer/termcolor/shim_impl.rs b/src/fmt/writer/termcolor/shim_impl.rs new file mode 100644 index 0000000..563f8ad --- /dev/null +++ b/src/fmt/writer/termcolor/shim_impl.rs @@ -0,0 +1,63 @@ +use std::io; + +use crate::fmt::{Target, WriteStyle}; + +pub(in crate::fmt::writer) mod glob {} + +pub(in crate::fmt::writer) struct BufferWriter { + target: Target, +} + +pub(in crate::fmt) struct Buffer(Vec<u8>); + +impl BufferWriter { + pub(in crate::fmt::writer) fn stderr(_is_test: bool, _write_style: WriteStyle) -> Self { + BufferWriter { + target: Target::Stderr, + } + } + + pub(in crate::fmt::writer) fn stdout(_is_test: bool, _write_style: WriteStyle) -> Self { + BufferWriter { + target: Target::Stdout, + } + } + + pub(in crate::fmt::writer) fn buffer(&self) -> Buffer { + Buffer(Vec::new()) + } + + 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); + + match self.target { + Target::Stderr => eprint!("{}", log), + Target::Stdout => print!("{}", log), + } + + Ok(()) + } +} + +impl Buffer { + pub(in crate::fmt) fn clear(&mut self) { + self.0.clear(); + } + + pub(in crate::fmt) fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + self.0.extend(buf); + Ok(buf.len()) + } + + pub(in crate::fmt) fn flush(&mut self) -> io::Result<()> { + Ok(()) + } + + #[cfg(test)] + pub(in crate::fmt) fn bytes(&self) -> &[u8] { + &self.0 + } +} |