Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(fmt): More anstream prep #297

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/fmt/writer/buffer/plain.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/fmt/writer/buffer/termcolor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
102 changes: 3 additions & 99 deletions src/fmt/writer/mod.rs
Original file line number Diff line number Diff line change
@@ -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<dyn io::Write + Send + 'static>),
}

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<Mutex<dyn io::Write + Send + 'static>>),
}
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 {
Expand Down
96 changes: 96 additions & 0 deletions src/fmt/writer/target.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::io::Write + Send + 'static>),
}

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<std::sync::Mutex<dyn std::io::Write + Send + 'static>>),
}

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",
}
)
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Loading