Skip to content

Commit b289a46

Browse files
committed
Override colored choices when running with stderr
1 parent f12a2b3 commit b289a46

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/lib.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ impl SimpleLogger {
348348
/// 'Init' the actual logger and instantiate it,
349349
/// this method MUST be called in order for the logger to be effective.
350350
pub fn init(self) -> Result<(), SetLoggerError> {
351-
#[cfg(all(windows, feature = "colored"))]
351+
// Setup colors if needed. The implementation if feature dependent.
352352
set_up_color_terminal();
353353

354354
log::set_max_level(self.max_level());
@@ -481,7 +481,7 @@ impl Log for SimpleLogger {
481481
fn flush(&self) {}
482482
}
483483

484-
/// Configure the console to display colours.
484+
/// Configure the console to display colours - Windows + colored
485485
///
486486
/// This is only needed on Windows when using the 'colored' feature.
487487
#[cfg(all(windows, feature = "colored"))]
@@ -513,10 +513,27 @@ pub fn set_up_color_terminal() {
513513
}
514514
}
515515

516-
/// Configure the console to display colours.
516+
/// Configure the console to display colours - Windows + !colored
517517
///
518-
/// This method does nothing if not running on Windows with the colored feature.
519-
#[cfg(not(all(windows, feature = "colored")))]
518+
/// This method does nothing if running on Windows with the colored feature disabled.
519+
#[cfg(all(windows, not(feature = "colored")))]
520+
pub fn set_up_color_terminal() {}
521+
522+
/// Configure the console to display colours - !Windows + stderr + colors
523+
///
524+
/// The colored crate will disable colors when stdout is not a terminal. This method overrides this
525+
/// behaviour to check the status of stderr instead.
526+
#[cfg(all(not(windows), feature = "stderr", feature = "colored"))]
527+
pub fn set_up_color_terminal() {
528+
use std::io::{stderr, IsTerminal};
529+
colored::control::set_override(stderr().is_terminal());
530+
}
531+
532+
/// Configure the console to display colours - !Windows + !stderr
533+
///
534+
/// This method does nothing if not running on Windows with the colored feature and outputting on
535+
/// stdout.
536+
#[cfg(all(not(windows), not(feature = "stderr")))]
520537
pub fn set_up_color_terminal() {}
521538

522539
/// Initialise the logger with its default configuration.

0 commit comments

Comments
 (0)