Skip to content

Commit

Permalink
Tidy up handling of stderr+colors
Browse files Browse the repository at this point in the history
  • Loading branch information
borntyping committed Apr 30, 2024
1 parent 4c461f7 commit 1fb7ee6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 45 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ windows-sys = { version = "^0.48.0", features = ["Win32_System_Console", "Win32_
name = "colors"
required-features = ["colors"]

[[example]]
name = "stderr"
required-features = ["colors", "stderr"]

[[example]]
name = "threads"
required-features = ["threads"]
Expand Down
19 changes: 5 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,13 @@ You might want to wrap this logger to do your own processing before handing even
of calling `init()` which calls `log::set_max_level` and `log::set_boxed_logger`, you can call those functions directly
giving you the chance to wrap or adjust the logger. See [wrap.rs](examples/wrap.rs) for a more detailed example.

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

```rust
use simple_logger::{SimpleLogger, set_up_color_terminal};

fn main() {
set_up_color_terminal();
The `SimpleLogger.init()` function attempts to configure colours support as best it can in various situations:

let logger = SimpleLogger::new();
let max_level = logger.max_level();

log::set_max_level(max_level);
log::set_boxed_logger(Box::new(logger)).unwrap();
}
```
- On Windows, it will enable colour output. _See `set_up_windows_color_terminal()`._
- When using the `colors` *and* `stderr` features, it will instruct the `colored` library to display colors if STDERR
is a terminal (instead of checking if STDOUT is a terminal). _See `use_stderr_for_colors()`._

Licence
-------
Expand Down
7 changes: 7 additions & 0 deletions examples/stderr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use simple_logger::SimpleLogger;

fn main() {
SimpleLogger::new().with_colors(true).init().unwrap();

log::warn!("This is an example message.");
}
6 changes: 3 additions & 3 deletions examples/wrap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use log::{Log, Metadata, Record};
use simple_logger::{set_up_color_terminal, SimpleLogger};
use simple_logger::SimpleLogger;

struct WrapperLogger {
simple_logger: SimpleLogger,
Expand All @@ -20,11 +20,11 @@ impl Log for WrapperLogger {
}

fn main() {
set_up_color_terminal();

let simple_logger = SimpleLogger::new();
log::set_max_level(simple_logger.max_level());

let wrapper_logger = WrapperLogger { simple_logger };
log::set_boxed_logger(Box::new(wrapper_logger)).unwrap();

log::warn!("This is an example message.");
}
43 changes: 15 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,11 @@ 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> {
// Setup colors if needed. The implementation if feature dependent.
set_up_color_terminal();
#[cfg(all(windows, feature = "colored"))]
set_up_windows_color_terminal();

#[cfg(all(feature = "colored", feature = "stderr"))]
use_stderr_for_colors();

log::set_max_level(self.max_level());
log::set_boxed_logger(Box::new(self))
Expand Down Expand Up @@ -481,11 +484,12 @@ impl Log for SimpleLogger {
fn flush(&self) {}
}

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

if stdout().is_terminal() {
Expand Down Expand Up @@ -513,32 +517,15 @@ pub fn set_up_color_terminal() {
}
}

/// 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() {}
/// 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(feature = "colored", feature = "stderr"))]
fn use_stderr_for_colors() {
use std::io::{stderr, IsTerminal};

/// 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());
}
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 and outputting on
/// stdout.
#[cfg(all(not(windows), not(feature = "stderr")))]
pub fn set_up_color_terminal() {}

/// Initialise the logger with its default configuration.
///
/// Log messages will not be filtered.
Expand Down

0 comments on commit 1fb7ee6

Please sign in to comment.