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 <QMutex>
20 #include <QDir>
21 #include <QUrl>
22 #include <QCoreApplication>
23 #include <QDateTime>
24 
25 #include <stdexcept>
26 #include <memory>
27 
28 static std::unique_ptr<SVDebug> svdebug = nullptr;
29 static std::unique_ptr<SVCerr> svcerr = nullptr;
30 static QMutex mutex;
31 
33  mutex.lock();
34  if (!svdebug) {
35  svdebug = std::unique_ptr<SVDebug>(new SVDebug());
36  }
37  mutex.unlock();
38  return *svdebug;
39 }
40 
42  mutex.lock();
43  if (!svcerr) {
44  if (!svdebug) {
45  svdebug = std::unique_ptr<SVDebug>(new SVDebug());
46  }
47  svcerr = std::unique_ptr<SVCerr>(new SVCerr(*svdebug));
48  }
49  mutex.unlock();
50  return *svcerr;
51 }
52 
53 bool SVDebug::m_silenced = false;
54 bool SVCerr::m_silenced = false;
55 
57  m_prefix(nullptr),
58  m_ok(false),
59  m_eol(true)
60 {
61  if (m_silenced) return;
62 
63  m_timer.start();
64 
65  if (qApp->applicationName() == "") {
66  cerr << "ERROR: Can't use SVDEBUG before setting application name" << endl;
67  throw std::logic_error("Can't use SVDEBUG before setting application name");
68  }
69 
70  QString pfx = ResourceFinder().getUserResourcePrefix();
71  QDir logdir(QString("%1/%2").arg(pfx).arg("log"));
72 
73  m_prefix = strdup(QString("[%1]")
74  .arg(QCoreApplication::applicationPid())
75  .toLatin1().data());
76 
78  if (!logdir.exists()) logdir.mkpath(logdir.path());
79 
80  QString fileName = logdir.path() + "/sv-debug.log";
81 
82  m_stream.open(fileName.toLocal8Bit().data(), std::ios_base::out);
83 
84  if (!m_stream) {
85  QDebug(QtWarningMsg) << (const char *)m_prefix
86  << "Failed to open debug log file "
87  << fileName << " for writing";
88  } else {
89  m_ok = true;
90 // cerr << "Log file is " << fileName << endl;
91  (*this) << "Debug log started at "
92  << QDateTime::currentDateTime().toString() << endl;
93  }
94 }
95 
97 {
98  if (m_stream) {
99  (*this) << "Debug log ends" << endl;
100  m_stream.close();
101  }
102 }
103 
104 QDebug &
105 operator<<(QDebug &dbg, const std::string &s)
106 {
107  dbg << QString::fromUtf8(s.c_str());
108  return dbg;
109 }
110 
111 std::ostream &
112 operator<<(std::ostream &target, const QString &str)
113 {
114  return target << str.toUtf8().data();
115 }
116 
117 std::ostream &
118 operator<<(std::ostream &target, const QUrl &u)
119 {
120  return target << "<" << u.toString() << ">";
121 }
122 
~SVDebug()
Definition: Debug.cpp:96
static QMutex mutex
Definition: Debug.cpp:30
bool m_ok
Definition: Debug.h:70
QElapsedTimer m_timer
Definition: Debug.h:72
Definition: Debug.h:76
char * m_prefix
Definition: Debug.h:69
std::fstream m_stream
Definition: Debug.h:68
static bool m_silenced
Definition: Debug.h:99
static std::unique_ptr< SVCerr > svcerr
Definition: Debug.cpp:29
SVCerr & getSVCerr()
Definition: Debug.cpp:41
SVDebug()
Definition: Debug.cpp:56
SVDebug & getSVDebug()
Definition: Debug.cpp:32
QString getUserResourcePrefix()
Return the root path for user-specific resource installation for this application (i...
static bool m_silenced
Definition: Debug.h:73
SVDebug & operator<<(const T &t)
Definition: Debug.h:46
Definition: Debug.h:40
static std::unique_ptr< SVDebug > svdebug
Definition: Debug.cpp:28