From 89fe648868f42d1acd547cf4578215de06de46e3 Mon Sep 17 00:00:00 2001 From: Phil Taylor Date: Sun, 7 Feb 2021 20:07:26 +0000 Subject: [PATCH] Add logging to file. --- logcategories.cpp | 6 +++++ logcategories.h | 11 ++++++++ main.cpp | 68 ++++++++++++++++++++++++++++++++++++++++------- wfmain.h | 2 +- wfview.pro | 6 +++-- 5 files changed, 81 insertions(+), 12 deletions(-) create mode 100644 logcategories.cpp create mode 100644 logcategories.h diff --git a/logcategories.cpp b/logcategories.cpp new file mode 100644 index 0000000..ea9efe7 --- /dev/null +++ b/logcategories.cpp @@ -0,0 +1,6 @@ +#include "logcategories.h" + +Q_LOGGING_CATEGORY(logDebug, "Debug") +Q_LOGGING_CATEGORY(logInfo, "Info") +Q_LOGGING_CATEGORY(logWarning, "Warning") +Q_LOGGING_CATEGORY(logCritical, "Critical") \ No newline at end of file diff --git a/logcategories.h b/logcategories.h new file mode 100644 index 0000000..6adbfff --- /dev/null +++ b/logcategories.h @@ -0,0 +1,11 @@ +#ifndef LOGCATEGORIES_H +#define LOGCATEGORIES_H + +#include + +Q_DECLARE_LOGGING_CATEGORY(logDebug) +Q_DECLARE_LOGGING_CATEGORY(logInfo) +Q_DECLARE_LOGGING_CATEGORY(logWarning) +Q_DECLARE_LOGGING_CATEGORY(logCritical) + +#endif // LOGCATEGORIES_H diff --git a/main.cpp b/main.cpp index 56e8320..62618f4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,14 @@ -#include "wfmain.h" #include #include +#include "wfmain.h" // Copytight 2017-2021 Elliott H. Liggett +// Smart pointer to log file +QScopedPointer m_logFile; +QMutex logMutex; + +void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg); int main(int argc, char *argv[]) { @@ -19,10 +24,11 @@ int main(int argc, char *argv[]) QString serialPortCL; QString hostCL; QString civCL; + QString logFilename= QStandardPaths::standardLocations(QStandardPaths::TempLocation)[0] + "/wfview.log"; QString currentArg; - const QString helpText = QString("Usage: -p --port /dev/port, -h --host remotehostname, -c --civ 0xAddr"); // TODO... + const QString helpText = QString("Usage: -p --port /dev/port, -h --host remotehostname, -c --civ 0xAddr, -l --logfile filename.log"); // TODO... for(int c=1; c c) + if (argc > c) { - civCL = argv[c+1]; - c+=1; + civCL = argv[c + 1]; + c += 1; + } + } + else if ((currentArg == "l") || (currentArg == "--logfile")) + { + if (argc > c) + { + logFilename = argv[c + 1]; + c += 1; } } else if ((currentArg == "--help")) { @@ -62,11 +77,20 @@ int main(int argc, char *argv[]) } + // Set the logging file before doing anything else. + m_logFile.reset(new QFile(logFilename)); + // Open the file logging + m_logFile.data()->open(QFile::Append | QFile::Text); + // Set handler + qInstallMessageHandler(messageHandler); + + qDebug(logInfo()) << "Starting wfview"; + #ifdef QT_DEBUG - qDebug() << "SerialPortCL as set by parser: " << serialPortCL; - qDebug() << "remote host as set by parser: " << hostCL; - qDebug() << "CIV as set by parser: " << civCL; + qDebug(logDebug()) << "SerialPortCL as set by parser: " << serialPortCL; + qDebug(logDebug()) << "remote host as set by parser: " << hostCL; + qDebug(logDebug()) << "CIV as set by parser: " << civCL; #endif a.setWheelScrollLines(1); // one line per wheel click wfmain w( serialPortCL, hostCL); @@ -76,4 +100,30 @@ int main(int argc, char *argv[]) return a.exec(); + qDebug(logInfo()) << "wfview is finished"; + } + + +void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg) +{ + // Open stream file writes + QMutexLocker locker(&logMutex); + QTextStream out(m_logFile.data()); + + // Write the date of recording + out << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz "); + // By type determine to what level belongs message + + switch (type) + { + case QtInfoMsg: out << "INF "; break; + case QtDebugMsg: out << "DBG "; break; + case QtWarningMsg: out << "WRN "; break; + case QtCriticalMsg: out << "CRT "; break; + case QtFatalMsg: out << "FTL "; break; + } + // Write to the output category of the message and the message itself + out << context.category << ": " << msg << "\n"; + out.flush(); // Clear the buffered data +} \ No newline at end of file diff --git a/wfmain.h b/wfmain.h index e4ef10d..0941a33 100644 --- a/wfmain.h +++ b/wfmain.h @@ -10,7 +10,7 @@ #include #include - +#include "logcategories.h" #include "commhandler.h" #include "rigcommander.h" #include "freqmemory.h" diff --git a/wfview.pro b/wfview.pro index 80da359..cb03a00 100644 --- a/wfview.pro +++ b/wfview.pro @@ -79,14 +79,16 @@ SOURCES += main.cpp\ rigcommander.cpp \ freqmemory.cpp \ rigidentities.cpp \ - udphandler.cpp + udphandler.cpp \ + logcategories.cpp HEADERS += wfmain.h \ commhandler.h \ rigcommander.h \ freqmemory.h \ rigidentities.h \ - udphandler.h + udphandler.h \ + logcategories.h FORMS += wfmain.ui