annotate src/debug.cpp @ 708:067252b1e732

Support Qt5 older than 5.4
author Chris Cannam
date Wed, 12 Dec 2018 11:19:42 +0000
parents af295de6a59b
children
rev   line source
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@663 28 #include <QStandardPaths>
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@708 44 #if QT_VERSION >= 0x050400
Chris@663 45 QString logDir = QStandardPaths::writableLocation
Chris@689 46 (QStandardPaths::AppDataLocation);
Chris@708 47 #else
Chris@708 48 QString logDir = QStandardPaths::writableLocation
Chris@708 49 (QStandardPaths::DataLocation);
Chris@708 50 #endif
Chris@477 51 if (logDir != "" &&
Chris@477 52 (QDir(logDir).exists() ||
Chris@477 53 QDir().mkpath(logDir))) {
Chris@477 54 logFileName = logDir + "/debug.log";
Chris@477 55 }
Chris@477 56 logFile = new QFile(logFileName);
Chris@172 57 if (logFile->open(QIODevice::WriteOnly | QIODevice::Truncate)) {
Chris@57 58 QDebug(QtDebugMsg) << (const char *)prefix
Chris@57 59 << "Opened debug log file "
Chris@57 60 << logFile->fileName();
Chris@57 61 debug = new QDebug(logFile);
Chris@57 62 } else {
Chris@57 63 QDebug(QtWarningMsg) << (const char *)prefix
Chris@57 64 << "Failed to open debug log file "
Chris@57 65 << logFile->fileName()
Chris@57 66 << " for writing, using console debug instead";
Chris@57 67 debug = new QDebug(QtDebugMsg);
Chris@57 68 delete logFile;
Chris@57 69 logFile = 0;
Chris@57 70 }
Chris@62 71 *debug << endl << (const char *)prefix << "Log started at "
Chris@62 72 << QDateTime::currentDateTime().toString();
Chris@57 73 }
Chris@57 74 mutex.unlock();
Chris@57 75
Chris@57 76 QDebug &dref = *debug;
Chris@57 77 return dref << endl << (const char *)prefix;
Chris@57 78 }
Chris@57 79
Chris@57 80 QDebug &
Chris@57 81 operator<<(QDebug &dbg, const std::string &s)
Chris@57 82 {
Chris@57 83 dbg << QString::fromUtf8(s.c_str());
Chris@57 84 return dbg;
Chris@57 85 }
Chris@57 86
Chris@57 87 std::ostream &
Chris@57 88 operator<<(std::ostream &target, const QString &str)
Chris@57 89 {
Chris@57 90 return target << str.toLocal8Bit().data();
Chris@57 91 }
Chris@57 92
Chris@57 93 std::ostream &
Chris@57 94 operator<<(std::ostream &target, const QUrl &u)
Chris@57 95 {
Chris@57 96 return target << "<" << u.toString() << ">";
Chris@57 97 }
Chris@57 98