From d8c29809f4e3265907041697173ccaca01519f99 Mon Sep 17 00:00:00 2001 From: teor Date: Wed, 21 Jun 2023 20:17:26 +1000 Subject: [PATCH] Refactor terminal color checks, fix force_color on panic logs (#6997) --- zebrad/src/application.rs | 10 ++-------- zebrad/src/components/tracing.rs | 15 +++++++++++++++ zebrad/src/components/tracing/component.rs | 9 ++++----- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/zebrad/src/application.rs b/zebrad/src/application.rs index 68134f7d8..d7fb3356d 100644 --- a/zebrad/src/application.rs +++ b/zebrad/src/application.rs @@ -166,11 +166,6 @@ pub struct ZebradApp { } impl ZebradApp { - /// Are standard output and standard error both connected to ttys? - fn outputs_are_ttys() -> bool { - atty::is(atty::Stream::Stdout) && atty::is(atty::Stream::Stderr) - } - /// Returns the git commit for this build, if available. /// /// @@ -215,8 +210,7 @@ impl Application for ZebradApp { // of the `color_eyre::install` part of `Terminal::new` without // ColorChoice::Never? - // The Tracing component uses stdout directly and will apply colors - // `if Self::outputs_are_ttys() && config.tracing.use_colors` + // The Tracing component uses stdout directly and will apply colors automatically. // // Note: It's important to use `ColorChoice::Never` here to avoid panicking in // `register_components()` below if `color_eyre::install()` is called @@ -257,7 +251,7 @@ impl Application for ZebradApp { let config = command.process_config(config)?; - let theme = if Self::outputs_are_ttys() && config.tracing.use_color { + let theme = if config.tracing.use_color_stdout_and_stderr() { color_eyre::config::Theme::dark() } else { color_eyre::config::Theme::new() diff --git a/zebrad/src/components/tracing.rs b/zebrad/src/components/tracing.rs index 3aa27f4c7..d12491f87 100644 --- a/zebrad/src/components/tracing.rs +++ b/zebrad/src/components/tracing.rs @@ -131,6 +131,21 @@ pub struct Config { pub use_journald: bool, } +impl Config { + /// Returns `true` if standard output should use color escapes. + /// Automatically checks if Zebra is running in a terminal. + pub fn use_color_stdout(&self) -> bool { + self.force_use_color || (self.use_color && atty::is(atty::Stream::Stdout)) + } + + /// Returns `true` if output that could go to standard output or standard error + /// should use color escapes. Automatically checks if Zebra is running in a terminal. + pub fn use_color_stdout_and_stderr(&self) -> bool { + self.force_use_color + || (self.use_color && atty::is(atty::Stream::Stdout) && atty::is(atty::Stream::Stderr)) + } +} + impl Default for Config { fn default() -> Self { #[cfg(feature = "progress-bar")] diff --git a/zebrad/src/components/tracing/component.rs b/zebrad/src/components/tracing/component.rs index 8f32e0943..4a3a4560e 100644 --- a/zebrad/src/components/tracing/component.rs +++ b/zebrad/src/components/tracing/component.rs @@ -54,6 +54,10 @@ impl Tracing { /// Try to create a new [`Tracing`] component with the given `filter`. #[allow(clippy::print_stdout, clippy::print_stderr)] pub fn new(config: Config) -> Result { + // Only use color if tracing output is being sent to a terminal or if it was explicitly + // forced to. + let use_color = config.use_color_stdout(); + let filter = config.filter.unwrap_or_default(); let flame_root = &config.flamegraph; @@ -98,11 +102,6 @@ impl Tracing { .buffered_lines_limit(config.buffer_limit.max(100)) .finish(writer); - // Only use color if tracing output is being sent to a terminal or if it was explicitly - // forced to. - let use_color = - config.force_use_color || (config.use_color && atty::is(atty::Stream::Stdout)); - // Construct a format subscriber with the supplied global logging filter, // and optionally enable reloading. //