ringlog: make printing to stderr faster

I was trying to debug an issue I'm seeing with wezterm and figured I
would try to use the terminal for my day-to-day with trace logds enabled
while I try to figure it out.

Unfortunately it turned out to be disruptive enough to make the terminal
almost unusable. I narrowed at least *that* part down to the fact that
printing out each log message would result in about screenful worth of
`write(2)` syscalls, many of them printing just one space character each
(padding? debug structure identation?)

Although it would be ideal to use a line-based buffered writer here,
pre-formatting the log string to a String before printing seemed like a
straightforward quick solution and it works amazingly well!
This commit is contained in:
Simonas Kazlauskas authored and Wez Furlong committed 2026-01-08 12:35:44 +00:00
1 parent 219d1ef33f
commit 4e8a7d0d86
1 file changed
+4 -2
+4 -2
View File
@@ -188,8 +188,9 @@ impl log::Log for Logger {
// to a device that is out of disk space.
// <https://github.com/wezterm/wezterm/issues/1839>
let mut stderr = std::io::stderr();
let _ = writeln!(
stderr,
// Direct `write!` will `write()` every single padding space as individual syscall
// which makes terminal with tracing logs enabled unusably slow.
let logline = format!(
"{} {level_color}{:6}{reset} {target_color}{:padding$}{reset} > {}",
ts,
level,
@@ -200,6 +201,7 @@ impl log::Log for Logger {
reset = reset,
target_color = target_color
);
let _ = stderr.write_all(logline.as_bytes());
let _ = stderr.flush();
}