From 8018168cf14d46e32a9674df97a8db64730575d3 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 17 Jan 2024 20:26:36 -0600 Subject: [PATCH 1/2] refactor(fmt): Remove glob mods --- src/fmt/mod.rs | 7 ++----- src/fmt/writer/buffer/plain.rs | 2 +- src/fmt/writer/buffer/termcolor.rs | 2 +- src/fmt/writer/mod.rs | 4 ---- src/lib.rs | 2 +- 5 files changed, 5 insertions(+), 12 deletions(-) diff --git a/src/fmt/mod.rs b/src/fmt/mod.rs index 5f1d5c8f..95feb782 100644 --- a/src/fmt/mod.rs +++ b/src/fmt/mod.rs @@ -50,14 +50,11 @@ pub use style::{Color, Style, StyledValue}; #[cfg(feature = "humantime")] pub use self::humantime::Timestamp; -pub use self::writer::glob::*; +pub use self::writer::Target; +pub use self::writer::WriteStyle; use self::writer::{Buffer, Writer}; -pub(crate) mod glob { - pub use super::{Target, TimestampPrecision, WriteStyle}; -} - /// Formatting precision of timestamps. /// /// Seconds give precision of full seconds, milliseconds give thousands of a diff --git a/src/fmt/writer/buffer/plain.rs b/src/fmt/writer/buffer/plain.rs index e6809b06..99056783 100644 --- a/src/fmt/writer/buffer/plain.rs +++ b/src/fmt/writer/buffer/plain.rs @@ -1,6 +1,6 @@ use std::{io, sync::Mutex}; -use crate::fmt::{WritableTarget, WriteStyle}; +use crate::fmt::writer::{WritableTarget, WriteStyle}; pub(in crate::fmt::writer) struct BufferWriter { target: WritableTarget, diff --git a/src/fmt/writer/buffer/termcolor.rs b/src/fmt/writer/buffer/termcolor.rs index d3090a17..648ed16f 100644 --- a/src/fmt/writer/buffer/termcolor.rs +++ b/src/fmt/writer/buffer/termcolor.rs @@ -3,7 +3,7 @@ use std::sync::Mutex; use termcolor::{self, ColorSpec, WriteColor}; -use crate::fmt::{WritableTarget, WriteStyle}; +use crate::fmt::writer::{WritableTarget, WriteStyle}; pub(in crate::fmt::writer) struct BufferWriter { inner: termcolor::BufferWriter, diff --git a/src/fmt/writer/mod.rs b/src/fmt/writer/mod.rs index 41466b92..986b75c6 100644 --- a/src/fmt/writer/mod.rs +++ b/src/fmt/writer/mod.rs @@ -5,10 +5,6 @@ use self::atty::{is_stderr, is_stdout}; use self::buffer::BufferWriter; use std::{fmt, io, mem, sync::Mutex}; -pub(super) mod glob { - pub use super::*; -} - pub(super) use self::buffer::Buffer; /// Log target, either `stdout`, `stderr` or a custom pipe. diff --git a/src/lib.rs b/src/lib.rs index cdc2badc..93eba6ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -280,5 +280,5 @@ mod logger; pub mod filter; pub mod fmt; -pub use self::fmt::glob::*; +pub use self::fmt::{Target, TimestampPrecision, WriteStyle}; pub use self::logger::*; From f4808e46e8adf6f4d0182538070ce98f11723bb1 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 17 Jan 2024 20:29:23 -0600 Subject: [PATCH 2/2] refactor(fmt): Pull out target mod --- src/fmt/writer/mod.rs | 98 ++-------------------------------------- src/fmt/writer/target.rs | 96 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 95 deletions(-) create mode 100644 src/fmt/writer/target.rs diff --git a/src/fmt/writer/mod.rs b/src/fmt/writer/mod.rs index 986b75c6..c8753e0f 100644 --- a/src/fmt/writer/mod.rs +++ b/src/fmt/writer/mod.rs @@ -1,5 +1,6 @@ mod atty; mod buffer; +mod target; use self::atty::{is_stderr, is_stdout}; use self::buffer::BufferWriter; @@ -7,102 +8,9 @@ use std::{fmt, io, mem, sync::Mutex}; pub(super) use self::buffer::Buffer; -/// Log target, either `stdout`, `stderr` or a custom pipe. -#[non_exhaustive] -pub enum Target { - /// Logs will be sent to standard output. - Stdout, - /// Logs will be sent to standard error. - Stderr, - /// Logs will be sent to a custom pipe. - Pipe(Box), -} - -impl Default for Target { - fn default() -> Self { - Target::Stderr - } -} - -impl fmt::Debug for Target { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "{}", - match self { - Self::Stdout => "stdout", - Self::Stderr => "stderr", - Self::Pipe(_) => "pipe", - } - ) - } -} - -/// Log target, either `stdout`, `stderr` or a custom pipe. -/// -/// Same as `Target`, except the pipe is wrapped in a mutex for interior mutability. -pub(super) enum WritableTarget { - /// Logs will be written to standard output. - #[allow(dead_code)] - WriteStdout, - /// Logs will be printed to standard output. - PrintStdout, - /// Logs will be written to standard error. - #[allow(dead_code)] - WriteStderr, - /// Logs will be printed to standard error. - PrintStderr, - /// Logs will be sent to a custom pipe. - Pipe(Box>), -} - -impl WritableTarget { - fn print(&self, buf: &Buffer) -> io::Result<()> { - use std::io::Write as _; +pub use target::Target; +use target::WritableTarget; - let buf = buf.as_bytes(); - match self { - WritableTarget::WriteStdout => { - let stream = std::io::stdout(); - let mut stream = stream.lock(); - stream.write_all(buf)?; - stream.flush()?; - } - WritableTarget::PrintStdout => print!("{}", String::from_utf8_lossy(buf)), - WritableTarget::WriteStderr => { - let stream = std::io::stderr(); - let mut stream = stream.lock(); - stream.write_all(buf)?; - stream.flush()?; - } - WritableTarget::PrintStderr => eprint!("{}", String::from_utf8_lossy(buf)), - // Safety: If the target type is `Pipe`, `target_pipe` will always be non-empty. - WritableTarget::Pipe(pipe) => { - let mut stream = pipe.lock().unwrap(); - stream.write_all(buf)?; - stream.flush()?; - } - } - - Ok(()) - } -} - -impl fmt::Debug for WritableTarget { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "{}", - match self { - Self::WriteStdout => "stdout", - Self::PrintStdout => "stdout", - Self::WriteStderr => "stderr", - Self::PrintStderr => "stderr", - Self::Pipe(_) => "pipe", - } - ) - } -} /// Whether or not to print styles to the target. #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub enum WriteStyle { diff --git a/src/fmt/writer/target.rs b/src/fmt/writer/target.rs new file mode 100644 index 00000000..3d908665 --- /dev/null +++ b/src/fmt/writer/target.rs @@ -0,0 +1,96 @@ +/// Log target, either `stdout`, `stderr` or a custom pipe. +#[non_exhaustive] +pub enum Target { + /// Logs will be sent to standard output. + Stdout, + /// Logs will be sent to standard error. + Stderr, + /// Logs will be sent to a custom pipe. + Pipe(Box), +} + +impl Default for Target { + fn default() -> Self { + Target::Stderr + } +} + +impl std::fmt::Debug for Target { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Self::Stdout => "stdout", + Self::Stderr => "stderr", + Self::Pipe(_) => "pipe", + } + ) + } +} + +/// Log target, either `stdout`, `stderr` or a custom pipe. +/// +/// Same as `Target`, except the pipe is wrapped in a mutex for interior mutability. +pub(super) enum WritableTarget { + /// Logs will be written to standard output. + #[allow(dead_code)] + WriteStdout, + /// Logs will be printed to standard output. + PrintStdout, + /// Logs will be written to standard error. + #[allow(dead_code)] + WriteStderr, + /// Logs will be printed to standard error. + PrintStderr, + /// Logs will be sent to a custom pipe. + Pipe(Box>), +} + +impl WritableTarget { + pub(super) fn print(&self, buf: &super::Buffer) -> std::io::Result<()> { + use std::io::Write as _; + + let buf = buf.as_bytes(); + match self { + WritableTarget::WriteStdout => { + let stream = std::io::stdout(); + let mut stream = stream.lock(); + stream.write_all(buf)?; + stream.flush()?; + } + WritableTarget::PrintStdout => print!("{}", String::from_utf8_lossy(buf)), + WritableTarget::WriteStderr => { + let stream = std::io::stderr(); + let mut stream = stream.lock(); + stream.write_all(buf)?; + stream.flush()?; + } + WritableTarget::PrintStderr => eprint!("{}", String::from_utf8_lossy(buf)), + // Safety: If the target type is `Pipe`, `target_pipe` will always be non-empty. + WritableTarget::Pipe(pipe) => { + let mut stream = pipe.lock().unwrap(); + stream.write_all(buf)?; + stream.flush()?; + } + } + + Ok(()) + } +} + +impl std::fmt::Debug for WritableTarget { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Self::WriteStdout => "stdout", + Self::PrintStdout => "stdout", + Self::WriteStderr => "stderr", + Self::PrintStderr => "stderr", + Self::Pipe(_) => "pipe", + } + ) + } +}