From c9046f02db26b038904025413893dd40527af871 Mon Sep 17 00:00:00 2001 From: Mariusz Bialonczyk Date: Mon, 25 Oct 2021 17:25:40 +0200 Subject: [PATCH 1/8] Add optional color and style support using paris crate More info about formatting: https://github.com/0x20F/paris When enabled you can use formatting like this: info!("I can write bold text or use tags to color it"); --- Cargo.toml | 1 + README.md | 19 +++++++++++++++++++ src/lib.rs | 3 +++ src/paris/mod.rs | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 src/paris/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 3d8e8dd3..6a83f5bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,4 +25,5 @@ default = ["termcolor"] [dependencies] log = { version = "0.4.*", features = ["std"] } termcolor = { version = "1.1.*", optional = true } +paris = { version = "*", optional = true } chrono = "0.4.1" diff --git a/README.md b/README.md index 2428131b..cfa4deb1 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,25 @@ simplelog = "^0.10.0" ``` to your `Cargo.toml` +## ANSI color and style support + +This crate can internally depend on a [paris](https://github.com/0x20F/paris) crate to provide support for ANSI color and styles. +To use this feature you need to set a _paris_ feature, like this: +``` +[dependencies] +simplelog = { version = "^0.10.0", features = ["paris"] } +``` +in your `Cargo.toml` + +After this you can use e.g. the following call: +```rust +info!("I can write bold text or use tags to color it"); +``` + +This will automatically generates terminal control sequences for desired styles. + +More formatting info: [paris crate documentation](https://github.com/0x20F/paris) + ## [Documentation](https://docs.rs/simplelog/) ## Contributing diff --git a/src/lib.rs b/src/lib.rs index b3fb2248..97459081 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,9 @@ use log::Log; #[cfg(test)] use log::*; +#[cfg(feature = "paris")] +mod paris; + /// Trait to have a common interface to obtain the Level of Loggers /// /// Necessary for CombinedLogger to calculate diff --git a/src/paris/mod.rs b/src/paris/mod.rs new file mode 100644 index 00000000..ec8f3cab --- /dev/null +++ b/src/paris/mod.rs @@ -0,0 +1,39 @@ +#[allow(missing_docs)] +#[macro_export] +macro_rules! info { + ($($args:tt)+) => { + log::info!("{}", paris::formatter::colorize_string(format!($($args)*))); + }; +} + +#[allow(missing_docs)] +#[macro_export] +macro_rules! debug { + ($($args:tt)+) => { + log::debug!("{}", paris::formatter::colorize_string(format!($($args)*))); + }; +} + +#[allow(missing_docs)] +#[macro_export] +macro_rules! trace { + ($($args:tt)+) => { + log::trace!("{}", paris::formatter::colorize_string(format!($($args)*))); + }; +} + +#[allow(missing_docs)] +#[macro_export] +macro_rules! warn { + ($($args:tt)+) => { + log::warn!("{}", paris::formatter::colorize_string(format!($($args)*))); + }; +} + +#[allow(missing_docs)] +#[macro_export] +macro_rules! error { + ($($args:tt)+) => { + log::error!("{}", paris::formatter::colorize_string(format!($($args)*))); + }; +} From 97f63d553fe798e67451b8b48d529f997c1b8581 Mon Sep 17 00:00:00 2001 From: Mariusz Bialonczyk Date: Tue, 2 Nov 2021 17:32:15 +0100 Subject: [PATCH 2/8] paris: rename paris to paris_macros This will distinguish between official crate and our module. --- src/lib.rs | 2 +- src/{paris => paris_macros}/mod.rs | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/{paris => paris_macros}/mod.rs (100%) diff --git a/src/lib.rs b/src/lib.rs index 97459081..a98fbf4d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,7 +40,7 @@ use log::Log; use log::*; #[cfg(feature = "paris")] -mod paris; +mod paris_macros; /// Trait to have a common interface to obtain the Level of Loggers /// diff --git a/src/paris/mod.rs b/src/paris_macros/mod.rs similarity index 100% rename from src/paris/mod.rs rename to src/paris_macros/mod.rs From 0b9cbc2bc9a93e6facaf5b86baa8b5caa6e531e3 Mon Sep 17 00:00:00 2001 From: Mariusz Bialonczyk Date: Tue, 2 Nov 2021 17:40:33 +0100 Subject: [PATCH 3/8] make paris as 'pub extern crate' Project using a simplelog don't need to add paris as dependency anymore. --- src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index a98fbf4d..d4dfc5c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,6 +41,8 @@ use log::*; #[cfg(feature = "paris")] mod paris_macros; +#[cfg(feature = "paris")] +pub extern crate paris; /// Trait to have a common interface to obtain the Level of Loggers /// From 0d8d7b274f13ada16430c6823cd9699c6642457f Mon Sep 17 00:00:00 2001 From: Mariusz Bialonczyk Date: Tue, 2 Nov 2021 17:44:18 +0100 Subject: [PATCH 4/8] Cargo.toml: stick with current paris version instead of wildcard --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 6a83f5bf..7d79968a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,5 +25,5 @@ default = ["termcolor"] [dependencies] log = { version = "0.4.*", features = ["std"] } termcolor = { version = "1.1.*", optional = true } -paris = { version = "*", optional = true } +paris = { version = "1.5.7", optional = true } chrono = "0.4.1" From 79d636d31b49680c7b3a5d549558c1d1f5caac71 Mon Sep 17 00:00:00 2001 From: Mariusz Bialonczyk Date: Tue, 2 Nov 2021 17:56:05 +0100 Subject: [PATCH 5/8] paris_macros: add macros documentation from log crate --- src/paris_macros/mod.rs | 81 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 5 deletions(-) diff --git a/src/paris_macros/mod.rs b/src/paris_macros/mod.rs index ec8f3cab..8a2ebaf9 100644 --- a/src/paris_macros/mod.rs +++ b/src/paris_macros/mod.rs @@ -1,4 +1,19 @@ -#[allow(missing_docs)] +/// Logs a message at the info level. +/// +/// # 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", "Successfull connection, port: {}, speed: {}", +/// conn_info.port, conn_info.speed); +/// # } +/// ``` #[macro_export] macro_rules! info { ($($args:tt)+) => { @@ -6,7 +21,21 @@ macro_rules! info { }; } -#[allow(missing_docs)] +/// Logs a message at the debug level. +/// +/// # 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)+) => { @@ -14,7 +43,23 @@ macro_rules! debug { }; } -#[allow(missing_docs)] +/// Logs a message at the trace level. +/// +/// # 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)+) => { @@ -22,7 +67,20 @@ macro_rules! trace { }; } -#[allow(missing_docs)] +/// Logs a message at the warn level. +/// +/// # 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)+) => { @@ -30,7 +88,20 @@ macro_rules! warn { }; } -#[allow(missing_docs)] +/// Logs a message at the error level. +/// +/// # 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)+) => { From 86703d2d6b9191dff076e5d2fc852c93ed0afe3c Mon Sep 17 00:00:00 2001 From: Mariusz Bialonczyk Date: Tue, 2 Nov 2021 17:57:09 +0100 Subject: [PATCH 6/8] paris_macros: add information about colorize_string --- src/paris_macros/mod.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/paris_macros/mod.rs b/src/paris_macros/mod.rs index 8a2ebaf9..3baebcfb 100644 --- a/src/paris_macros/mod.rs +++ b/src/paris_macros/mod.rs @@ -1,5 +1,9 @@ /// 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: https://docs.rs/paris/1.5.7/paris/formatter/fn.colorize_string.html +/// /// # Examples /// /// ```edition2018 @@ -23,6 +27,10 @@ macro_rules! info { /// 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: https://docs.rs/paris/1.5.7/paris/formatter/fn.colorize_string.html +/// /// # Examples /// /// ```edition2018 @@ -45,6 +53,10 @@ macro_rules! debug { /// 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: https://docs.rs/paris/1.5.7/paris/formatter/fn.colorize_string.html +/// /// # Examples /// /// ```edition2018 @@ -69,6 +81,10 @@ macro_rules! trace { /// 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: https://docs.rs/paris/1.5.7/paris/formatter/fn.colorize_string.html +/// /// # Examples /// /// ```edition2018 @@ -90,6 +106,10 @@ macro_rules! warn { /// 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: https://docs.rs/paris/1.5.7/paris/formatter/fn.colorize_string.html +/// /// # Examples /// /// ```edition2018 From 16d452e8c4bad69a39a9ade36fedc2fd1a9fae94 Mon Sep 17 00:00:00 2001 From: Mariusz Bialonczyk Date: Wed, 3 Nov 2021 04:38:50 +0100 Subject: [PATCH 7/8] paris: make the paris_macros module public --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index d4dfc5c8..9d3b04ff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,7 +40,7 @@ use log::Log; use log::*; #[cfg(feature = "paris")] -mod paris_macros; +pub(crate) mod paris_macros; #[cfg(feature = "paris")] pub extern crate paris; From 5b234724aa7b054f9877eda3fa01f1627e4a8f9d Mon Sep 17 00:00:00 2001 From: Mariusz Bialonczyk Date: Sat, 6 Nov 2021 16:01:23 +0100 Subject: [PATCH 8/8] paris: fix tests and examples --- 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, 9 insertions(+), 4 deletions(-) diff --git a/examples/custom_colors.rs b/examples/custom_colors.rs index f3c1a0ea..8d45a501 100644 --- a/examples/custom_colors.rs +++ b/examples/custom_colors.rs @@ -1,4 +1,4 @@ -#[cfg(feature = "termcolor")] +#[cfg(all(feature = "termcolor", not(feature = "paris")))] use log::*; #[cfg(feature = "termcolor")] use simplelog::*; diff --git a/examples/default_colors.rs b/examples/default_colors.rs index 3c6984ce..1d8d141b 100644 --- a/examples/default_colors.rs +++ b/examples/default_colors.rs @@ -1,4 +1,4 @@ -#[cfg(feature = "termcolor")] +#[cfg(all(feature = "termcolor", not(feature = "paris")))] use log::*; #[cfg(feature = "termcolor")] use simplelog::*; diff --git a/examples/rgb_colors.rs b/examples/rgb_colors.rs index 6cffa9cc..8fc1c15a 100644 --- a/examples/rgb_colors.rs +++ b/examples/rgb_colors.rs @@ -1,4 +1,8 @@ -#[cfg(all(not(target_family = "windows"), feature = "termcolor"))] +#[cfg(all( + not(target_family = "windows"), + feature = "termcolor", + not(feature = "paris") +))] use log::*; #[cfg(all(not(target_family = "windows"), feature = "termcolor"))] use simplelog::*; diff --git a/examples/usage.rs b/examples/usage.rs index 7e17295c..8d4959c3 100644 --- a/examples/usage.rs +++ b/examples/usage.rs @@ -1,3 +1,4 @@ +#[cfg(not(feature = "paris"))] use log::*; use simplelog::*; diff --git a/src/lib.rs b/src/lib.rs index 9d3b04ff..481404db 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,7 +36,7 @@ pub use termcolor::{Color, ColorChoice}; pub use log::{Level, LevelFilter}; use log::Log; -#[cfg(test)] +#[cfg(all(test, not(feature = "paris")))] use log::*; #[cfg(feature = "paris")]