Skip to content

Commit

Permalink
Add set_enable_paris_formatting() ConfigBuilder method to control whe…
Browse files Browse the repository at this point in the history
…ther a given logger uses or strips paris formatting. Only available with the paris feature, defaults to enabled for backwards compatibility. Fixes #98 and #112
  • Loading branch information
shimaowo committed May 30, 2023
1 parent ee2ab1b commit 9ee5fd1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 135 deletions.
14 changes: 14 additions & 0 deletions src/config.rs
Expand Up @@ -86,6 +86,8 @@ pub struct Config {
#[cfg(feature = "termcolor")]
pub(crate) level_color: [Option<Color>; 6],
pub(crate) write_log_enable_colors: bool,
#[cfg(feature = "paris")]
pub(crate) enable_paris_formatting: bool
}

/// Builder for the Logger Configurations (`Config`)
Expand Down Expand Up @@ -242,6 +244,15 @@ impl ConfigBuilder {
self
}

/// set if you want paris formatting to be applied to this logger (default is On)
///
/// If disabled, paris markup and formatting will be stripped.
#[cfg(feature = "paris")]
pub fn set_enable_paris_formatting(&mut self, enable_formatting: bool) -> &mut ConfigBuilder {
self.0.enable_paris_formatting = enable_formatting;
self
}

/// Add allowed target filters.
/// If any are specified, only records from targets matching one of these entries will be printed
///
Expand Down Expand Up @@ -339,6 +350,9 @@ impl Default for Config {
Some(Color::Cyan), // Debug
Some(Color::White), // Trace
],

#[cfg(feature = "paris")]
enable_paris_formatting: true
}
}
}
5 changes: 1 addition & 4 deletions src/lib.rs
Expand Up @@ -39,15 +39,12 @@ pub use termcolor::{Color, ColorChoice};
pub use log::{Level, LevelFilter};

use log::Log;
#[cfg(all(test, not(feature = "paris")))]
#[cfg(all(test))]
use log::*;

#[cfg(feature = "paris")]
pub(crate) mod paris_macros;
#[cfg(feature = "paris")]
#[doc(hidden)]
pub mod __private {
pub use log;
pub use paris;
}

Expand Down
16 changes: 15 additions & 1 deletion src/loggers/logging.rs
Expand Up @@ -57,7 +57,10 @@ where
write_location(record, write)?;
}

write_args(record, write)
#[cfg(feature = "paris")]
return write_args(record, write, config.enable_paris_formatting);
#[cfg(not(feature = "paris"))]
return write_args(record, write);
}

#[inline(always)]
Expand Down Expand Up @@ -209,6 +212,17 @@ where
}

#[inline(always)]
#[cfg(feature = "paris")]
pub fn write_args<W>(record: &Record<'_>, write: &mut W, with_colors: bool) -> Result<(), Error>
where
W: Write + Sized,
{
writeln!(write, "{}", crate::__private::paris::formatter::format_string(format!("{}", record.args()), with_colors))?;

Check failure on line 220 in src/loggers/logging.rs

View workflow job for this annotation

GitHub Actions / check-minimal

cannot find function `format_string` in module `crate::__private::paris::formatter`
Ok(())
}

#[inline(always)]
#[cfg(not(feature = "paris"))]
pub fn write_args<W>(record: &Record<'_>, write: &mut W) -> Result<(), Error>
where
W: Write + Sized,
Expand Down
3 changes: 3 additions & 0 deletions src/loggers/termlog.rs
Expand Up @@ -171,6 +171,9 @@ impl TermLogger {
write_location(record, term_lock)?;
}

#[cfg(feature = "paris")]
write_args(record, term_lock, self.config.enable_paris_formatting)?;
#[cfg(not(feature = "paris"))]
write_args(record, term_lock)?;

// The log crate holds the logger as a `static mut`, which isn't dropped
Expand Down
130 changes: 0 additions & 130 deletions src/paris_macros/mod.rs

This file was deleted.

0 comments on commit 9ee5fd1

Please sign in to comment.