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"] } 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/config.rs b/src/config.rs index 050dd90a..e2a0c283 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..3067d1d2 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(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..a19d5098 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,24 @@ 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)*))); - }; -}