comparison base/Debug.cpp @ 938:90e9a3f873cf

endl to qDebug does not seem to be threadsafe
author Chris Cannam
date Tue, 01 Jul 2014 11:39:09 +0100
parents 2010409af203
children 387fa69f4eaa
comparison
equal deleted inserted replaced
937:24964a8b31e7 938:90e9a3f873cf
32 { 32 {
33 static QFile *logFile = 0; 33 static QFile *logFile = 0;
34 static QDebug *debug = 0; 34 static QDebug *debug = 0;
35 static QMutex mutex; 35 static QMutex mutex;
36 static char *prefix; 36 static char *prefix;
37
37 mutex.lock(); 38 mutex.lock();
39
38 if (!debug) { 40 if (!debug) {
39 prefix = new char[20]; 41 prefix = new char[20];
40 sprintf(prefix, "[%lu]", (unsigned long)QCoreApplication::applicationPid()); 42 sprintf(prefix, "[%lu]", (unsigned long)QCoreApplication::applicationPid());
41 QString pfx = ResourceFinder().getUserResourcePrefix(); 43 QString pfx = ResourceFinder().getUserResourcePrefix();
42 QDir logdir(QString("%1/%2").arg(pfx).arg("log")); 44 QDir logdir(QString("%1/%2").arg(pfx).arg("log"));
43 if (!logdir.exists()) logdir.mkpath(logdir.path()); 45 if (!logdir.exists()) logdir.mkpath(logdir.path());
44 logFile = new QFile(logdir.path() + "/debug.log"); 46 logFile = new QFile(logdir.path() + "/sv-debug.log");
45 if (logFile->open(QIODevice::WriteOnly | QIODevice::Truncate)) { 47 if (logFile->open(QIODevice::WriteOnly | QIODevice::Truncate)) {
46 QDebug(QtDebugMsg) << (const char *)prefix 48 QDebug(QtDebugMsg) << (const char *)prefix
47 << "Opened debug log file " 49 << "Opened debug log file "
48 << logFile->fileName(); 50 << logFile->fileName();
49 debug = new QDebug(logFile); 51 debug = new QDebug(logFile);
57 logFile = 0; 59 logFile = 0;
58 } 60 }
59 *debug << endl << (const char *)prefix << "Log started at " 61 *debug << endl << (const char *)prefix << "Log started at "
60 << QDateTime::currentDateTime().toString(); 62 << QDateTime::currentDateTime().toString();
61 } 63 }
62 mutex.unlock();
63 64
64 QDebug &dref = *debug; 65 QDebug &dref = *debug;
65 return dref << endl << (const char *)prefix; 66 dref << endl << (const char *)prefix;
67
68 mutex.unlock();
69 return dref;
66 } 70 }
67 71
68 QDebug & 72 QDebug &
69 operator<<(QDebug &dbg, const std::string &s) 73 operator<<(QDebug &dbg, const std::string &s)
70 { 74 {