flush logs every 5 lines written
authorMatthieu Gallien <matthieu.gallien@nextcloud.com>
Wed, 13 Nov 2024 10:54:36 +0000 (11:54 +0100)
committerMatthieu Gallien <matthieu.gallien@nextcloud.com>
Thu, 21 Nov 2024 11:06:06 +0000 (12:06 +0100)
should help not loosing too much logs in case of a crash or similar not
nominal stop of the client

Signed-off-by: Matthieu Gallien <matthieu.gallien@nextcloud.com>
src/libsync/logger.cpp
src/libsync/logger.h

index 5e885d7afebe4de4b3a41bd17a5a6b61e70c663a..a9ffad0f348c0c8e1e70fda139661cb4f3b2f904 100644 (file)
@@ -37,6 +37,7 @@ namespace {
 
 constexpr int CrashLogSize = 20;
 constexpr auto MaxLogLinesCount = 50000;
+constexpr auto MaxLogLinesBeforeFlush = 10;
 
 static bool compressLog(const QString &originalName, const QString &targetName)
 {
@@ -145,8 +146,13 @@ void Logger::doLog(QtMsgType type, const QMessageLogContext &ctx, const QString
 
         if (_logstream) {
             (*_logstream) << msg << "\n";
-            if (_doFileFlush)
+            ++_linesCounter;
+            if (_doFileFlush ||
+                _linesCounter >= MaxLogLinesBeforeFlush ||
+                type == QtMsgType::QtWarningMsg || type == QtMsgType::QtCriticalMsg || type == QtMsgType::QtFatalMsg) {
                 _logstream->flush();
+                _linesCounter = 0;
+            }
         }
         if (_permanentDeleteLogStream && ctx.category && strcmp(ctx.category, lcPermanentLog().categoryName()) == 0) {
             (*_permanentDeleteLogStream) << msg << "\n";
index 2d9dce3193edd1194705388f4b982c3ac021dfb3..b913678b40172001b30b4b188a66125be7e77e47 100644 (file)
@@ -110,6 +110,7 @@ private:
 
     QFile _logFile;
     bool _doFileFlush = false;
+    int _linesCounter = 0;
     int _logExpire = 0;
     bool _logDebug = false;
     QScopedPointer<QTextStream> _logstream;