diff base/Debug.cpp @ 954:35b05cac32d0 tonioni

Update subrepos and merge from default branch
author Chris Cannam
date Tue, 02 Sep 2014 16:23:48 +0100
parents 4b9fc70a37d5
children d002827b5896
line wrap: on
line diff
--- a/base/Debug.cpp	Fri Jul 18 11:07:30 2014 +0100
+++ b/base/Debug.cpp	Tue Sep 02 16:23:48 2014 +0100
@@ -24,27 +24,33 @@
 #include <QDir>
 #include <QCoreApplication>
 #include <QDateTime>
+#include <QThreadStorage>
 
 #include <cstdio>
 
+static QThreadStorage<QDebug *> debugs;
+static QMutex mutex;
+static char *prefix = 0;
+
 QDebug &
 getSVDebug()
 {
-    static QFile *logFile = 0;
-    static QDebug *debug = 0;
-    static QMutex mutex;
-    static char *prefix;
-
     mutex.lock();
 
-    if (!debug) {
+    QDebug *debug = 0;
+
+    QString pfx = ResourceFinder().getUserResourcePrefix();
+    QDir logdir(QString("%1/%2").arg(pfx).arg("log"));
+
+    if (!prefix) {
         prefix = new char[20];
         sprintf(prefix, "[%lu]", (unsigned long)QCoreApplication::applicationPid());
-	QString pfx = ResourceFinder().getUserResourcePrefix();
-	QDir logdir(QString("%1/%2").arg(pfx).arg("log"));
 	if (!logdir.exists()) logdir.mkpath(logdir.path());
-        logFile = new QFile(logdir.path() + "/sv-debug.log");
-        if (logFile->open(QIODevice::WriteOnly | QIODevice::Truncate)) {
+    }
+
+    if (!debugs.hasLocalData()) {
+        QFile *logFile = new QFile(logdir.path() + "/sv-debug.log");
+        if (logFile->open(QIODevice::WriteOnly | QIODevice::Append)) {
             QDebug(QtDebugMsg) << (const char *)prefix
                                << "Opened debug log file "
                                << logFile->fileName();
@@ -54,18 +60,22 @@
                                  << "Failed to open debug log file "
                                  << logFile->fileName()
                                  << " for writing, using console debug instead";
-            debug = new QDebug(QtDebugMsg);
             delete logFile;
             logFile = 0;
+            debug = new QDebug(QtDebugMsg);
         }
+        debugs.setLocalData(debug);
         *debug << endl << (const char *)prefix << "Log started at "
                << QDateTime::currentDateTime().toString();
+    } else {
+        debug = debugs.localData();
     }
 
+    mutex.unlock();
+
     QDebug &dref = *debug;
     dref << endl << (const char *)prefix;
 
-    mutex.unlock();
     return dref;
 }