Skip to content

Commit

Permalink
Add format for stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Piriou committed Jul 1, 2020
1 parent d3bb737 commit 92125fe
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
43 changes: 31 additions & 12 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ pub struct Logger {
duplicate_err: Duplicate,
duplicate_out: Duplicate,
format_for_file: FormatFunction,
format_for_std_x: FormatFunction,
format_for_stderr: FormatFunction,
format_for_stdout: FormatFunction,
format_for_writer: FormatFunction,
flwb: FileLogWriterBuilder,
other_writers: HashMap<String, Box<dyn LogWriter>>,
Expand Down Expand Up @@ -136,9 +137,13 @@ impl Logger {
duplicate_out: Duplicate::None,
format_for_file: formats::default_format,
#[cfg(feature = "colors")]
format_for_std_x: formats::colored_default_format,
format_for_stderr: formats::colored_default_format,
#[cfg(not(feature = "colors"))]
format_for_std_x: formats::default_format,
format_for_stderr: formats::default_format,
#[cfg(feature = "colors")]
format_for_stdout: formats::colored_default_format,
#[cfg(not(feature = "colors"))]
format_for_stdout: formats::default_format,
format_for_writer: formats::default_format,
flwb: FileLogWriter::builder(),
other_writers: HashMap::<String, Box<dyn LogWriter>>::new(),
Expand Down Expand Up @@ -258,7 +263,8 @@ impl Logger {
/// `default_format()` is used for all outputs.
pub fn format(mut self, format: FormatFunction) -> Self {
self.format_for_file = format;
self.format_for_std_x = format;
self.format_for_stderr = format;
self.format_for_stdout = format;
self.format_for_writer = format;
self
}
Expand All @@ -273,11 +279,20 @@ impl Logger {
}

/// Makes the logger use the provided format function for messages
/// that are written to stderr or to stdout.
/// that are written to stderr.
///
/// Regarding the default, see [`Logger::format`](struct.Logger.html#method.format).
pub fn format_for_stderr(mut self, format: FormatFunction) -> Self {
self.format_for_std_x = format;
self.format_for_stderr = format;
self
}

/// Makes the logger use the provided format function for messages
/// that are written to stdout.
///
/// Regarding the default, see [`Logger::format`](struct.Logger.html#method.format).
pub fn format_for_stdout(mut self, format: FormatFunction) -> Self {
self.format_for_stdout = format;
self
}

Expand Down Expand Up @@ -565,7 +580,8 @@ impl Logger {
PrimaryWriter::multi(
self.duplicate_err,
self.duplicate_out,
self.format_for_std_x,
self.format_for_stderr,
self.format_for_stdout,
vec![Box::new(self.flwb.try_build()?)],
)
}
Expand All @@ -574,7 +590,8 @@ impl Logger {
PrimaryWriter::multi(
self.duplicate_err,
self.duplicate_out,
self.format_for_std_x,
self.format_for_stderr,
self.format_for_stdout,
vec![w],
)
}
Expand All @@ -584,16 +601,18 @@ impl Logger {
PrimaryWriter::multi(
self.duplicate_err,
self.duplicate_out,
self.format_for_std_x,
self.format_for_stderr,
self.format_for_stdout,
vec![Box::new(self.flwb.try_build()?), w],
)
}
LogTarget::StdOut => PrimaryWriter::stdout(self.format_for_std_x),
LogTarget::StdErr => PrimaryWriter::stderr(self.format_for_std_x),
LogTarget::StdOut => PrimaryWriter::stdout(self.format_for_stdout),
LogTarget::StdErr => PrimaryWriter::stderr(self.format_for_stderr),
LogTarget::DevNull => PrimaryWriter::black_hole(
self.duplicate_err,
self.duplicate_out,
self.format_for_std_x,
self.format_for_stderr,
self.format_for_stdout,
),
});

Expand Down
25 changes: 18 additions & 7 deletions src/primary_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ impl PrimaryWriter {
pub fn multi(
duplicate_stderr: Duplicate,
duplicate_stdout: Duplicate,
format_for_std_x: FormatFunction,
format_for_stderr: FormatFunction,
format_for_stdout: FormatFunction,
writers: Vec<Box<dyn LogWriter>>,
) -> Self {
Self::Multi(MultiWriter {
duplicate_stderr,
duplicate_stdout,
format_for_std_x,
format_for_stderr,
format_for_stdout,
writers,
})
}
Expand All @@ -41,9 +43,16 @@ impl PrimaryWriter {
pub fn black_hole(
duplicate_err: Duplicate,
duplicate_out: Duplicate,
format: FormatFunction,
format_for_stderr: FormatFunction,
format_for_stdout: FormatFunction,
) -> Self {
Self::multi(duplicate_err, duplicate_out, format, vec![])
Self::multi(
duplicate_err,
duplicate_out,
format_for_stderr,
format_for_stdout,
vec![],
)
}

// Write out a log line.
Expand Down Expand Up @@ -116,9 +125,11 @@ impl StdOutWriter {
pub(crate) struct MultiWriter {
duplicate_stderr: Duplicate,
duplicate_stdout: Duplicate,
format_for_std_x: FormatFunction,
format_for_stderr: FormatFunction,
format_for_stdout: FormatFunction,
writers: Vec<Box<dyn LogWriter>>,
}

impl LogWriter for MultiWriter {
fn validate_logs(&self, expected: &[(&'static str, &'static str, &'static str)]) {
for writer in &self.writers {
Expand All @@ -135,7 +146,7 @@ impl LogWriter for MultiWriter {
Duplicate::Trace | Duplicate::All => true,
Duplicate::None => false,
} {
write_buffered(self.format_for_std_x, now, record, &mut std::io::stderr())?;
write_buffered(self.format_for_stderr, now, record, &mut std::io::stderr())?;
}

if match self.duplicate_stdout {
Expand All @@ -146,7 +157,7 @@ impl LogWriter for MultiWriter {
Duplicate::Trace | Duplicate::All => true,
Duplicate::None => false,
} {
write_buffered(self.format_for_std_x, now, record, &mut std::io::stdout())?;
write_buffered(self.format_for_stdout, now, record, &mut std::io::stdout())?;
}

for writer in &self.writers {
Expand Down

0 comments on commit 92125fe

Please sign in to comment.