env_logger/fmt/writer/termcolor/
shim_impl.rs
1use std::{io, sync::Mutex};
2
3use crate::fmt::{WritableTarget, WriteStyle};
4
5pub(in crate::fmt::writer) mod glob {}
6
7pub(in crate::fmt::writer) struct BufferWriter {
8 target: WritableTarget,
9}
10
11pub(in crate::fmt) struct Buffer(Vec<u8>);
12
13impl BufferWriter {
14 pub(in crate::fmt::writer) fn stderr(_is_test: bool, _write_style: WriteStyle) -> Self {
15 BufferWriter {
16 target: WritableTarget::Stderr,
17 }
18 }
19
20 pub(in crate::fmt::writer) fn stdout(_is_test: bool, _write_style: WriteStyle) -> Self {
21 BufferWriter {
22 target: WritableTarget::Stdout,
23 }
24 }
25
26 pub(in crate::fmt::writer) fn pipe(
27 _is_test: bool,
28 _write_style: WriteStyle,
29 pipe: Box<Mutex<dyn io::Write + Send + 'static>>,
30 ) -> Self {
31 BufferWriter {
32 target: WritableTarget::Pipe(pipe),
33 }
34 }
35
36 pub(in crate::fmt::writer) fn buffer(&self) -> Buffer {
37 Buffer(Vec::new())
38 }
39
40 pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
41 match &self.target {
45 WritableTarget::Pipe(pipe) => pipe.lock().unwrap().write_all(&buf.0)?,
47 WritableTarget::Stdout => print!("{}", String::from_utf8_lossy(&buf.0)),
48 WritableTarget::Stderr => eprint!("{}", String::from_utf8_lossy(&buf.0)),
49 }
50
51 Ok(())
52 }
53}
54
55impl Buffer {
56 pub(in crate::fmt) fn clear(&mut self) {
57 self.0.clear();
58 }
59
60 pub(in crate::fmt) fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
61 self.0.extend(buf);
62 Ok(buf.len())
63 }
64
65 pub(in crate::fmt) fn flush(&mut self) -> io::Result<()> {
66 Ok(())
67 }
68
69 #[cfg(test)]
70 pub(in crate::fmt) fn bytes(&self) -> &[u8] {
71 &self.0
72 }
73}