]> git.proxmox.com Git - cargo.git/blob - vendor/env_logger/src/fmt/writer/termcolor/shim_impl.rs
New upstream version 0.35.0
[cargo.git] / vendor / env_logger / src / fmt / writer / termcolor / shim_impl.rs
1 use std::io;
2
3 use fmt::{WriteStyle, Target};
4
5 pub(in ::fmt::writer) mod glob {
6
7 }
8
9 pub(in ::fmt::writer) struct BufferWriter {
10 target: Target,
11 }
12
13 pub(in ::fmt) struct Buffer(Vec<u8>);
14
15 impl BufferWriter {
16 pub(in ::fmt::writer) fn stderr(_is_test: bool, _write_style: WriteStyle) -> Self {
17 BufferWriter {
18 target: Target::Stderr,
19 }
20 }
21
22 pub(in ::fmt::writer) fn stdout(_is_test: bool, _write_style: WriteStyle) -> Self {
23 BufferWriter {
24 target: Target::Stdout,
25 }
26 }
27
28 pub(in ::fmt::writer) fn buffer(&self) -> Buffer {
29 Buffer(Vec::new())
30 }
31
32 pub(in ::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
33 // This impl uses the `eprint` and `print` macros
34 // instead of using the streams directly.
35 // This is so their output can be captured by `cargo test`
36 let log = String::from_utf8_lossy(&buf.0);
37
38 match self.target {
39 Target::Stderr => eprint!("{}", log),
40 Target::Stdout => print!("{}", log),
41 }
42
43 Ok(())
44 }
45 }
46
47 impl Buffer {
48 pub(in ::fmt) fn clear(&mut self) {
49 self.0.clear();
50 }
51
52 pub(in ::fmt) fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
53 self.0.extend(buf);
54 Ok(buf.len())
55 }
56
57 pub(in ::fmt) fn flush(&mut self) -> io::Result<()> {
58 Ok(())
59 }
60
61 #[cfg(test)]
62 pub(in ::fmt) fn bytes(&self) -> &[u8] {
63 &self.0
64 }
65 }