Skip to content

Commit 1fb7ee6

Browse files
committed
Tidy up handling of stderr+colors
1 parent 4c461f7 commit 1fb7ee6

File tree

5 files changed

+34
-45
lines changed

5 files changed

+34
-45
lines changed

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ windows-sys = { version = "^0.48.0", features = ["Win32_System_Console", "Win32_
2727
name = "colors"
2828
required-features = ["colors"]
2929

30+
[[example]]
31+
name = "stderr"
32+
required-features = ["colors", "stderr"]
33+
3034
[[example]]
3135
name = "threads"
3236
required-features = ["threads"]

README.md

+5-14
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,13 @@ You might want to wrap this logger to do your own processing before handing even
104104
of calling `init()` which calls `log::set_max_level` and `log::set_boxed_logger`, you can call those functions directly
105105
giving you the chance to wrap or adjust the logger. See [wrap.rs](examples/wrap.rs) for a more detailed example.
106106

107-
The call to `set_up_color_terminal()` is currently only needed on Windows when the `colored` feature is enabled. If
108-
you're not on Windows and not using the `colored` feature, it will do nothing.
107+
### Console setup
109108

110-
```rust
111-
use simple_logger::{SimpleLogger, set_up_color_terminal};
112-
113-
fn main() {
114-
set_up_color_terminal();
109+
The `SimpleLogger.init()` function attempts to configure colours support as best it can in various situations:
115110

116-
let logger = SimpleLogger::new();
117-
let max_level = logger.max_level();
118-
119-
log::set_max_level(max_level);
120-
log::set_boxed_logger(Box::new(logger)).unwrap();
121-
}
122-
```
111+
- On Windows, it will enable colour output. _See `set_up_windows_color_terminal()`._
112+
- When using the `colors` *and* `stderr` features, it will instruct the `colored` library to display colors if STDERR
113+
is a terminal (instead of checking if STDOUT is a terminal). _See `use_stderr_for_colors()`._
123114

124115
Licence
125116
-------

examples/stderr.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use simple_logger::SimpleLogger;
2+
3+
fn main() {
4+
SimpleLogger::new().with_colors(true).init().unwrap();
5+
6+
log::warn!("This is an example message.");
7+
}

examples/wrap.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use log::{Log, Metadata, Record};
2-
use simple_logger::{set_up_color_terminal, SimpleLogger};
2+
use simple_logger::SimpleLogger;
33

44
struct WrapperLogger {
55
simple_logger: SimpleLogger,
@@ -20,11 +20,11 @@ impl Log for WrapperLogger {
2020
}
2121

2222
fn main() {
23-
set_up_color_terminal();
24-
2523
let simple_logger = SimpleLogger::new();
2624
log::set_max_level(simple_logger.max_level());
2725

2826
let wrapper_logger = WrapperLogger { simple_logger };
2927
log::set_boxed_logger(Box::new(wrapper_logger)).unwrap();
28+
29+
log::warn!("This is an example message.");
3030
}

src/lib.rs

+15-28
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,11 @@ 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-
// Setup colors if needed. The implementation if feature dependent.
352-
set_up_color_terminal();
351+
#[cfg(all(windows, feature = "colored"))]
352+
set_up_windows_color_terminal();
353+
354+
#[cfg(all(feature = "colored", feature = "stderr"))]
355+
use_stderr_for_colors();
353356

354357
log::set_max_level(self.max_level());
355358
log::set_boxed_logger(Box::new(self))
@@ -481,11 +484,12 @@ impl Log for SimpleLogger {
481484
fn flush(&self) {}
482485
}
483486

484-
/// Configure the console to display colours - Windows + colored
487+
/// Configure the console to display colours.
485488
///
486-
/// This is only needed on Windows when using the 'colored' feature.
489+
/// This is only needed on Windows when using the 'colors' feature.
490+
/// It doesn't currently handle combining the 'colors' and 'stderr' features.
487491
#[cfg(all(windows, feature = "colors"))]
488-
pub fn set_up_color_terminal() {
492+
pub fn set_up_windows_color_terminal() {
489493
use std::io::{stdout, IsTerminal};
490494

491495
if stdout().is_terminal() {
@@ -513,32 +517,15 @@ pub fn set_up_color_terminal() {
513517
}
514518
}
515519

516-
/// Configure the console to display colours - Windows + !colored
517-
///
518-
/// This method does nothing if running on Windows with the colored feature disabled.
519-
#[cfg(all(windows, not(feature = "colors")))]
520-
pub fn set_up_color_terminal() {}
520+
/// The colored crate will disable colors when STDOUT is not a terminal. This method overrides this
521+
/// behaviour to check the status of STDERR instead.
522+
#[cfg(all(feature = "colored", feature = "stderr"))]
523+
fn use_stderr_for_colors() {
524+
use std::io::{stderr, IsTerminal};
521525

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"))]
527-
pub fn set_up_color_terminal() {
528-
#[cfg(feature = "colors")]
529-
{
530-
use std::io::{stderr, IsTerminal};
531-
colored::control::set_override(stderr().is_terminal());
532-
}
526+
colored::control::set_override(stderr().is_terminal());
533527
}
534528

535-
/// Configure the console to display colours - !Windows + !stderr
536-
///
537-
/// This method does nothing if not running on Windows with the colored feature and outputting on
538-
/// stdout.
539-
#[cfg(all(not(windows), not(feature = "stderr")))]
540-
pub fn set_up_color_terminal() {}
541-
542529
/// Initialise the logger with its default configuration.
543530
///
544531
/// Log messages will not be filtered.

0 commit comments

Comments
 (0)