From a00fbae7c5f37f87af30a1e34d2c217262bd29ea Mon Sep 17 00:00:00 2001 From: Ashod Nakashian Date: Sun, 20 Aug 2023 08:25:50 -0400 Subject: [PATCH] wsd: log: retry on write failure and flush Change-Id: I7417a74959a52e2a079bfe4502ed238d1517ea60 Signed-off-by: Ashod Nakashian --- common/Log.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/common/Log.cpp b/common/Log.cpp index 195a36e43..7cb27255b 100644 --- a/common/Log.cpp +++ b/common/Log.cpp @@ -42,10 +42,28 @@ namespace Log static constexpr std::size_t BufferSize = 64 * 1024; public: + void close() override { flush(); } + /// Write the given buffer to stderr directly. - static inline bool writeRaw(const char* data, std::size_t size) + static inline int writeRaw(const char* data, std::size_t size) { - return ::write(STDERR_FILENO, data, size) == static_cast(size); + std::size_t i = 0; + for (; i < size;) + { + int wrote; + while ((wrote = ::write(STDERR_FILENO, data + i, size - i)) < 0 && errno == EINTR) + { + } + + if (wrote < 0) + { + return i; + } + + i += wrote; + } + + return i; } template inline void writeRaw(const char (&data)[N])