diff --git a/src/fmt/mod.rs b/src/fmt/mod.rs index 5f1d5c8..95feb78 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 e6809b0..9905678 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 d3090a1..648ed16 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 41466b9..c8753e0 100644 --- a/src/fmt/writer/mod.rs +++ b/src/fmt/writer/mod.rs @@ -1,112 +1,16 @@ mod atty; mod buffer; +mod target; 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. -#[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>), -} +pub use target::Target; +use target::WritableTarget; -impl WritableTarget { - fn print(&self, buf: &Buffer) -> 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 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 0000000..3d90866 --- /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", + } + ) + } +} diff --git a/src/lib.rs b/src/lib.rs index cdc2bad..93eba6c 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::*;