Skip to content

Commit 9719c68

Browse files
authored
Merge pull request #297 from epage/refactor
refactor(fmt): More anstream prep
2 parents 7929b7f + f4808e4 commit 9719c68

File tree

6 files changed

+104
-107
lines changed

6 files changed

+104
-107
lines changed

src/fmt/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,11 @@ pub use style::{Color, Style, StyledValue};
5050

5151
#[cfg(feature = "humantime")]
5252
pub use self::humantime::Timestamp;
53-
pub use self::writer::glob::*;
53+
pub use self::writer::Target;
54+
pub use self::writer::WriteStyle;
5455

5556
use self::writer::{Buffer, Writer};
5657

57-
pub(crate) mod glob {
58-
pub use super::{Target, TimestampPrecision, WriteStyle};
59-
}
60-
6158
/// Formatting precision of timestamps.
6259
///
6360
/// Seconds give precision of full seconds, milliseconds give thousands of a

src/fmt/writer/buffer/plain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{io, sync::Mutex};
22

3-
use crate::fmt::{WritableTarget, WriteStyle};
3+
use crate::fmt::writer::{WritableTarget, WriteStyle};
44

55
pub(in crate::fmt::writer) struct BufferWriter {
66
target: WritableTarget,

src/fmt/writer/buffer/termcolor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Mutex;
33

44
use termcolor::{self, ColorSpec, WriteColor};
55

6-
use crate::fmt::{WritableTarget, WriteStyle};
6+
use crate::fmt::writer::{WritableTarget, WriteStyle};
77

88
pub(in crate::fmt::writer) struct BufferWriter {
99
inner: termcolor::BufferWriter,

src/fmt/writer/mod.rs

Lines changed: 3 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,16 @@
11
mod atty;
22
mod buffer;
3+
mod target;
34

45
use self::atty::{is_stderr, is_stdout};
56
use self::buffer::BufferWriter;
67
use std::{fmt, io, mem, sync::Mutex};
78

8-
pub(super) mod glob {
9-
pub use super::*;
10-
}
11-
129
pub(super) use self::buffer::Buffer;
1310

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;
6213

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-
}
11014
/// Whether or not to print styles to the target.
11115
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
11216
pub enum WriteStyle {

src/fmt/writer/target.rs

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

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,5 +280,5 @@ mod logger;
280280
pub mod filter;
281281
pub mod fmt;
282282

283-
pub use self::fmt::glob::*;
283+
pub use self::fmt::{Target, TimestampPrecision, WriteStyle};
284284
pub use self::logger::*;

0 commit comments

Comments
 (0)