From 1fb7ee65cc2c33936bec2885e2132975404688b4 Mon Sep 17 00:00:00 2001 From: Sam Clements Date: Tue, 30 Apr 2024 16:59:25 +0100 Subject: [PATCH] Tidy up handling of stderr+colors --- Cargo.toml | 4 ++++ README.md | 19 +++++-------------- examples/stderr.rs | 7 +++++++ examples/wrap.rs | 6 +++--- src/lib.rs | 43 +++++++++++++++---------------------------- 5 files changed, 34 insertions(+), 45 deletions(-) create mode 100644 examples/stderr.rs diff --git a/Cargo.toml b/Cargo.toml index c581dd7..b107b09 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/README.md b/README.md index 9fef9b3..a3be8c5 100644 --- a/README.md +++ b/README.md @@ -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 ------- diff --git a/examples/stderr.rs b/examples/stderr.rs new file mode 100644 index 0000000..ed72a5a --- /dev/null +++ b/examples/stderr.rs @@ -0,0 +1,7 @@ +use simple_logger::SimpleLogger; + +fn main() { + SimpleLogger::new().with_colors(true).init().unwrap(); + + log::warn!("This is an example message."); +} diff --git a/examples/wrap.rs b/examples/wrap.rs index 6d14204..779a8f4 100644 --- a/examples/wrap.rs +++ b/examples/wrap.rs @@ -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, @@ -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."); } diff --git a/src/lib.rs b/src/lib.rs index ee2ad1c..f29bf4d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)) @@ -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() { @@ -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.