Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/controller/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@
namespace
{

constexpr qint64 MAX_LOG_FILE_SIZE = 5 * 1024 * 1024; // 5 MB

void rotateLogFileIfTooLarge(const QString& logFilePath)
{
QFile existingLogFile {logFilePath};
if (existingLogFile.size() <= MAX_LOG_FILE_SIZE) {
return;
}
const auto rotatedLogFilePath = logFilePath + QStringLiteral(".1");
QFile::remove(rotatedLogFilePath);
existingLogFile.rename(rotatedLogFilePath);
}

bool openLogFile(QFile& logFile)
{
const auto logFilePath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
Expand All @@ -52,6 +65,7 @@ bool openLogFile(QFile& logFile)
return false;
}
logFile.setFileName(logFileDir.filePath(QStringLiteral("%1.log").arg(qApp->applicationName())));
rotateLogFileIfTooLarge(logFile.fileName());
if (!logFile.open(QIODevice::Append | QIODevice::Text)) {
std::cerr << "Unable to open logfile '" << logFile.fileName().toStdString() << '\''
<< std::endl;
Expand Down
Loading