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