From 9ee5fd159480aef5a58d4ebdc23766b1cb7b82e3 Mon Sep 17 00:00:00 2001 From: shimaowo <45767709+shimaowo@users.noreply.github.com> Date: Tue, 30 May 2023 11:19:19 -0700 Subject: [PATCH 1/4] Add set_enable_paris_formatting() ConfigBuilder method to control whether a given logger uses or strips paris formatting. Only available with the paris feature, defaults to enabled for backwards compatibility. Fixes #98 and #112 --- src/config.rs | 14 +++++ src/lib.rs | 5 +- src/loggers/logging.rs | 16 ++++- src/loggers/termlog.rs | 3 + src/paris_macros/mod.rs | 130 ---------------------------------------- 5 files changed, 33 insertions(+), 135 deletions(-) delete mode 100644 src/paris_macros/mod.rs diff --git a/src/config.rs b/src/config.rs index 050dd90a..17044801 100644 --- a/src/config.rs +++ b/src/config.rs @@ -86,6 +86,8 @@ pub struct Config { #[cfg(feature = "termcolor")] pub(crate) level_color: [Option; 6], pub(crate) write_log_enable_colors: bool, + #[cfg(feature = "paris")] + pub(crate) enable_paris_formatting: bool } /// Builder for the Logger Configurations (`Config`) @@ -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 /// @@ -339,6 +350,9 @@ impl Default for Config { Some(Color::Cyan), // Debug Some(Color::White), // Trace ], + + #[cfg(feature = "paris")] + enable_paris_formatting: true } } } diff --git a/src/lib.rs b/src/lib.rs index 51c38787..178c53b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; } diff --git a/src/loggers/logging.rs b/src/loggers/logging.rs index a09bff3d..3622bcae 100644 --- a/src/loggers/logging.rs +++ b/src/loggers/logging.rs @@ -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)] @@ -209,6 +212,17 @@ where } #[inline(always)] +#[cfg(feature = "paris")] +pub fn write_args(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(record: &Record<'_>, write: &mut W) -> Result<(), Error> where W: Write + Sized, diff --git a/src/loggers/termlog.rs b/src/loggers/termlog.rs index 9bf13f9a..a74d9ac9 100644 --- a/src/loggers/termlog.rs +++ b/src/loggers/termlog.rs @@ -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 diff --git a/src/paris_macros/mod.rs b/src/paris_macros/mod.rs deleted file mode 100644 index 8d1b91f2..00000000 --- a/src/paris_macros/mod.rs +++ /dev/null @@ -1,130 +0,0 @@ -/// Logs a message at the info level. -/// -/// Passed data uses a colorize_string formatter from a `paris` crate, so it can -/// contains special tags for controlling ANSI colors and styles -/// More info: -/// -/// # Examples -/// -/// ```edition2018 -/// use log::info; -/// -/// # fn main() { -/// # struct Connection { port: u32, speed: f32 } -/// let conn_info = Connection { port: 40, speed: 3.20 }; -/// -/// info!("Connected to port {} at {} Mb/s", conn_info.port, conn_info.speed); -/// info!(target: "connection_events", "Successful connection, port: {}, speed: {}", -/// conn_info.port, conn_info.speed); -/// # } -/// ``` -#[macro_export] -macro_rules! info { - ($($args:tt)+) => { - $crate::__private::log::info!("{}", $crate::__private::paris::formatter::colorize_string(format!($($args)*))); - }; -} - -/// Logs a message at the debug level. -/// -/// Passed data uses a colorize_string formatter from a `paris` crate, so it can -/// contains special tags for controlling ANSI colors and styles -/// More info: -/// -/// # Examples -/// -/// ```edition2018 -/// use log::debug; -/// -/// # fn main() { -/// # struct Position { x: f32, y: f32 } -/// let pos = Position { x: 3.234, y: -1.223 }; -/// -/// debug!("New position: x: {}, y: {}", pos.x, pos.y); -/// debug!(target: "app_events", "New position: x: {}, y: {}", pos.x, pos.y); -/// # } -/// ``` -#[macro_export] -macro_rules! debug { - ($($args:tt)+) => { - $crate::__private::log::debug!("{}", $crate::__private::paris::formatter::colorize_string(format!($($args)*))); - }; -} - -/// Logs a message at the trace level. -/// -/// Passed data uses a colorize_string formatter from a `paris` crate, so it can -/// contains special tags for controlling ANSI colors and styles -/// More info: -/// -/// # Examples -/// -/// ```edition2018 -/// use log::trace; -/// -/// # fn main() { -/// # struct Position { x: f32, y: f32 } -/// let pos = Position { x: 3.234, y: -1.223 }; -/// -/// trace!("Position is: x: {}, y: {}", pos.x, pos.y); -/// trace!(target: "app_events", "x is {} and y is {}", -/// if pos.x >= 0.0 { "positive" } else { "negative" }, -/// if pos.y >= 0.0 { "positive" } else { "negative" }); -/// # } -/// ``` -#[macro_export] -macro_rules! trace { - ($($args:tt)+) => { - $crate::__private::log::trace!("{}", $crate::__private::paris::formatter::colorize_string(format!($($args)*))); - }; -} - -/// Logs a message at the warn level. -/// -/// Passed data uses a colorize_string formatter from a `paris` crate, so it can -/// contains special tags for controlling ANSI colors and styles -/// More info: -/// -/// # Examples -/// -/// ```edition2018 -/// use log::warn; -/// -/// # fn main() { -/// let warn_description = "Invalid Input"; -/// -/// warn!("Warning! {}!", warn_description); -/// warn!(target: "input_events", "App received warning: {}", warn_description); -/// # } -/// ``` -#[macro_export] -macro_rules! warn { - ($($args:tt)+) => { - $crate::__private::log::warn!("{}", $crate::__private::paris::formatter::colorize_string(format!($($args)*))); - }; -} - -/// Logs a message at the error level. -/// -/// Passed data uses a colorize_string formatter from a `paris` crate, so it can -/// contains special tags for controlling ANSI colors and styles -/// More info: -/// -/// # Examples -/// -/// ```edition2018 -/// use log::error; -/// -/// # fn main() { -/// let (err_info, port) = ("No connection", 22); -/// -/// error!("Error: {} on port {}", err_info, port); -/// error!(target: "app_events", "App Error: {}, Port: {}", err_info, 22); -/// # } -/// ``` -#[macro_export] -macro_rules! error { - ($($args:tt)+) => { - $crate::__private::log::error!("{}", $crate::__private::paris::formatter::colorize_string(format!($($args)*))); - }; -} From b997b2a23cedc1f23d9256cd6897075a29abf874 Mon Sep 17 00:00:00 2001 From: shimaowo <45767709+shimaowo@users.noreply.github.com> Date: Tue, 30 May 2023 12:41:01 -0700 Subject: [PATCH 2/4] update minimum paris version to when the format_string() method was introduced --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2895974f..6ebc6322 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } From edad11013d6f7bc33defa2ecb53bd835812ff6c8 Mon Sep 17 00:00:00 2001 From: shimaowo <45767709+shimaowo@users.noreply.github.com> Date: Tue, 30 May 2023 12:42:10 -0700 Subject: [PATCH 3/4] run rustfmt --- src/config.rs | 6 +++--- src/loggers/logging.rs | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index 17044801..e2a0c283 100644 --- a/src/config.rs +++ b/src/config.rs @@ -87,7 +87,7 @@ pub struct Config { pub(crate) level_color: [Option; 6], pub(crate) write_log_enable_colors: bool, #[cfg(feature = "paris")] - pub(crate) enable_paris_formatting: bool + pub(crate) enable_paris_formatting: bool, } /// Builder for the Logger Configurations (`Config`) @@ -245,7 +245,7 @@ impl ConfigBuilder { } /// 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 { @@ -352,7 +352,7 @@ impl Default for Config { ], #[cfg(feature = "paris")] - enable_paris_formatting: true + enable_paris_formatting: true, } } } diff --git a/src/loggers/logging.rs b/src/loggers/logging.rs index 3622bcae..a19d5098 100644 --- a/src/loggers/logging.rs +++ b/src/loggers/logging.rs @@ -217,7 +217,14 @@ pub fn write_args(record: &Record<'_>, write: &mut W, with_colors: bool) -> R where W: Write + Sized, { - writeln!(write, "{}", crate::__private::paris::formatter::format_string(format!("{}", record.args()), with_colors))?; + writeln!( + write, + "{}", + crate::__private::paris::formatter::format_string( + format!("{}", record.args()), + with_colors + ) + )?; Ok(()) } From 1c2c36c3d687fff9f17936d6e6ecc9f77dbb2140 Mon Sep 17 00:00:00 2001 From: shimaowo <45767709+shimaowo@users.noreply.github.com> Date: Tue, 30 May 2023 13:53:52 -0700 Subject: [PATCH 4/4] fix tests/examples; unify log import handling now that paris doesn't require something different --- examples/custom_colors.rs | 2 +- examples/default_colors.rs | 2 +- examples/rgb_colors.rs | 6 +----- examples/usage.rs | 1 - src/lib.rs | 2 +- 5 files changed, 4 insertions(+), 9 deletions(-) diff --git a/examples/custom_colors.rs b/examples/custom_colors.rs index 8d45a501..f3c1a0ea 100644 --- a/examples/custom_colors.rs +++ b/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::*; diff --git a/examples/default_colors.rs b/examples/default_colors.rs index 1d8d141b..3c6984ce 100644 --- a/examples/default_colors.rs +++ b/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::*; diff --git a/examples/rgb_colors.rs b/examples/rgb_colors.rs index 8fc1c15a..6cffa9cc 100644 --- a/examples/rgb_colors.rs +++ b/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::*; diff --git a/examples/usage.rs b/examples/usage.rs index 8d4959c3..7e17295c 100644 --- a/examples/usage.rs +++ b/examples/usage.rs @@ -1,4 +1,3 @@ -#[cfg(not(feature = "paris"))] use log::*; use simplelog::*; diff --git a/src/lib.rs b/src/lib.rs index 178c53b2..3067d1d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,7 @@ pub use termcolor::{Color, ColorChoice}; pub use log::{Level, LevelFilter}; use log::Log; -#[cfg(all(test))] +#[cfg(test)] use log::*; #[cfg(feature = "paris")]