]> git.proxmox.com Git - rustc.git/blob - vendor/env_logger/src/fmt/writer/termcolor/shim_impl.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / vendor / env_logger / src / fmt / writer / termcolor / shim_impl.rs
1 use std::{io, sync::Mutex};
2
3 use crate::fmt::{WritableTarget, WriteStyle};
4
5 pub(in crate::fmt::writer) mod glob {}
6
7 pub(in crate::fmt::writer) struct BufferWriter {
8 target: WritableTarget,
9 }
10
11 pub(in crate::fmt) struct Buffer(Vec<u8>);
12
13 impl 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 _write_style: WriteStyle,
28 pipe: Box<Mutex<dyn io::Write + Send + 'static>>,
29 ) -> Self {
30 BufferWriter {
31 target: WritableTarget::Pipe(pipe),
32 }
33 }
34
35 pub(in crate::fmt::writer) fn buffer(&self) -> Buffer {
36 Buffer(Vec::new())
37 }
38
39 pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
40 // This impl uses the `eprint` and `print` macros
41 // instead of using the streams directly.
42 // This is so their output can be captured by `cargo test`.
43 match &self.target {
44 // Safety: If the target type is `Pipe`, `target_pipe` will always be non-empty.
45 WritableTarget::Pipe(pipe) => pipe.lock().unwrap().write_all(&buf.0)?,
46 WritableTarget::Stdout => print!("{}", String::from_utf8_lossy(&buf.0)),
47 WritableTarget::Stderr => eprint!("{}", String::from_utf8_lossy(&buf.0)),
48 }
49
50 Ok(())
51 }
52 }
53
54 impl Buffer {
55 pub(in crate::fmt) fn clear(&mut self) {
56 self.0.clear();
57 }
58
59 pub(in crate::fmt) fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
60 self.0.extend(buf);
61 Ok(buf.len())
62 }
63
64 pub(in crate::fmt) fn flush(&mut self) -> io::Result<()> {
65 Ok(())
66 }
67
68 #[cfg(test)]
69 pub(in crate::fmt) fn bytes(&self) -> &[u8] {
70 &self.0
71 }
72 }