Skip to content

Commit 7af29ab

Browse files
committed
log-color: Detect TTY based on stderr, not stdout
Logging goes to stderr, not stdout, so we should base our automated detection on stderr instead of stdout. Thanks to Ralf Jung for noticing and reporting the bug!
1 parent f30733a commit 7af29ab

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
@@ -566,6 +566,25 @@ fn stdout_isatty() -> bool {
566566
}
567567
}
568568

569+
// FIXME remove these and use winapi 0.3 instead
570+
#[cfg(unix)]
571+
fn stderr_isatty() -> bool {
572+
unsafe { libc::isatty(libc::STDERR_FILENO) != 0 }
573+
}
574+
575+
#[cfg(windows)]
576+
fn stderr_isatty() -> bool {
577+
use winapi::um::consoleapi::GetConsoleMode;
578+
use winapi::um::processenv::GetStdHandle;
579+
use winapi::um::winbase::STD_ERROR_HANDLE;
580+
581+
unsafe {
582+
let handle = GetStdHandle(STD_ERROR_HANDLE);
583+
let mut out = 0;
584+
GetConsoleMode(handle, &mut out) != 0
585+
}
586+
}
587+
569588
fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) {
570589
let normalised =
571590
if code.starts_with('E') { code.to_string() } else { format!("E{0:0>4}", code) };
@@ -1290,7 +1309,7 @@ pub fn init_env_logger(env: &str) {
12901309
Ok(value) => match value.as_ref() {
12911310
"always" => true,
12921311
"never" => false,
1293-
"auto" => stdout_isatty(),
1312+
"auto" => stderr_isatty(),
12941313
_ => early_error(
12951314
ErrorOutputType::default(),
12961315
&format!(
@@ -1299,7 +1318,7 @@ pub fn init_env_logger(env: &str) {
12991318
),
13001319
),
13011320
},
1302-
Err(std::env::VarError::NotPresent) => stdout_isatty(),
1321+
Err(std::env::VarError::NotPresent) => stderr_isatty(),
13031322
Err(std::env::VarError::NotUnicode(_value)) => early_error(
13041323
ErrorOutputType::default(),
13051324
"non-Unicode log color value: expected one of always, never, or auto",

0 commit comments

Comments
 (0)