Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stderr color dependence on stdout #91

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading