Always call doLog to ensure we get a crash log
authorHannah von Reth <hannah.vonreth@owncloud.com>
Thu, 4 Mar 2021 09:28:53 +0000 (10:28 +0100)
committerMatthieu Gallien <matthieu.gallien@nextcloud.com>
Mon, 23 Aug 2021 07:44:34 +0000 (09:44 +0200)
src/libsync/logger.cpp
src/libsync/logger.h

index 5bfd27389adff44e678685acb7d966b3da9a4e82..c6281252c8c0a94b88902ae85333c11c001fb8aa 100644 (file)
@@ -37,35 +37,6 @@ constexpr int CrashLogSize = 20;
 }
 namespace OCC {
 
-QtMessageHandler s_originalMessageHandler = nullptr;
-
-static void mirallLogCatcher(QtMsgType type, const QMessageLogContext &ctx, const QString &message)
-{
-    auto logger = Logger::instance();
-    if (type == QtDebugMsg && !logger->logDebug()) {
-        if (s_originalMessageHandler) {
-            s_originalMessageHandler(type, ctx, message);
-        }
-    } else if (!logger->isNoop()) {
-        logger->doLog(qFormatLogMessage(type, ctx, message));
-    }
-    if(type == QtCriticalMsg || type == QtFatalMsg) {
-        std::cerr << qPrintable(qFormatLogMessage(type, ctx, message)) << std::endl;
-    }
-
-    if(type == QtFatalMsg) {
-        if (!logger->isNoop()) {
-            logger->dumpCrashLog();
-            logger->close();
-        }
-#if defined(Q_OS_WIN)
-    // Make application terminate in a way that can be caught by the crash reporter
-        Utility::crash();
-#endif
-    }
-}
-
-
 Logger *Logger::instance()
 {
     static Logger log;
@@ -78,9 +49,9 @@ Logger::Logger(QObject *parent)
     qSetMessagePattern(QStringLiteral("%{time yyyy-MM-dd hh:mm:ss:zzz} [ %{type} %{category} ]%{if-debug}\t[ %{function} ]%{endif}:\t%{message}"));
     _crashLog.resize(CrashLogSize);
 #ifndef NO_MSG_HANDLER
-   s_originalMessageHandler = qInstallMessageHandler(mirallLogCatcher);
-#else
-    Q_UNUSED(mirallLogCatcher)
+    qInstallMessageHandler([](QtMsgType type, const QMessageLogContext &ctx, const QString &message) {
+            Logger::instance()->doLog(type, ctx, message);
+        });
 #endif
 }
 
@@ -107,23 +78,15 @@ void Logger::postGuiMessage(const QString &title, const QString &message)
     emit guiMessage(title, message);
 }
 
-/**
- * Returns true if doLog does nothing and need not to be called
- */
-bool Logger::isNoop() const
-{
-    QMutexLocker lock(&_mutex);
-    return !_logstream;
-}
-
 bool Logger::isLoggingToFile() const
 {
     QMutexLocker lock(&_mutex);
     return _logstream;
 }
 
-void Logger::doLog(const QString &msg)
+void Logger::doLog(QtMsgType type, const QMessageLogContext &ctx, const QString &message)
 {
+    const QString msg = qFormatLogMessage(type, ctx, message);
     {
         QMutexLocker lock(&_mutex);
         _crashLogIndex = (_crashLogIndex + 1) % CrashLogSize;
@@ -133,13 +96,20 @@ void Logger::doLog(const QString &msg)
             if (_doFileFlush)
                 _logstream->flush();
         }
+        if (type == QtFatalMsg) {
+            close();
+#if defined(Q_OS_WIN)
+            // Make application terminate in a way that can be caught by the crash reporter
+            Utility::crash();
+#endif
+        }
     }
     emit logWindowLog(msg);
 }
 
 void Logger::close()
 {
-    QMutexLocker lock(&_mutex);
+    dumpCrashLog();
     if (_logstream)
     {
         _logstream->flush();
index 32d36d617ea86fa783a0c724b40e63d7e710161e..c37da97faa63fad91a5a623a3dda28296b3ea2a1 100644 (file)
@@ -35,11 +35,9 @@ class OWNCLOUDSYNC_EXPORT Logger : public QObject
 {
     Q_OBJECT
 public:
-    bool isNoop() const;
     bool isLoggingToFile() const;
 
-    void doLog(const QString &log);
-    void close();
+    void doLog(QtMsgType type, const QMessageLogContext &ctx, const QString &message);
 
     static Logger *instance();
 
@@ -84,8 +82,6 @@ public:
     }
     void setLogRules(const QSet<QString> &rules);
 
-    void dumpCrashLog();
-
 signals:
     void logWindowLog(const QString &);
 
@@ -99,6 +95,10 @@ public slots:
 private:
     Logger(QObject *parent = nullptr);
     ~Logger() override;
+
+    void close();
+    void dumpCrashLog();
+
     QFile _logFile;
     bool _doFileFlush = false;
     int _logExpire = 0;