From 98c5ae234d24943b63736b6581f440155252e21f Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Thu, 9 Nov 2017 20:13:12 +0100 Subject: [PATCH] Experimental persistent log. --- resources/binaries | 2 +- src/main.cpp | 5 ++++ src/miscellaneous/debugging.cpp | 48 ++++++++++++++++++++++++++++----- src/miscellaneous/debugging.h | 17 +++++++++--- 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/resources/binaries b/resources/binaries index 4a01edaec..ae7084718 160000 --- a/resources/binaries +++ b/resources/binaries @@ -1 +1 @@ -Subproject commit 4a01edaec7d67d3b2ae81aeea2a3c876216fbab8 +Subproject commit ae7084718c41afc01919779e58cd449e0eebd401 diff --git a/src/main.cpp b/src/main.cpp index b74fb1bd1..5383d9e3b 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -62,6 +62,11 @@ int main(int argc, char* argv[]) { // Instantiate base application object. Application application(APP_LOW_NAME, argc, argv); + if (application.arguments().contains(QL1S("-log"))) { + Debugging::instance()->setTargetFile(IOFactory::getSystemFolder(QStandardPaths::TempLocation) + + QDir::separator() + QL1S("rssguard.log")); + } + qDebug("Instantiated Application class."); // Check if another instance is running. diff --git a/src/miscellaneous/debugging.cpp b/src/miscellaneous/debugging.cpp index 20ef55188..4b3c51f0d 100755 --- a/src/miscellaneous/debugging.cpp +++ b/src/miscellaneous/debugging.cpp @@ -11,7 +11,28 @@ #include #include -Debugging::Debugging() {} +Q_GLOBAL_STATIC(Debugging, qz_debug_acmanager) + +Debugging * Debugging::instance() { + return qz_debug_acmanager(); +} + +void Debugging::setTargetFile(const QString& targetFile) { + m_targetFile = targetFile; + + if (!m_targetFile.isEmpty()) { + m_targetFileHandle = new QFile(m_targetFile); + m_targetFileHandle->open(QIODevice::WriteOnly | QIODevice::Append); + } +} + +QString Debugging::targetFile() const { + return m_targetFile; +} + +QFile* Debugging::targetFileHandle() { + return m_targetFileHandle; +} void Debugging::performLog(const char* message, QtMsgType type, const char* file, const char* function, int line) { const char* type_string = typeToString(type); @@ -21,13 +42,26 @@ void Debugging::performLog(const char* message, QtMsgType type, const char* file std::strftime(mbstr, sizeof(mbstr), "%y/%d/%m %H:%M:%S", std::localtime(&t)); - // Write to console. - if (file == 0 || function == 0 || line < 0) { - fprintf(stderr, "[%s] %s: %s (%s)\n", APP_LOW_NAME, type_string, message, mbstr); + if (instance()->targetFile().isEmpty()) { + + // Write to console. + if (file == 0 || function == 0 || line < 0) { + fprintf(stderr, "[%s] %s: %s (%s)\n", APP_LOW_NAME, type_string, message, mbstr); + } + else { + fprintf(stderr, "[%s] %s (%s)\n Type: %s\n File: %s (line %d)\n Function: %s\n\n", + APP_LOW_NAME, message, mbstr, type_string, file, line, function); + } } else { - fprintf(stderr, "[%s] %s (%s)\n Type: %s\n File: %s (line %d)\n Function: %s\n\n", - APP_LOW_NAME, message, mbstr, type_string, file, line, function); + if (file == 0 || function == 0 || line < 0) { + instance()->targetFileHandle()->write(QString("[%1] %2: %3 (%4)\n").arg(APP_LOW_NAME, type_string, message, mbstr).toUtf8()); + } + else { + instance()->targetFileHandle()->write(QString("[%1] %2 (%3)\n Type: %4\n File: %5 (line %6)\n Function: %7\n\n") + .arg(APP_LOW_NAME, message, mbstr, type_string, + file, QString::number(line), function).toUtf8()); + } } if (type == QtFatalMsg) { @@ -52,6 +86,8 @@ const char* Debugging::typeToString(QtMsgType type) { } } +Debugging::Debugging() {} + void Debugging::debugHandler(QtMsgType type, const QMessageLogContext& placement, const QString& message) { #ifndef QT_NO_DEBUG_OUTPUT performLog(qPrintable(message), type, placement.file, placement.function, placement.line); diff --git a/src/miscellaneous/debugging.h b/src/miscellaneous/debugging.h index 3e8cd6d5b..ddec493b7 100755 --- a/src/miscellaneous/debugging.h +++ b/src/miscellaneous/debugging.h @@ -5,8 +5,12 @@ #include +#include +#include + class Debugging { public: + explicit Debugging(); // Specifies format of output console messages. // NOTE: QT_NO_DEBUG_OUTPUT - disables debug outputs completely!!! @@ -14,10 +18,17 @@ class Debugging { static void performLog(const char* message, QtMsgType type, const char* file = 0, const char* function = 0, int line = -1); static const char* typeToString(QtMsgType type); - private: + // Returns pointer to global silent network manager + static Debugging* instance(); - // Constructor. - explicit Debugging(); + void setTargetFile(const QString& targetFile); + QString targetFile() const; + + QFile* targetFileHandle(); + + private: + QString m_targetFile; + QFile* m_targetFileHandle; }; #endif // DEBUGGING_H