Skip to content

Commit 1c389ff

Browse files
committed
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`
2 parents 7445993 + 173a7db commit 1c389ff

File tree

1 file changed

+21
-2
lines changed
  • compiler/rustc_driver/src

1 file changed

+21
-2
lines changed

compiler/rustc_driver/src/lib.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -1284,11 +1284,30 @@ pub fn init_env_logger(env: &str) {
12841284
Ok(s) if s.is_empty() => return,
12851285
Ok(_) => {}
12861286
}
1287+
let color_logs = match std::env::var(String::from(env) + "_COLOR") {
1288+
Ok(value) => match value.as_ref() {
1289+
"always" => true,
1290+
"never" => false,
1291+
"auto" => stdout_isatty(),
1292+
_ => early_error(
1293+
ErrorOutputType::default(),
1294+
&format!(
1295+
"invalid log color value '{}': expected one of always, never, or auto",
1296+
value
1297+
),
1298+
),
1299+
},
1300+
Err(std::env::VarError::NotPresent) => stdout_isatty(),
1301+
Err(std::env::VarError::NotUnicode(_value)) => early_error(
1302+
ErrorOutputType::default(),
1303+
"non-Unicode log color value: expected one of always, never, or auto",
1304+
),
1305+
};
12871306
let filter = tracing_subscriber::EnvFilter::from_env(env);
12881307
let layer = tracing_tree::HierarchicalLayer::default()
12891308
.with_writer(io::stderr)
12901309
.with_indent_lines(true)
1291-
.with_ansi(true)
1310+
.with_ansi(color_logs)
12921311
.with_targets(true)
12931312
.with_wraparound(10)
12941313
.with_verbose_exit(true)
@@ -1314,7 +1333,7 @@ pub fn main() -> ! {
13141333
arg.into_string().unwrap_or_else(|arg| {
13151334
early_error(
13161335
ErrorOutputType::default(),
1317-
&format!("Argument {} is not valid Unicode: {:?}", i, arg),
1336+
&format!("argument {} is not valid Unicode: {:?}", i, arg),
13181337
)
13191338
})
13201339
})

0 commit comments

Comments
 (0)