From 3303d2c7dbc03eb2d40185029ce2efc3926ec82a Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Wed, 7 Jun 2023 15:57:16 +0200 Subject: [PATCH] Keep last 50 log files Keeping just the last two probably leads to important info being lost. --- src/logger.cpp | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/logger.cpp b/src/logger.cpp index 942023339..382597974 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -131,22 +131,33 @@ public: } auto filePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QDir::separator() + appName; - QFileInfo infoOld(filePath + QLatin1String(".old")); - if (infoOld.exists()) { - QFile fileOld(infoOld.absoluteFilePath()); - const bool success = fileOld.remove(); - if (!success) { - qFatal("Cannot remove old log file '%s': %s", qUtf8Printable(fileOld.fileName()), qUtf8Printable(fileOld.errorString())); + QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QDir::separator()); + auto entryList = dir.entryList({appName + QStringLiteral(".*")}); + std::sort(entryList.begin(), entryList.end(), [](const auto &left, const auto &right) { + return left > right; + }); + for (const auto &entry : entryList) { + bool ok = false; + const auto index = entry.split(".").last().toInt(&ok); + if (!ok) { + continue; } - } - - QFileInfo info(filePath); - if (info.exists()) { - QFile file(info.absoluteFilePath()); - const QString oldName = filePath + QLatin1String(".old"); - const bool success = file.copy(oldName); - if (!success) { - qFatal("Cannot rename log file '%s' to '%s': %s", qUtf8Printable(file.fileName()), qUtf8Printable(oldName), qUtf8Printable(file.errorString())); + QFileInfo info(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QDir::separator() + entry); + if (info.exists()) { + QFile file(info.absoluteFilePath()); + if (index > 50) { + file.remove(); + } + const QString newName = filePath + QStringLiteral(".%1").arg(index + 1); + const bool success = file.copy(newName); + if (success) { + file.remove(); + } else { + qFatal("Cannot rename log file '%s' to '%s': %s", + qUtf8Printable(file.fileName()), + qUtf8Printable(newName), + qUtf8Printable(file.errorString())); + } } } @@ -154,7 +165,7 @@ public: if (!finfo.absoluteDir().exists()) { QDir().mkpath(finfo.absolutePath()); } - file.setFileName(filePath); + file.setFileName(filePath + QStringLiteral(".0")); file.open(QIODevice::WriteOnly | QIODevice::Unbuffered); }