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.
This commit is contained in:
Morph 2023-06-24 02:30:06 +03:00 committed by GPUCode
parent b57773b1cf
commit 8e8ca7d9d0

View file

@ -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();