|
1 | 1 | mod atty; |
2 | 2 | mod buffer; |
| 3 | +mod target; |
3 | 4 |
|
4 | 5 | use self::atty::{is_stderr, is_stdout}; |
5 | 6 | use self::buffer::BufferWriter; |
6 | 7 | use std::{fmt, io, mem, sync::Mutex}; |
7 | 8 |
|
8 | | -pub(super) mod glob { |
9 | | - pub use super::*; |
10 | | -} |
11 | | - |
12 | 9 | pub(super) use self::buffer::Buffer; |
13 | 10 |
|
14 | | -/// Log target, either `stdout`, `stderr` or a custom pipe. |
15 | | -#[non_exhaustive] |
16 | | -pub enum Target { |
17 | | - /// Logs will be sent to standard output. |
18 | | - Stdout, |
19 | | - /// Logs will be sent to standard error. |
20 | | - Stderr, |
21 | | - /// Logs will be sent to a custom pipe. |
22 | | - Pipe(Box<dyn io::Write + Send + 'static>), |
23 | | -} |
24 | | - |
25 | | -impl Default for Target { |
26 | | - fn default() -> Self { |
27 | | - Target::Stderr |
28 | | - } |
29 | | -} |
30 | | - |
31 | | -impl fmt::Debug for Target { |
32 | | - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
33 | | - write!( |
34 | | - f, |
35 | | - "{}", |
36 | | - match self { |
37 | | - Self::Stdout => "stdout", |
38 | | - Self::Stderr => "stderr", |
39 | | - Self::Pipe(_) => "pipe", |
40 | | - } |
41 | | - ) |
42 | | - } |
43 | | -} |
44 | | - |
45 | | -/// Log target, either `stdout`, `stderr` or a custom pipe. |
46 | | -/// |
47 | | -/// Same as `Target`, except the pipe is wrapped in a mutex for interior mutability. |
48 | | -pub(super) enum WritableTarget { |
49 | | - /// Logs will be written to standard output. |
50 | | - #[allow(dead_code)] |
51 | | - WriteStdout, |
52 | | - /// Logs will be printed to standard output. |
53 | | - PrintStdout, |
54 | | - /// Logs will be written to standard error. |
55 | | - #[allow(dead_code)] |
56 | | - WriteStderr, |
57 | | - /// Logs will be printed to standard error. |
58 | | - PrintStderr, |
59 | | - /// Logs will be sent to a custom pipe. |
60 | | - Pipe(Box<Mutex<dyn io::Write + Send + 'static>>), |
61 | | -} |
| 11 | +pub use target::Target; |
| 12 | +use target::WritableTarget; |
62 | 13 |
|
63 | | -impl WritableTarget { |
64 | | - fn print(&self, buf: &Buffer) -> io::Result<()> { |
65 | | - use std::io::Write as _; |
66 | | - |
67 | | - let buf = buf.as_bytes(); |
68 | | - match self { |
69 | | - WritableTarget::WriteStdout => { |
70 | | - let stream = std::io::stdout(); |
71 | | - let mut stream = stream.lock(); |
72 | | - stream.write_all(buf)?; |
73 | | - stream.flush()?; |
74 | | - } |
75 | | - WritableTarget::PrintStdout => print!("{}", String::from_utf8_lossy(buf)), |
76 | | - WritableTarget::WriteStderr => { |
77 | | - let stream = std::io::stderr(); |
78 | | - let mut stream = stream.lock(); |
79 | | - stream.write_all(buf)?; |
80 | | - stream.flush()?; |
81 | | - } |
82 | | - WritableTarget::PrintStderr => eprint!("{}", String::from_utf8_lossy(buf)), |
83 | | - // Safety: If the target type is `Pipe`, `target_pipe` will always be non-empty. |
84 | | - WritableTarget::Pipe(pipe) => { |
85 | | - let mut stream = pipe.lock().unwrap(); |
86 | | - stream.write_all(buf)?; |
87 | | - stream.flush()?; |
88 | | - } |
89 | | - } |
90 | | - |
91 | | - Ok(()) |
92 | | - } |
93 | | -} |
94 | | - |
95 | | -impl fmt::Debug for WritableTarget { |
96 | | - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
97 | | - write!( |
98 | | - f, |
99 | | - "{}", |
100 | | - match self { |
101 | | - Self::WriteStdout => "stdout", |
102 | | - Self::PrintStdout => "stdout", |
103 | | - Self::WriteStderr => "stderr", |
104 | | - Self::PrintStderr => "stderr", |
105 | | - Self::Pipe(_) => "pipe", |
106 | | - } |
107 | | - ) |
108 | | - } |
109 | | -} |
110 | 14 | /// Whether or not to print styles to the target. |
111 | 15 | #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] |
112 | 16 | pub enum WriteStyle { |
|
0 commit comments