Skip to content

Commit 038cc34

Browse files
fix: emit CRLF in log output when stderr is a terminal
Child processes such as sudo (with use_pty, the default on modern distros) put the controlling terminal into raw mode while they run, disabling ONLCR. Log lines written by the stderr-relay thread during that window arrived as bare LF, so the cursor never returned to column 0 and subsequent lines cascaded to the right. Closes #91.
1 parent bd3bb0c commit 038cc34

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

src/logger.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1+
use std::io::{IsTerminal, Write};
2+
13
use env_logger::Env;
24

35
pub fn try_init_default_logger() {
46
let _ = env_logger::Builder::from_env(Env::default().default_filter_or("info"))
57
.format_target(false)
68
.format_timestamp(None)
9+
.format(|buf, record| {
10+
// Use CRLF when writing to a terminal. A child process such as
11+
// `sudo` (with `use_pty`) may put the controlling terminal into
12+
// raw mode while it runs, leaving ONLCR disabled — so a bare LF
13+
// from the stderr-relay thread would not return the cursor to
14+
// column 0 and subsequent log lines would cascade to the right.
15+
let eol = if std::io::stderr().is_terminal() {
16+
"\r\n"
17+
} else {
18+
"\n"
19+
};
20+
let level_style = buf.default_level_style(record.level());
21+
write!(
22+
buf,
23+
"[{level_style}{:<5}{level_style:#}] {}{eol}",
24+
record.level(),
25+
record.args(),
26+
)
27+
})
728
.try_init();
829
}
930

0 commit comments

Comments
 (0)