comparison 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
comparison
equal deleted inserted replaced
949:5f7b509a441a 954:35b05cac32d0
22 #include <QMutexLocker> 22 #include <QMutexLocker>
23 #include <QFile> 23 #include <QFile>
24 #include <QDir> 24 #include <QDir>
25 #include <QCoreApplication> 25 #include <QCoreApplication>
26 #include <QDateTime> 26 #include <QDateTime>
27 #include <QThreadStorage>
27 28
28 #include <cstdio> 29 #include <cstdio>
30
31 static QThreadStorage<QDebug *> debugs;
32 static QMutex mutex;
33 static char *prefix = 0;
29 34
30 QDebug & 35 QDebug &
31 getSVDebug() 36 getSVDebug()
32 { 37 {
33 static QFile *logFile = 0;
34 static QDebug *debug = 0;
35 static QMutex mutex;
36 static char *prefix;
37
38 mutex.lock(); 38 mutex.lock();
39 39
40 if (!debug) { 40 QDebug *debug = 0;
41
42 QString pfx = ResourceFinder().getUserResourcePrefix();
43 QDir logdir(QString("%1/%2").arg(pfx).arg("log"));
44
45 if (!prefix) {
41 prefix = new char[20]; 46 prefix = new char[20];
42 sprintf(prefix, "[%lu]", (unsigned long)QCoreApplication::applicationPid()); 47 sprintf(prefix, "[%lu]", (unsigned long)QCoreApplication::applicationPid());
43 QString pfx = ResourceFinder().getUserResourcePrefix();
44 QDir logdir(QString("%1/%2").arg(pfx).arg("log"));
45 if (!logdir.exists()) logdir.mkpath(logdir.path()); 48 if (!logdir.exists()) logdir.mkpath(logdir.path());
46 logFile = new QFile(logdir.path() + "/sv-debug.log"); 49 }
47 if (logFile->open(QIODevice::WriteOnly | QIODevice::Truncate)) { 50
51 if (!debugs.hasLocalData()) {
52 QFile *logFile = new QFile(logdir.path() + "/sv-debug.log");
53 if (logFile->open(QIODevice::WriteOnly | QIODevice::Append)) {
48 QDebug(QtDebugMsg) << (const char *)prefix 54 QDebug(QtDebugMsg) << (const char *)prefix
49 << "Opened debug log file " 55 << "Opened debug log file "
50 << logFile->fileName(); 56 << logFile->fileName();
51 debug = new QDebug(logFile); 57 debug = new QDebug(logFile);
52 } else { 58 } else {
53 QDebug(QtWarningMsg) << (const char *)prefix 59 QDebug(QtWarningMsg) << (const char *)prefix
54 << "Failed to open debug log file " 60 << "Failed to open debug log file "
55 << logFile->fileName() 61 << logFile->fileName()
56 << " for writing, using console debug instead"; 62 << " for writing, using console debug instead";
57 debug = new QDebug(QtDebugMsg);
58 delete logFile; 63 delete logFile;
59 logFile = 0; 64 logFile = 0;
65 debug = new QDebug(QtDebugMsg);
60 } 66 }
67 debugs.setLocalData(debug);
61 *debug << endl << (const char *)prefix << "Log started at " 68 *debug << endl << (const char *)prefix << "Log started at "
62 << QDateTime::currentDateTime().toString(); 69 << QDateTime::currentDateTime().toString();
70 } else {
71 debug = debugs.localData();
63 } 72 }
73
74 mutex.unlock();
64 75
65 QDebug &dref = *debug; 76 QDebug &dref = *debug;
66 dref << endl << (const char *)prefix; 77 dref << endl << (const char *)prefix;
67 78
68 mutex.unlock();
69 return dref; 79 return dref;
70 } 80 }
71 81
72 QDebug & 82 QDebug &
73 operator<<(QDebug &dbg, const std::string &s) 83 operator<<(QDebug &dbg, const std::string &s)