Allow forcing `zebrad` to use color output (#3547)

* Allow forcing colored output in `zebrad`

Add a configuration item that allows forcing Zebra to output in color
mode even if the output device is not a terminal.

* Allow enabling colored output from Zebra in tests

Force Zebrad instances to use colored output if the
`ZEBRA_FORCE_USE_COLOR` environment variable is set.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Janito Vaqueiro Ferreira Filho 2022-02-16 21:09:04 -03:00 committed by GitHub
parent 61d3243da5
commit 3ca653120a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 3 deletions

View File

@ -21,8 +21,10 @@ impl Tracing {
let filter = config.filter.unwrap_or_else(|| "".to_string()); let filter = config.filter.unwrap_or_else(|| "".to_string());
let flame_root = &config.flamegraph; let flame_root = &config.flamegraph;
// Only use color if tracing output is being sent to a terminal // Only use color if tracing output is being sent to a terminal or if it was explicitly
let use_color = config.use_color && atty::is(atty::Stream::Stdout); // forced to.
let use_color =
config.force_use_color || (config.use_color && atty::is(atty::Stream::Stdout));
// Construct a tracing subscriber with the supplied filter and enable reloading. // Construct a tracing subscriber with the supplied filter and enable reloading.
let builder = FmtSubscriber::builder() let builder = FmtSubscriber::builder()

View File

@ -57,6 +57,14 @@ pub struct TracingSection {
/// terminals. /// terminals.
pub use_color: bool, pub use_color: bool,
/// Whether to force the use of colored terminal output, even if it's not available.
///
/// Will force Zebra to use colored terminal output even if it does not detect that the output
/// is a terminal that supports colors.
///
/// Defaults to `false`, which keeps the behavior of `use_color`.
pub force_use_color: bool,
/// The filter used for tracing events. /// The filter used for tracing events.
/// ///
/// The filter is used to create a `tracing-subscriber` /// The filter is used to create a `tracing-subscriber`
@ -120,6 +128,7 @@ impl Default for TracingSection {
fn default() -> Self { fn default() -> Self {
Self { Self {
use_color: true, use_color: true,
force_use_color: false,
filter: None, filter: None,
endpoint_addr: None, endpoint_addr: None,
flamegraph: None, flamegraph: None,

View File

@ -42,7 +42,7 @@ use zebra_test::{
}; };
use zebrad::{ use zebrad::{
components::{mempool, sync}, components::{mempool, sync},
config::{SyncSection, ZebradConfig}, config::{SyncSection, TracingSection, ZebradConfig},
}; };
/// The amount of time we wait after launching `zebrad`. /// The amount of time we wait after launching `zebrad`.
@ -82,12 +82,22 @@ fn default_test_config() -> Result<ZebradConfig> {
..zebra_consensus::Config::default() ..zebra_consensus::Config::default()
}; };
let force_use_color = !matches!(
env::var("ZEBRA_FORCE_USE_COLOR"),
Err(env::VarError::NotPresent)
);
let tracing = TracingSection {
force_use_color,
..TracingSection::default()
};
let config = ZebradConfig { let config = ZebradConfig {
network, network,
state: zebra_state::Config::ephemeral(), state: zebra_state::Config::ephemeral(),
sync, sync,
mempool, mempool,
consensus, consensus,
tracing,
..ZebradConfig::default() ..ZebradConfig::default()
}; };