From 03bee1f014015be11541032c167026e64663ff2c Mon Sep 17 00:00:00 2001 From: Mariusz Bialonczyk Date: Mon, 25 Oct 2021 17:25:40 +0200 Subject: [PATCH] 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 | 18 ++++++++++++++++++ src/lib.rs | 3 +++ src/paris/mod.rs | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 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..7b5b369d 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,24 @@ 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)*))); + }; +}