From 473d54ed5f1798e43fe93b8383c3b2c06826a4d1 Mon Sep 17 00:00:00 2001 From: dramforever Date: Thu, 26 Feb 2026 00:51:31 +0800 Subject: [PATCH 1/2] libutil/logging: Generalize writeToStderr into writeFullLogging Generalize writeToStderr into writeFullLogging, with similar behavior but taking an arbitrary fd, and reimplement writeToStderr with it as a convenient wrapper. --- src/libutil/logging.cc | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index 1aab24041b2..23edb09c948 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -150,18 +150,23 @@ class SimpleLogger : public Logger Verbosity verbosity = lvlInfo; -void writeToStderr(std::string_view s) +static void writeFullLogging(Descriptor fd, std::string_view s) { try { - writeFull(getStandardError(), s, false); + writeFull(fd, s, false); } catch (SystemError & e) { - /* Ignore failing writes to stderr. We need to ignore write - errors to ensure that cleanup code that logs to stderr runs - to completion if the other side of stderr has been closed - unexpectedly. */ + /* Ignore failing logging writes. We need to ignore write + errors to ensure that cleanup code that writes logs runs + to completion if the other side of the logging fd has + been closed unexpectedly. */ } } +void writeToStderr(std::string_view s) +{ + writeFullLogging(getStandardError(), s); +} + std::unique_ptr makeSimpleLogger(bool printBuildLogs) { return std::make_unique(printBuildLogs); From 12101004216089329cc2831608be07ef84cd7404 Mon Sep 17 00:00:00 2001 From: dramforever Date: Thu, 26 Feb 2026 01:04:05 +0800 Subject: [PATCH 2/2] libutil/logging: Use writeFullLogging in JSONLogger::write Logging should not check for interrupts. Use the new writeFullLogging function to write JSON output to get similar behavior to SimpleLogger. This avoids the logger itself throwing Interrupted exceptions in handleExceptions. --- src/libutil/logging.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index 23edb09c948..f1c8190f7db 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -250,15 +250,15 @@ struct JSONLogger : Logger void write(const nlohmann::json & json) { - auto line = - (includeNixPrefix ? "@nix " : "") + json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace); + auto line = (includeNixPrefix ? "@nix " : "") + + json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace) + "\n"; /* Acquire a lock to prevent log messages from clobbering each other. */ try { auto state(_state.lock()); if (state->enabled) - writeLine(fd, line); + writeFullLogging(fd, line); } catch (...) { bool enabled = false; std::swap(_state.lock()->enabled, enabled);