Automatically disable colors when tracing to a file

This commit is contained in:
teor 2020-12-01 11:08:55 +10:00 committed by Henry de Valence
parent 6a075ed4b2
commit 97d1a81b7c
4 changed files with 11 additions and 2 deletions

1
Cargo.lock generated
View File

@ -3555,6 +3555,7 @@ name = "zebrad"
version = "3.0.0-alpha.0"
dependencies = [
"abscissa_core",
"atty",
"chrono",
"color-eyre",
"dirs",

View File

@ -39,6 +39,7 @@ metrics-exporter-prometheus = "0.1.0-alpha.10"
dirs = "3.0.1"
inferno = { version = "0.10.2", default-features = false }
atty = "0.2.14"
[build-dependencies]
vergen = "3.1.0"

View File

@ -21,9 +21,12 @@ impl Tracing {
let filter = config.filter.unwrap_or_else(|| "".to_string());
let flame_root = &config.flamegraph;
// Only use color if tracing output is being sent to a terminal
let use_color = config.use_color && atty::is(atty::Stream::Stdout);
// Construct a tracing subscriber with the supplied filter and enable reloading.
let builder = FmtSubscriber::builder()
.with_ansi(config.use_color)
.with_ansi(use_color)
.with_env_filter(filter)
.with_filter_reloading();
let filter_handle = builder.reload_handle();

View File

@ -45,7 +45,11 @@ pub struct ZebradConfig {
pub struct TracingSection {
/// Whether to use colored terminal output, if available.
///
/// Defaults to `true`.
/// Colored terminal output is automatically disabled if an output stream
/// is connected to a file. (Or another non-terminal device.)
///
/// Defaults to `true`, which automatically enables colored output to
/// terminals.
pub use_color: bool,
/// The filter used for tracing events.