Skip to content

Commit

Permalink
TermLogger/ConfigBuilder: Make colors optional
Browse files Browse the repository at this point in the history
White isn't a good default for those with light terminal screens
(brighter background, darker text). This is already trivial in
termcolor, so just allow level colors to be None.
  • Loading branch information
mrkline committed Mar 26, 2021
1 parent 8338b80 commit d87ea5c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "simplelog"
version = "0.9.0"
version = "0.10.0"
edition = "2018"
authors = ["Drakulix <github@drakulix.de>"]
description = "A simple and easy-to-use logging facility for Rust's log crate"
Expand Down
6 changes: 3 additions & 3 deletions examples/custom_colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use simplelog::*;
#[cfg(feature = "termcolor")]
fn main() {
let config = ConfigBuilder::new()
.set_level_color(Level::Error, Color::Magenta)
.set_level_color(Level::Trace, Color::Green)
.set_level_color(Level::Error, Some(Color::Magenta))
.set_level_color(Level::Trace, Some(Color::Green))
.build();

TermLogger::init(LevelFilter::Trace, config, TerminalMode::Stdout).unwrap();
Expand All @@ -19,4 +19,4 @@ fn main() {
#[cfg(not(feature = "termcolor"))]
fn main() {
println!("this example requires the termcolor feature.");
}
}
12 changes: 6 additions & 6 deletions examples/rgb_colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use simplelog::*;
#[cfg(all(not(target_family = "windows"), feature = "termcolor"))]
fn main() {
let config = ConfigBuilder::new()
.set_level_color(Level::Error, Color::Rgb(191, 0, 0))
.set_level_color(Level::Warn, Color::Rgb(255, 127, 0))
.set_level_color(Level::Info, Color::Rgb(192, 192, 0))
.set_level_color(Level::Debug, Color::Rgb(63, 127, 0))
.set_level_color(Level::Trace, Color::Rgb(127, 127, 255))
.set_level_color(Level::Error, Some(Color::Rgb(191, 0, 0)))
.set_level_color(Level::Warn, Some(Color::Rgb(255, 127, 0)))
.set_level_color(Level::Info, Some(Color::Rgb(192, 192, 0)))
.set_level_color(Level::Debug, Some(Color::Rgb(63, 127, 0)))
.set_level_color(Level::Trace, Some(Color::Rgb(127, 127, 255)))
.build();

TermLogger::init(LevelFilter::Trace, config, TerminalMode::Stdout).unwrap();
Expand All @@ -22,4 +22,4 @@ fn main() {
#[cfg(any(target_family = "windows", not(feature = "termcolor")))]
fn main() {
println!("this example requires the termcolor feature and a non-Windows OS.");
}
}
19 changes: 10 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub struct Config {
pub(crate) filter_allow: Cow<'static, [Cow<'static, str>]>,
pub(crate) filter_ignore: Cow<'static, [Cow<'static, str>]>,
#[cfg(feature = "termcolor")]
pub(crate) level_color: [Color; 6],
pub(crate) level_color: [Option<Color>; 6],
}

/// Builder for the Logger Configurations (`Config`)
Expand Down Expand Up @@ -138,9 +138,10 @@ impl ConfigBuilder {
self
}

/// Set the color used for printing the level (if the logger supports it)
/// Set the color used for printing the level (if the logger supports it),
/// or None to use the default foreground color
#[cfg(feature = "termcolor")]
pub fn set_level_color<'a>(&'a mut self, level: Level, color: Color) -> &'a mut ConfigBuilder {
pub fn set_level_color<'a>(&'a mut self, level: Level, color: Option<Color>) -> &'a mut ConfigBuilder {
self.0.level_color[level as usize] = color;
self
}
Expand Down Expand Up @@ -265,12 +266,12 @@ impl Default for Config {

#[cfg(feature = "termcolor")]
level_color: [
Color::White, // (dummy)
Color::Red, // Error
Color::Yellow, // Warn
Color::Blue, // Info
Color::Cyan, // Debug
Color::White, // Trace
None, // Default foreground
Some(Color::Red), // Error
Some(Color::Yellow), // Warn
Some(Color::Blue), // Info
Some(Color::Cyan), // Debug
Some(Color::White), // Trace
],
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/loggers/termlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl TermLogger {
}

if self.config.level <= record.level() && self.config.level != LevelFilter::Off {
term_lock.set_color(ColorSpec::new().set_fg(Some(color)))?;
term_lock.set_color(ColorSpec::new().set_fg(color))?;
write_level(record, &mut *term_lock, &self.config)?;
term_lock.reset()?;
}
Expand Down

0 comments on commit d87ea5c

Please sign in to comment.