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

[feat] Add set_enable_paris_formatting() ConfigBuilder method to control whether… #126

Merged
merged 4 commits into from May 31, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -85,7 +85,9 @@
pub(crate) filter_ignore: Cow<'static, [Cow<'static, str>]>,
#[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 --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 (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)

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

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
#[cfg(feature = "paris")]
pub(crate) enable_paris_formatting: bool,
}

/// Builder for the Logger Configurations (`Config`)
Expand Down Expand Up @@ -178,7 +180,7 @@
/// The easiest way to satisfy the static lifetime of the argument is to directly use the
/// re-exported [`time::macros::format_description`] macro.
///
/// *Note*: The default time format is "[hour]:[minute]:[second]".

Check warning on line 183 in src/config.rs

View workflow job for this annotation

GitHub Actions / doc

unresolved link to `hour`

Check warning on line 183 in src/config.rs

View workflow job for this annotation

GitHub Actions / doc

unresolved link to `minute`

Check warning on line 183 in src/config.rs

View workflow job for this annotation

GitHub Actions / doc

unresolved link to `second`
///
/// The syntax for the format_description macro can be found in the
/// [`time` crate book](https://time-rs.github.io/book/api/format-description.html).
Expand Down Expand Up @@ -242,6 +244,15 @@
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 @@
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!(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blame rustfmt for this one

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.