Skip to content

Commit d87de90

Browse files
authored
Merge pull request #366 from epage/write
refactor: Pull out writer as a top-level concept
2 parents b8ecedc + d0061e3 commit d87de90

6 files changed

Lines changed: 25 additions & 24 deletions

File tree

src/fmt/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ use log::Record;
7171
mod humantime;
7272
#[cfg(feature = "kv")]
7373
mod kv;
74-
pub(crate) mod writer;
7574

7675
#[cfg(feature = "color")]
7776
pub use anstyle as style;
@@ -80,10 +79,10 @@ pub use anstyle as style;
8079
pub use self::humantime::Timestamp;
8180
#[cfg(feature = "kv")]
8281
pub use self::kv::*;
83-
pub use self::writer::Target;
84-
pub use self::writer::WriteStyle;
82+
pub use crate::writer::Target;
83+
pub use crate::writer::WriteStyle;
8584

86-
use self::writer::{Buffer, Writer};
85+
use crate::writer::{Buffer, Writer};
8786

8887
/// Formatting precision of timestamps.
8988
///
@@ -650,7 +649,7 @@ mod tests {
650649
}
651650

652651
fn formatter() -> Formatter {
653-
let writer = writer::Builder::new()
652+
let writer = crate::writer::Builder::new()
654653
.write_style(WriteStyle::Never)
655654
.build();
656655

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@
265265
#![warn(clippy::print_stdout)]
266266

267267
mod logger;
268+
mod writer;
268269

269270
pub mod fmt;
270271

src/logger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::{borrow::Cow, cell::RefCell, env, io};
33
use log::{LevelFilter, Log, Metadata, Record, SetLoggerError};
44

55
use crate::fmt;
6-
use crate::fmt::writer::{self, Writer};
76
use crate::fmt::{FormatFn, Formatter};
7+
use crate::writer::{self, Writer};
88

99
/// The default name for the environment variable to read filters from.
1010
pub const DEFAULT_FILTER_ENV: &str = "RUST_LOG";
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use std::{io, sync::Mutex};
22

3-
use crate::fmt::writer::WriteStyle;
3+
use crate::writer::WriteStyle;
44

55
#[derive(Debug)]
6-
pub(in crate::fmt::writer) struct BufferWriter {
6+
pub(crate) struct BufferWriter {
77
target: WritableTarget,
88
write_style: WriteStyle,
99
}
1010

1111
impl BufferWriter {
12-
pub(in crate::fmt::writer) fn stderr(is_test: bool, write_style: WriteStyle) -> Self {
12+
pub(crate) fn stderr(is_test: bool, write_style: WriteStyle) -> Self {
1313
BufferWriter {
1414
target: if is_test {
1515
WritableTarget::PrintStderr
@@ -20,7 +20,7 @@ impl BufferWriter {
2020
}
2121
}
2222

23-
pub(in crate::fmt::writer) fn stdout(is_test: bool, write_style: WriteStyle) -> Self {
23+
pub(crate) fn stdout(is_test: bool, write_style: WriteStyle) -> Self {
2424
BufferWriter {
2525
target: if is_test {
2626
WritableTarget::PrintStdout
@@ -31,7 +31,7 @@ impl BufferWriter {
3131
}
3232
}
3333

34-
pub(in crate::fmt::writer) fn pipe(
34+
pub(crate) fn pipe(
3535
pipe: Box<Mutex<dyn io::Write + Send + 'static>>,
3636
write_style: WriteStyle,
3737
) -> Self {
@@ -41,15 +41,15 @@ impl BufferWriter {
4141
}
4242
}
4343

44-
pub(in crate::fmt::writer) fn write_style(&self) -> WriteStyle {
44+
pub(crate) fn write_style(&self) -> WriteStyle {
4545
self.write_style
4646
}
4747

48-
pub(in crate::fmt::writer) fn buffer(&self) -> Buffer {
48+
pub(crate) fn buffer(&self) -> Buffer {
4949
Buffer(Vec::new())
5050
}
5151

52-
pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
52+
pub(crate) fn print(&self, buf: &Buffer) -> io::Result<()> {
5353
#![allow(clippy::print_stdout)] // enabled for tests only
5454
#![allow(clippy::print_stderr)] // enabled for tests only
5555

@@ -115,23 +115,23 @@ fn adapt(buf: &[u8], write_style: WriteStyle) -> io::Result<Vec<u8>> {
115115
Ok(adapted)
116116
}
117117

118-
pub(in crate::fmt) struct Buffer(Vec<u8>);
118+
pub(crate) struct Buffer(Vec<u8>);
119119

120120
impl Buffer {
121-
pub(in crate::fmt) fn clear(&mut self) {
121+
pub(crate) fn clear(&mut self) {
122122
self.0.clear();
123123
}
124124

125-
pub(in crate::fmt) fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
125+
pub(crate) fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
126126
self.0.extend(buf);
127127
Ok(buf.len())
128128
}
129129

130-
pub(in crate::fmt) fn flush(&mut self) -> io::Result<()> {
130+
pub(crate) fn flush(&mut self) -> io::Result<()> {
131131
Ok(())
132132
}
133133

134-
pub(in crate::fmt) fn as_bytes(&self) -> &[u8] {
134+
pub(crate) fn as_bytes(&self) -> &[u8] {
135135
&self.0
136136
}
137137
}
@@ -145,7 +145,7 @@ impl std::fmt::Debug for Buffer {
145145
/// Log target, either `stdout`, `stderr` or a custom pipe.
146146
///
147147
/// Same as `Target`, except the pipe is wrapped in a mutex for interior mutability.
148-
pub(super) enum WritableTarget {
148+
pub(crate) enum WritableTarget {
149149
/// Logs will be written to standard output.
150150
WriteStdout,
151151
/// Logs will be printed to standard output.
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
mod buffer;
22
mod target;
33

4-
use self::buffer::BufferWriter;
54
use std::{io, mem, sync::Mutex};
65

7-
pub(super) use self::buffer::Buffer;
6+
use buffer::BufferWriter;
7+
8+
pub(crate) use buffer::Buffer;
89

910
pub use target::Target;
1011

@@ -55,11 +56,11 @@ impl Writer {
5556
self.inner.write_style()
5657
}
5758

58-
pub(super) fn buffer(&self) -> Buffer {
59+
pub(crate) fn buffer(&self) -> Buffer {
5960
self.inner.buffer()
6061
}
6162

62-
pub(super) fn print(&self, buf: &Buffer) -> io::Result<()> {
63+
pub(crate) fn print(&self, buf: &Buffer) -> io::Result<()> {
6364
self.inner.print(buf)
6465
}
6566
}

0 commit comments

Comments
 (0)