Chris@57
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@57
|
2
|
Chris@57
|
3 /*
|
Chris@57
|
4 EasyMercurial
|
Chris@57
|
5
|
Chris@57
|
6 Based on HgExplorer by Jari Korhonen
|
Chris@57
|
7 Copyright (c) 2010 Jari Korhonen
|
Chris@644
|
8 Copyright (c) 2013 Chris Cannam
|
Chris@644
|
9 Copyright (c) 2013 Queen Mary, University of London
|
Chris@57
|
10
|
Chris@57
|
11 This program is free software; you can redistribute it and/or
|
Chris@57
|
12 modify it under the terms of the GNU General Public License as
|
Chris@57
|
13 published by the Free Software Foundation; either version 2 of the
|
Chris@57
|
14 License, or (at your option) any later version. See the file
|
Chris@57
|
15 COPYING included with this distribution for more information.
|
Chris@57
|
16 */
|
Chris@57
|
17
|
Chris@57
|
18 #include "debug.h"
|
Chris@57
|
19
|
Chris@57
|
20 #include <QString>
|
Chris@57
|
21 #include <QUrl>
|
Chris@57
|
22 #include <QMutex>
|
Chris@57
|
23 #include <QMutexLocker>
|
Chris@57
|
24 #include <QFile>
|
Chris@57
|
25 #include <QDir>
|
Chris@57
|
26 #include <QCoreApplication>
|
Chris@62
|
27 #include <QDateTime>
|
Chris@477
|
28 #include <QDesktopServices>
|
Chris@57
|
29
|
Chris@57
|
30 #include <cstdio>
|
Chris@57
|
31
|
Chris@57
|
32 QDebug &
|
Chris@57
|
33 getEasyHgDebug()
|
Chris@57
|
34 {
|
Chris@57
|
35 static QFile *logFile = 0;
|
Chris@57
|
36 static QDebug *debug = 0;
|
Chris@57
|
37 static QMutex mutex;
|
Chris@57
|
38 static char *prefix;
|
Chris@57
|
39 mutex.lock();
|
Chris@57
|
40 if (!debug) {
|
Chris@57
|
41 prefix = new char[20];
|
Chris@60
|
42 sprintf(prefix, "[%lu]", (unsigned long)QCoreApplication::applicationPid());
|
Chris@477
|
43 QString logFileName = QDir::homePath() + "/.easyhg.log"; // the fallback
|
Chris@477
|
44 QString logDir = QDesktopServices::storageLocation
|
Chris@477
|
45 (QDesktopServices::DataLocation);
|
Chris@477
|
46 if (logDir != "" &&
|
Chris@477
|
47 (QDir(logDir).exists() ||
|
Chris@477
|
48 QDir().mkpath(logDir))) {
|
Chris@477
|
49 logFileName = logDir + "/debug.log";
|
Chris@477
|
50 }
|
Chris@477
|
51 logFile = new QFile(logFileName);
|
Chris@172
|
52 if (logFile->open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
Chris@57
|
53 QDebug(QtDebugMsg) << (const char *)prefix
|
Chris@57
|
54 << "Opened debug log file "
|
Chris@57
|
55 << logFile->fileName();
|
Chris@57
|
56 debug = new QDebug(logFile);
|
Chris@57
|
57 } else {
|
Chris@57
|
58 QDebug(QtWarningMsg) << (const char *)prefix
|
Chris@57
|
59 << "Failed to open debug log file "
|
Chris@57
|
60 << logFile->fileName()
|
Chris@57
|
61 << " for writing, using console debug instead";
|
Chris@57
|
62 debug = new QDebug(QtDebugMsg);
|
Chris@57
|
63 delete logFile;
|
Chris@57
|
64 logFile = 0;
|
Chris@57
|
65 }
|
Chris@62
|
66 *debug << endl << (const char *)prefix << "Log started at "
|
Chris@62
|
67 << QDateTime::currentDateTime().toString();
|
Chris@57
|
68 }
|
Chris@57
|
69 mutex.unlock();
|
Chris@57
|
70
|
Chris@57
|
71 QDebug &dref = *debug;
|
Chris@57
|
72 return dref << endl << (const char *)prefix;
|
Chris@57
|
73 }
|
Chris@57
|
74
|
Chris@57
|
75 QDebug &
|
Chris@57
|
76 operator<<(QDebug &dbg, const std::string &s)
|
Chris@57
|
77 {
|
Chris@57
|
78 dbg << QString::fromUtf8(s.c_str());
|
Chris@57
|
79 return dbg;
|
Chris@57
|
80 }
|
Chris@57
|
81
|
Chris@57
|
82 std::ostream &
|
Chris@57
|
83 operator<<(std::ostream &target, const QString &str)
|
Chris@57
|
84 {
|
Chris@57
|
85 return target << str.toLocal8Bit().data();
|
Chris@57
|
86 }
|
Chris@57
|
87
|
Chris@57
|
88 std::ostream &
|
Chris@57
|
89 operator<<(std::ostream &target, const QUrl &u)
|
Chris@57
|
90 {
|
Chris@57
|
91 return target << "<" << u.toString() << ">";
|
Chris@57
|
92 }
|
Chris@57
|
93
|