Chris@685
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@685
|
2
|
Chris@685
|
3 /*
|
Chris@685
|
4 Sonic Visualiser
|
Chris@685
|
5 An audio file viewer and annotation editor.
|
Chris@685
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@685
|
7 This file copyright 2010-2011 Chris Cannam and QMUL.
|
Chris@685
|
8
|
Chris@685
|
9 This program is free software; you can redistribute it and/or
|
Chris@685
|
10 modify it under the terms of the GNU General Public License as
|
Chris@685
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@685
|
12 License, or (at your option) any later version. See the file
|
Chris@685
|
13 COPYING included with this distribution for more information.
|
Chris@685
|
14 */
|
Chris@685
|
15
|
Chris@685
|
16 #include "Debug.h"
|
Chris@685
|
17 #include "ResourceFinder.h"
|
Chris@685
|
18
|
Chris@685
|
19 #include <QString>
|
Chris@685
|
20 #include <QUrl>
|
Chris@685
|
21 #include <QMutex>
|
Chris@685
|
22 #include <QMutexLocker>
|
Chris@685
|
23 #include <QFile>
|
Chris@685
|
24 #include <QDir>
|
Chris@685
|
25 #include <QCoreApplication>
|
Chris@685
|
26 #include <QDateTime>
|
Chris@950
|
27 #include <QThreadStorage>
|
Chris@685
|
28
|
Chris@685
|
29 #include <cstdio>
|
Chris@685
|
30
|
Chris@950
|
31 static QThreadStorage<QDebug *> debugs;
|
Chris@950
|
32 static QMutex mutex;
|
Chris@950
|
33 static char *prefix = 0;
|
Chris@950
|
34
|
Chris@685
|
35 QDebug &
|
Chris@685
|
36 getSVDebug()
|
Chris@685
|
37 {
|
Chris@685
|
38 mutex.lock();
|
Chris@938
|
39
|
Chris@950
|
40 QDebug *debug = 0;
|
Chris@950
|
41
|
Chris@951
|
42 QString pfx = ResourceFinder().getUserResourcePrefix();
|
Chris@951
|
43 QDir logdir(QString("%1/%2").arg(pfx).arg("log"));
|
Chris@951
|
44
|
Chris@950
|
45 if (!prefix) {
|
Chris@986
|
46 prefix = strdup(QString("[%1]")
|
Chris@986
|
47 .arg(QCoreApplication::applicationPid())
|
Chris@986
|
48 .toLatin1().data());
|
Chris@951
|
49 }
|
Chris@951
|
50
|
Chris@1058
|
51 //!!! what to do if mkpath fails?
|
Chris@1058
|
52 if (!logdir.exists()) logdir.mkpath(logdir.path());
|
Chris@1058
|
53
|
Chris@951
|
54 if (!debugs.hasLocalData()) {
|
Chris@951
|
55 QFile *logFile = new QFile(logdir.path() + "/sv-debug.log");
|
Chris@951
|
56 if (logFile->open(QIODevice::WriteOnly | QIODevice::Append)) {
|
Chris@685
|
57 QDebug(QtDebugMsg) << (const char *)prefix
|
Chris@685
|
58 << "Opened debug log file "
|
Chris@685
|
59 << logFile->fileName();
|
Chris@951
|
60 debug = new QDebug(logFile);
|
Chris@685
|
61 } else {
|
Chris@685
|
62 QDebug(QtWarningMsg) << (const char *)prefix
|
Chris@685
|
63 << "Failed to open debug log file "
|
Chris@685
|
64 << logFile->fileName()
|
Chris@685
|
65 << " for writing, using console debug instead";
|
Chris@685
|
66 delete logFile;
|
Chris@685
|
67 logFile = 0;
|
Chris@950
|
68 debug = new QDebug(QtDebugMsg);
|
Chris@950
|
69 }
|
Chris@950
|
70 debugs.setLocalData(debug);
|
Chris@685
|
71 *debug << endl << (const char *)prefix << "Log started at "
|
Chris@685
|
72 << QDateTime::currentDateTime().toString();
|
Chris@951
|
73 } else {
|
Chris@951
|
74 debug = debugs.localData();
|
Chris@685
|
75 }
|
Chris@685
|
76
|
Chris@950
|
77 mutex.unlock();
|
Chris@950
|
78
|
Chris@685
|
79 QDebug &dref = *debug;
|
Chris@938
|
80 dref << endl << (const char *)prefix;
|
Chris@938
|
81
|
Chris@938
|
82 return dref;
|
Chris@685
|
83 }
|
Chris@685
|
84
|
Chris@685
|
85 QDebug &
|
Chris@685
|
86 operator<<(QDebug &dbg, const std::string &s)
|
Chris@685
|
87 {
|
Chris@685
|
88 dbg << QString::fromUtf8(s.c_str());
|
Chris@685
|
89 return dbg;
|
Chris@685
|
90 }
|
Chris@685
|
91
|
Chris@685
|
92 std::ostream &
|
Chris@685
|
93 operator<<(std::ostream &target, const QString &str)
|
Chris@685
|
94 {
|
Chris@846
|
95 return target << str.toStdString();
|
Chris@685
|
96 }
|
Chris@685
|
97
|
Chris@685
|
98 std::ostream &
|
Chris@685
|
99 operator<<(std::ostream &target, const QUrl &u)
|
Chris@685
|
100 {
|
Chris@846
|
101 return target << "<" << u.toString().toStdString() << ">";
|
Chris@685
|
102 }
|
Chris@685
|
103
|