[Logger] Ensure log file is flushed before crash
authorHannah von Reth <hannah.vonreth@owncloud.com>
Tue, 17 Mar 2020 08:09:43 +0000 (09:09 +0100)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Wed, 1 Jul 2020 13:13:20 +0000 (15:13 +0200)
src/libsync/logger.cpp
src/libsync/logger.h

index fade2084bd559f2c815ee20a15fe0fd5e400d24a..ba9f2fd877ead016ea554c542823828c39d0707f 100644 (file)
@@ -47,12 +47,15 @@ static void mirallLogCatcher(QtMsgType type, const QMessageLogContext &ctx, cons
         std::cerr << qPrintable(qFormatLogMessage(type, ctx, message)) << std::endl;
     }
 
+    if(type == QtFatalMsg) {
+        if (!logger->isNoop()) {
+            logger->close();
+        }
 #if defined(Q_OS_WIN)
     // Make application terminate in a way that can be caught by the crash reporter
-    if(type == QtFatalMsg) {
         Utility::crash();
-    }
 #endif
+    }
 }
 
 
@@ -138,6 +141,17 @@ void Logger::doLog(const QString &msg)
     emit logWindowLog(msg);
 }
 
+void Logger::close()
+{
+    QMutexLocker lock(&_mutex);
+    if (_logstream)
+    {
+        _logstream->flush();
+        _logFile.close();
+        _logstream.reset();
+    }
+}
+
 void Logger::mirallLog(const QString &message)
 {
     Log log_;
index b5742ed4453825050643b236677cec952c6fd5e1..5f32a8a0d8730b724e29e1adf02b6c882c7f3077 100644 (file)
@@ -47,6 +47,7 @@ public:
 
     void log(Log log);
     void doLog(const QString &log);
+    void close();
 
     static void mirallLog(const QString &message);