Auto merge of #78548 - camelid:driver-tty, r=oli-obk

driver: Only output ANSI logging if connected to a terminal

Fixes #78435.

See #78435 for more.

Cc `@RalfJung` `@oli-obk`
This commit is contained in:
bors 2020-11-24 20:58:20 +00:00
commit 1c389ffeff

View File

@ -1284,11 +1284,30 @@ pub fn init_env_logger(env: &str) {
Ok(s) if s.is_empty() => return, Ok(s) if s.is_empty() => return,
Ok(_) => {} Ok(_) => {}
} }
let color_logs = match std::env::var(String::from(env) + "_COLOR") {
Ok(value) => match value.as_ref() {
"always" => true,
"never" => false,
"auto" => stdout_isatty(),
_ => early_error(
ErrorOutputType::default(),
&format!(
"invalid log color value '{}': expected one of always, never, or auto",
value
),
),
},
Err(std::env::VarError::NotPresent) => stdout_isatty(),
Err(std::env::VarError::NotUnicode(_value)) => early_error(
ErrorOutputType::default(),
"non-Unicode log color value: expected one of always, never, or auto",
),
};
let filter = tracing_subscriber::EnvFilter::from_env(env); let filter = tracing_subscriber::EnvFilter::from_env(env);
let layer = tracing_tree::HierarchicalLayer::default() let layer = tracing_tree::HierarchicalLayer::default()
.with_writer(io::stderr) .with_writer(io::stderr)
.with_indent_lines(true) .with_indent_lines(true)
.with_ansi(true) .with_ansi(color_logs)
.with_targets(true) .with_targets(true)
.with_wraparound(10) .with_wraparound(10)
.with_verbose_exit(true) .with_verbose_exit(true)
@ -1314,7 +1333,7 @@ pub fn main() -> ! {
arg.into_string().unwrap_or_else(|arg| { arg.into_string().unwrap_or_else(|arg| {
early_error( early_error(
ErrorOutputType::default(), ErrorOutputType::default(),
&format!("Argument {} is not valid Unicode: {:?}", i, arg), &format!("argument {} is not valid Unicode: {:?}", i, arg),
) )
}) })
}) })