From 8e8ca7d9d02388eefa1b1afa73b935349a6165a0 Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Sat, 24 Jun 2023 02:30:06 +0300 Subject: [PATCH] common: logging: backend: Close the file after exceeding the write limit There's no point in keeping the file open after the write limit is exceeded. This allows the file to be committed to the disk shortly after it is closed and avoids redundantly checking whether or not the write limit is exceeded. --- src/common/logging/backend.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 4afba68330..3c8f8f54d5 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -173,12 +173,19 @@ FileBackend::FileBackend(const std::string& filename) { FileBackend::~FileBackend() = default; void FileBackend::Write(const Entry& entry) { - // prevent logs from going over the maximum size (in case its spamming and the user doesn't - // know) - constexpr std::size_t MAX_BYTES_WRITTEN = 50 * 1024L * 1024L; - if (!file->IsOpen() || bytes_written > MAX_BYTES_WRITTEN) { + if (!file->IsOpen()) { return; } + + // Prevent logs from exceeding a set maximum size in the event that log entries are spammed. + constexpr std::size_t MAX_BYTES_WRITTEN = 50 * 1024L * 1024L; + + // Close the file after the write limit is exceeded. + if (bytes_written > MAX_BYTES_WRITTEN) { + file->Close(); + return; + } + bytes_written += file->WriteString(FormatLogMessage(entry).append(1, '\n')); if (entry.log_level >= Level::Error) { file->Flush();