Skip to content

Commit 8ad1a1c

Browse files
committed
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.
1 parent d282aca commit 8ad1a1c

File tree

1 file changed

+13
-1
lines changed
  • compiler/rustc_driver/src

1 file changed

+13
-1
lines changed

compiler/rustc_driver/src/lib.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1283,10 +1283,22 @@ pub fn init_env_logger(env: &str) {
12831283
Ok(s) if s.is_empty() => return,
12841284
Ok(_) => {}
12851285
}
1286+
let color_logs = match std::env::var(String::from(env) + "_COLOR") {
1287+
Ok(value) => match value.as_ref() {
1288+
"always" => true,
1289+
"never" => false,
1290+
"auto" => stdout_isatty(),
1291+
_ => panic!("invalid log color value '{}': expected one of always, never, or auto", value),
1292+
},
1293+
Err(std::env::VarError::NotPresent) => stdout_isatty(),
1294+
Err(std::env::VarError::NotUnicode(_value)) => {
1295+
panic!("non-unicode log color value: expected one of always, never, or auto")
1296+
}
1297+
};
12861298
let filter = tracing_subscriber::EnvFilter::from_env(env);
12871299
let layer = tracing_tree::HierarchicalLayer::default()
12881300
.with_indent_lines(true)
1289-
.with_ansi(stdout_isatty())
1301+
.with_ansi(color_logs)
12901302
.with_targets(true)
12911303
.with_thread_ids(true)
12921304
.with_thread_names(true)

0 commit comments

Comments
 (0)