From d282aca273d9a765b32d38db85d2e1ce7d588a1f Mon Sep 17 00:00:00 2001 From: Camelid Date: Thu, 29 Oct 2020 16:54:59 -0700 Subject: [PATCH 1/3] driver: Only output ANSI if connected to a terminal See #78435 for more. --- compiler/rustc_driver/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index 29771bee9ae..9b5b08bce61 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -1286,7 +1286,7 @@ pub fn init_env_logger(env: &str) { let filter = tracing_subscriber::EnvFilter::from_env(env); let layer = tracing_tree::HierarchicalLayer::default() .with_indent_lines(true) - .with_ansi(true) + .with_ansi(stdout_isatty()) .with_targets(true) .with_thread_ids(true) .with_thread_names(true) From 8ad1a1cf398622e6e179726aeadaf19cca7677d9 Mon Sep 17 00:00:00 2001 From: Camelid Date: Fri, 30 Oct 2020 14:11:46 -0700 Subject: [PATCH 2/3] driver: Add env var to control log colors The log color variable is whatever the log variable is (`RUSTC_LOG`, `RUSTDOC_LOG`, `MIRI_LOG`, etc.) + `_COLOR`. So `RUSTC_LOG_COLOR`, `RUSTDOC_LOG_COLOR`, `MIRI_LOG_COLOR`, etc. Thanks to @RalfJung for suggesting this! It was much easier to implement than adding a new unstable argument, which is what I tried before. --- compiler/rustc_driver/src/lib.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index 9b5b08bce61..6c70faf080f 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -1283,10 +1283,22 @@ pub fn init_env_logger(env: &str) { Ok(s) if s.is_empty() => return, 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(), + _ => panic!("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)) => { + panic!("non-unicode log color value: expected one of always, never, or auto") + } + }; let filter = tracing_subscriber::EnvFilter::from_env(env); let layer = tracing_tree::HierarchicalLayer::default() .with_indent_lines(true) - .with_ansi(stdout_isatty()) + .with_ansi(color_logs) .with_targets(true) .with_thread_ids(true) .with_thread_names(true) From 173a7dbace4f3d9921dd1c66830c847aff9ba23b Mon Sep 17 00:00:00 2001 From: Camelid Date: Mon, 23 Nov 2020 17:38:20 -0800 Subject: [PATCH 3/3] Use `early_error` --- compiler/rustc_driver/src/lib.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index 6c70faf080f..40a32cf32f2 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -1288,12 +1288,19 @@ pub fn init_env_logger(env: &str) { "always" => true, "never" => false, "auto" => stdout_isatty(), - _ => panic!("invalid log color value '{}': expected one of always, never, or auto", value), + _ => 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)) => { - panic!("non-unicode log color value: expected one of always, never, or auto") - } + 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 layer = tracing_tree::HierarchicalLayer::default() @@ -1324,7 +1331,7 @@ pub fn main() -> ! { arg.into_string().unwrap_or_else(|arg| { early_error( ErrorOutputType::default(), - &format!("Argument {} is not valid Unicode: {:?}", i, arg), + &format!("argument {} is not valid Unicode: {:?}", i, arg), ) }) })