Keep last 50 log files

Keeping just the last two probably leads to important info being lost.
This commit is contained in:
Tobias Fella
2023-06-07 15:57:16 +02:00
parent 22107fc598
commit 3303d2c7db

View File

@@ -131,22 +131,33 @@ public:
} }
auto filePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QDir::separator() + appName; auto filePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QDir::separator() + appName;
QFileInfo infoOld(filePath + QLatin1String(".old")); QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QDir::separator());
if (infoOld.exists()) { auto entryList = dir.entryList({appName + QStringLiteral(".*")});
QFile fileOld(infoOld.absoluteFilePath()); std::sort(entryList.begin(), entryList.end(), [](const auto &left, const auto &right) {
const bool success = fileOld.remove(); return left > right;
if (!success) { });
qFatal("Cannot remove old log file '%s': %s", qUtf8Printable(fileOld.fileName()), qUtf8Printable(fileOld.errorString())); for (const auto &entry : entryList) {
bool ok = false;
const auto index = entry.split(".").last().toInt(&ok);
if (!ok) {
continue;
} }
} QFileInfo info(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QDir::separator() + entry);
if (info.exists()) {
QFileInfo info(filePath); QFile file(info.absoluteFilePath());
if (info.exists()) { if (index > 50) {
QFile file(info.absoluteFilePath()); file.remove();
const QString oldName = filePath + QLatin1String(".old"); }
const bool success = file.copy(oldName); const QString newName = filePath + QStringLiteral(".%1").arg(index + 1);
if (!success) { const bool success = file.copy(newName);
qFatal("Cannot rename log file '%s' to '%s': %s", qUtf8Printable(file.fileName()), qUtf8Printable(oldName), qUtf8Printable(file.errorString())); 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()) { if (!finfo.absoluteDir().exists()) {
QDir().mkpath(finfo.absolutePath()); QDir().mkpath(finfo.absolutePath());
} }
file.setFileName(filePath); file.setFileName(filePath + QStringLiteral(".0"));
file.open(QIODevice::WriteOnly | QIODevice::Unbuffered); file.open(QIODevice::WriteOnly | QIODevice::Unbuffered);
} }