Skip to content

Commit

Permalink
Merge pull request #126 from shimaowo/smw_customizable_paris_formatting
Browse files Browse the repository at this point in the history
[feat] Add set_enable_paris_formatting() ConfigBuilder method to control whether…
  • Loading branch information
Drakulix committed May 31, 2023
2 parents ee2ab1b + 1c2c36c commit ee6aefa
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 144 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -26,6 +26,6 @@ local-offset = ["time/local-offset"]
[dependencies]
log = { version = "0.4.*", features = ["std"] }
termcolor = { version = "1.1.*", optional = true }
paris = { version = "~1.5", optional = true }
paris = { version = "~1.5.12", optional = true }
ansi_term = { version = "0.12", optional = true }
time = { version = "0.3.7", features = ["formatting", "macros"] }
2 changes: 1 addition & 1 deletion examples/custom_colors.rs
@@ -1,4 +1,4 @@
#[cfg(all(feature = "termcolor", not(feature = "paris")))]
#[cfg(feature = "termcolor")]
use log::*;
#[cfg(feature = "termcolor")]
use simplelog::*;
Expand Down
2 changes: 1 addition & 1 deletion examples/default_colors.rs
@@ -1,4 +1,4 @@
#[cfg(all(feature = "termcolor", not(feature = "paris")))]
#[cfg(feature = "termcolor")]
use log::*;
#[cfg(feature = "termcolor")]
use simplelog::*;
Expand Down
6 changes: 1 addition & 5 deletions examples/rgb_colors.rs
@@ -1,8 +1,4 @@
#[cfg(all(
not(target_family = "windows"),
feature = "termcolor",
not(feature = "paris")
))]
#[cfg(all(not(target_family = "windows"), feature = "termcolor"))]
use log::*;
#[cfg(all(not(target_family = "windows"), feature = "termcolor"))]
use simplelog::*;
Expand Down
1 change: 0 additions & 1 deletion examples/usage.rs
@@ -1,4 +1,3 @@
#[cfg(not(feature = "paris"))]
use log::*;
use simplelog::*;

Expand Down
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,

Check warning on line 88 in src/config.rs

View workflow job for this annotation

GitHub Actions / test (beta, --no-default-features --features "test")

field `write_log_enable_colors` is never read

Check warning on line 88 in src/config.rs

View workflow job for this annotation

GitHub Actions / test (nightly, --no-default-features)

field `write_log_enable_colors` is never read

Check warning on line 88 in src/config.rs

View workflow job for this annotation

GitHub Actions / test (beta, --no-default-features)

field `write_log_enable_colors` is never read

Check warning on line 88 in src/config.rs

View workflow job for this annotation

GitHub Actions / test (nightly, --no-default-features --features "test")

field `write_log_enable_colors` is never read

Check warning on line 88 in src/config.rs

View workflow job for this annotation

GitHub Actions / test (stable, --no-default-features --features "test")

field `write_log_enable_colors` is never read

Check warning on line 88 in src/config.rs

View workflow job for this annotation

GitHub Actions / test (stable, --no-default-features)

field `write_log_enable_colors` is never read
#[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(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
23 changes: 22 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,24 @@ 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
)
)?;
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 ee6aefa

Please sign in to comment.