Skip to content

Commit

Permalink
Merge pull request #91 from spoutn1k/main
Browse files Browse the repository at this point in the history
Fix stderr color dependence on stdout
  • Loading branch information
borntyping authored Apr 30, 2024
2 parents f12a2b3 + 169b958 commit 4c461f7
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#![cfg_attr(feature = "nightly", feature(thread_id_value))]

#[cfg(feature = "colored")]
#[cfg(feature = "colors")]
use colored::*;
use log::{Level, LevelFilter, Log, Metadata, Record, SetLoggerError};
use std::{collections::HashMap, str::FromStr};
Expand Down Expand Up @@ -88,7 +88,7 @@ pub struct SimpleLogger {
/// Whether to use color output or not.
///
/// This field is only available if the `color` feature is enabled.
#[cfg(feature = "colored")]
#[cfg(feature = "colors")]
colors: bool,
}

Expand Down Expand Up @@ -118,7 +118,7 @@ impl SimpleLogger {
#[cfg(feature = "timestamps")]
timestamps_format: None,

#[cfg(feature = "colored")]
#[cfg(feature = "colors")]
colors: true,
}
}
Expand Down Expand Up @@ -331,7 +331,7 @@ impl SimpleLogger {
///
/// This method is only available if the `colored` feature is enabled.
#[must_use = "You must call init() to begin logging"]
#[cfg(feature = "colored")]
#[cfg(feature = "colors")]
pub fn with_colors(mut self, colors: bool) -> SimpleLogger {
self.colors = colors;
self
Expand All @@ -348,7 +348,7 @@ impl SimpleLogger {
/// 'Init' the actual logger and instantiate it,
/// this method MUST be called in order for the logger to be effective.
pub fn init(self) -> Result<(), SetLoggerError> {
#[cfg(all(windows, feature = "colored"))]
// Setup colors if needed. The implementation if feature dependent.
set_up_color_terminal();

log::set_max_level(self.max_level());
Expand Down Expand Up @@ -380,7 +380,7 @@ impl Log for SimpleLogger {
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
let level_string = {
#[cfg(feature = "colored")]
#[cfg(feature = "colors")]
{
if self.colors {
match record.level() {
Expand All @@ -394,7 +394,7 @@ impl Log for SimpleLogger {
format!("{:<5}", record.level().to_string())
}
}
#[cfg(not(feature = "colored"))]
#[cfg(not(feature = "colors"))]
{
format!("{:<5}", record.level().to_string())
}
Expand Down Expand Up @@ -481,10 +481,10 @@ impl Log for SimpleLogger {
fn flush(&self) {}
}

/// Configure the console to display colours.
/// Configure the console to display colours - Windows + colored
///
/// This is only needed on Windows when using the 'colored' feature.
#[cfg(all(windows, feature = "colored"))]
#[cfg(all(windows, feature = "colors"))]
pub fn set_up_color_terminal() {
use std::io::{stdout, IsTerminal};

Expand Down Expand Up @@ -513,10 +513,30 @@ pub fn set_up_color_terminal() {
}
}

/// Configure the console to display colours.
/// Configure the console to display colours - Windows + !colored
///
/// This method does nothing if running on Windows with the colored feature disabled.
#[cfg(all(windows, not(feature = "colors")))]
pub fn set_up_color_terminal() {}

/// Configure the console to display colours - !Windows + stderr + colors
///
/// The colored crate will disable colors when stdout is not a terminal. This method overrides this
/// behaviour to check the status of stderr instead.
#[cfg(all(not(windows), feature = "stderr"))]
pub fn set_up_color_terminal() {
#[cfg(feature = "colors")]
{
use std::io::{stderr, IsTerminal};
colored::control::set_override(stderr().is_terminal());
}
}

/// Configure the console to display colours - !Windows + !stderr
///
/// This method does nothing if not running on Windows with the colored feature.
#[cfg(not(all(windows, feature = "colored")))]
/// This method does nothing if not running on Windows with the colored feature and outputting on
/// stdout.
#[cfg(all(not(windows), not(feature = "stderr")))]
pub fn set_up_color_terminal() {}

/// Initialise the logger with its default configuration.
Expand Down

0 comments on commit 4c461f7

Please sign in to comment.