From 10dacf3d3b13b77789fdbafbd143449e6d4ab0e6 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 8 Apr 2022 07:26:32 -0700 Subject: [PATCH] ignore errors when writing to logs For errors like eg: ENOSPC we shouldn't terminate the program when logging output. refs: https://github.com/wez/wezterm/issues/1839 --- env-bootstrap/src/ringlog.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/env-bootstrap/src/ringlog.rs b/env-bootstrap/src/ringlog.rs index 547abfbd5..54c237b35 100644 --- a/env-bootstrap/src/ringlog.rs +++ b/env-bootstrap/src/ringlog.rs @@ -182,17 +182,26 @@ impl log::Log for Logger { let reset = if self.is_tty { "\u{1b}[0m" } else { "" }; let target_color = if self.is_tty { "\u{1b}[1m" } else { "" }; - eprintln!( - "{} {level_color}{:6}{reset} {target_color}{:padding$}{reset} > {}", - ts, - level, - target, - msg, - padding = padding, - level_color = level_color, - reset = reset, - target_color = target_color - ); + { + // We use writeln! here rather than eprintln! so that we can ignore + // a failed log write in the case that stderr has been redirected + // to a device that is out of disk space. + // + let mut stderr = std::io::stderr(); + let _ = writeln!( + stderr, + "{} {level_color}{:6}{reset} {target_color}{:padding$}{reset} > {}", + ts, + level, + target, + msg, + padding = padding, + level_color = level_color, + reset = reset, + target_color = target_color + ); + let _ = stderr.flush(); + } let mut file = self.file.lock().unwrap(); if file.is_none() {