void FilePersistThread::rollFileIfNecessary(QFile& file, bool notifyListenersIfRolled) { uint64_t now = usecTimestampNow(); if ((file.size() > MAX_LOG_SIZE) || (now - _lastRollTime) > MAX_LOG_AGE_USECS) { QString newFileName = getLogRollerFilename(); if (file.copy(newFileName)) { file.open(QIODevice::WriteOnly | QIODevice::Truncate); file.close(); qDebug() << "Rolled log file:" << newFileName; if (notifyListenersIfRolled) { emit rollingLogFile(newFileName); } _lastRollTime = now; } QStringList nameFilters; nameFilters << FILENAME_WILDCARD; QDir logQDir(FileUtils::standardPath(LOGS_DIRECTORY)); logQDir.setNameFilters(nameFilters); logQDir.setSorting(QDir::Time); QFileInfoList filesInDir = logQDir.entryInfoList(); qint64 totalSizeOfDir = 0; foreach(QFileInfo dirItm, filesInDir){ if (totalSizeOfDir < MAX_LOG_DIR_SIZE){ totalSizeOfDir += dirItm.size(); } else { QFile file(dirItm.filePath()); file.remove(); } } }
void rollFileIfNecessary(QFile& file) { uint64_t now = usecTimestampNow(); if ((file.size() > MAX_LOG_SIZE) || (now - _lastRollTime) > MAX_LOG_AGE_USECS) { QString newFileName = getLogRollerFilename(); if (file.copy(newFileName)) { _lastRollTime = now; file.open(QIODevice::WriteOnly | QIODevice::Truncate); file.close(); qDebug() << "Rolled log file: " << newFileName; } } }