From 428cfc5e1c1046708584327dd2d3089f9eccbf8d Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Tue, 10 Apr 2018 12:20:33 +0200 Subject: [PATCH] LogDir: Compress log when switching files #6442 --- src/libsync/logger.cpp | 44 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/libsync/logger.cpp b/src/libsync/logger.cpp index 40c7a2da5..5f5ebb05d 100644 --- a/src/libsync/logger.cpp +++ b/src/libsync/logger.cpp @@ -19,6 +19,8 @@ #include #include +#include + namespace OCC { static void mirallLogCatcher(QtMsgType type, const QMessageLogContext &ctx, const QString &message) @@ -181,18 +183,41 @@ void Logger::setLogDebug(bool debug) _logDebug = debug; } +static bool compressLog(const QString &originalName, const QString &targetName) +{ + QFile original(originalName); + if (!original.open(QIODevice::ReadOnly)) + return false; + auto compressed = gzopen(targetName.toUtf8(), "wb"); + if (!compressed) { + return false; + } + + while (!original.atEnd()) { + auto data = original.read(1024 * 1024); + auto written = gzwrite(compressed, data.data(), data.size()); + if (written != data.size()) { + gzclose(compressed); + return false; + } + } + gzclose(compressed); + return true; +} + void Logger::enterNextLogFile() { if (!_logDirectory.isEmpty()) { + QDir dir(_logDirectory); if (!dir.exists()) { dir.mkpath("."); } // Find out what is the file with the highest number if any - QStringList files = dir.entryList(QStringList("owncloud.log.*"), + QStringList files = dir.entryList(QStringList("*owncloud.log.*"), QDir::Files); - QRegExp rx("owncloud.log.(\\d+)"); + QRegExp rx(R"(.*owncloud\.log\.(\d+).*)"); uint maxNumber = 0; QDateTime now = QDateTime::currentDateTimeUtc(); foreach (const QString &s, files) { @@ -207,8 +232,21 @@ void Logger::enterNextLogFile() } } - QString filename = _logDirectory + "/owncloud.log." + QString::number(maxNumber + 1); + QString filename = _logDirectory + "/" + + QDateTime::currentDateTime().toString("yyyyMMdd_HHmm") + + "_owncloud.log." + + QString::number(maxNumber + 1); + auto previousLog = _logFile.fileName(); setLogFile(filename); + + if (!previousLog.isEmpty()) { + QString compressedName = previousLog + ".gz"; + if (compressLog(previousLog, compressedName)) { + QFile::remove(previousLog); + } else { + QFile::remove(compressedName); + } + } } }