1
Fork 0
mirror of https://github.com/redstrate/Astra.git synced 2025-04-22 04:37:46 +00:00

Change log filename format

This commit is contained in:
Joshua Goins 2023-12-21 20:52:14 -05:00
parent 53603f4032
commit 42dfce96ef

View file

@ -36,23 +36,21 @@ public:
void initialize() void initialize()
{ {
const QString filename{QStringLiteral("astra.log")};
const QDir logDirectory = Utility::stateDirectory().absoluteFilePath(QStringLiteral("log")); const QDir logDirectory = Utility::stateDirectory().absoluteFilePath(QStringLiteral("log"));
Utility::createPathIfNeeded(logDirectory); Utility::createPathIfNeeded(logDirectory);
// Sort them from highest to lowest (4, 3, 2, 1, 0) // Sort them from highest to lowest (4, 3, 2, 1, 0)
auto existingLogEntries = logDirectory.entryList({filename + QStringLiteral(".*")}); auto existingLogEntries = logDirectory.entryList({QStringLiteral("astra.*.log")});
std::sort(existingLogEntries.begin(), existingLogEntries.end(), [](const auto &left, const auto &right) { std::sort(existingLogEntries.begin(), existingLogEntries.end(), [](const auto &left, const auto &right) {
auto leftIndex = left.split(QStringLiteral(".")).last().toInt(); auto leftIndex = left.split(QStringLiteral("."))[1].toInt();
auto rightIndex = right.split(QStringLiteral(".")).last().toInt(); auto rightIndex = right.split(QStringLiteral("."))[1].toInt();
return leftIndex > rightIndex; return leftIndex > rightIndex;
}); });
// Iterate through the existing logs, prune them and move the oldest up // Iterate through the existing logs, prune them and move the oldest up
for (const auto &entry : existingLogEntries) { for (const auto &entry : existingLogEntries) {
bool valid = false; bool valid = false;
const auto index = entry.split(QStringLiteral(".")).last().toInt(&valid); const auto index = entry.split(QStringLiteral("."))[1].toInt(&valid);
if (!valid) { if (!valid) {
continue; continue;
} }
@ -65,7 +63,7 @@ public:
existingFile.remove(); existingFile.remove();
continue; continue;
} }
const auto &newName = logDirectory.absoluteFilePath(QStringLiteral("%1.%2").arg(filename, QString::number(index + 1))); const auto &newName = logDirectory.absoluteFilePath(QStringLiteral("astra.%1.log").arg(QString::number(index + 1)));
const auto success = existingFile.copy(newName); const auto success = existingFile.copy(newName);
if (success) { if (success) {
existingFile.remove(); existingFile.remove();
@ -78,7 +76,7 @@ public:
} }
} }
file.setFileName(logDirectory.absoluteFilePath(filename + QStringLiteral(".0"))); file.setFileName(logDirectory.absoluteFilePath(QStringLiteral("astra.0.log")));
file.open(QIODevice::WriteOnly | QIODevice::Unbuffered); file.open(QIODevice::WriteOnly | QIODevice::Unbuffered);
} }