comparison src/debug.cpp @ 370:b9c153e00e84

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