Logger: don't call qFormatLog when unessesary
authorOlivier Goffart <ogoffart@woboq.com>
Tue, 20 Oct 2015 11:22:48 +0000 (13:22 +0200)
committerOlivier Goffart <ogoffart@woboq.com>
Tue, 20 Oct 2015 11:22:48 +0000 (13:22 +0200)
That's a lot of string operations that can be avoided if the log window
is not shown

src/libsync/logger.cpp
src/libsync/logger.h

index c3ac1f1f29ea5d2cb080dd0315bd5c44890d0e15..d4767dced175b384e6cc776f3e4d49dc5de08a2b 100644 (file)
@@ -17,6 +17,7 @@
 #include <QDir>
 #include <QStringList>
 #include <QThread>
+#include <qmetaobject.h>
 
 namespace OCC {
 
@@ -40,7 +41,10 @@ static void mirallLogCatcher(QtMsgType, const QMessageLogContext &ctx, const QSt
 }
 #else
 static void mirallLogCatcher(QtMsgType type, const QMessageLogContext &ctx, const QString &message) {
-    Logger::instance()->doLog( qFormatLogMessage(type, ctx, message) ) ;
+    auto logger = Logger::instance();
+    if (!logger->isNoop()) {
+        logger->doLog( qFormatLogMessage(type, ctx, message) ) ;
+    }
 }
 #endif
 
@@ -105,6 +109,24 @@ void Logger::log(Log log)
     doLog(msg);
 }
 
+/**
+ * Returns true if doLog does nothing and need not to be called
+ */
+bool Logger::isNoop() const
+{
+#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
+    return false;
+#else
+    static auto signal = QMetaMethod::fromSignal(&Logger::newLog);
+    if (isSignalConnected(signal)) {
+        return false;
+    }
+    QMutexLocker lock(const_cast<QMutex *>(&_mutex));
+    return !_logstream;
+#endif
+}
+
+
 void Logger::doLog(const QString& msg)
 {
     {
index ada8f26be855bd8ffd7e40f1e05988a547015d08..d73d32b693fc24eee787d01ece2a2204e90d961d 100644 (file)
@@ -46,6 +46,7 @@ class OWNCLOUDSYNC_EXPORT Logger : public QObject
   Q_OBJECT
 public:
 
+  bool isNoop() const;
   void log(Log log);
   void doLog(const QString &log);