From 4594c913e0d980279a2828bfd89f55ef71531cda Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Sun, 6 Aug 2023 07:34:17 +0200 Subject: [PATCH] Using syslog for debugging. --- main.cpp | 5 ++- message_handler.cpp | 100 +++++++++++++++++--------------------------- message_handler.h | 7 +++- 3 files changed, 46 insertions(+), 66 deletions(-) diff --git a/main.cpp b/main.cpp index 47c2dce..eaf6a9f 100644 --- a/main.cpp +++ b/main.cpp @@ -46,14 +46,15 @@ int main(int argc, char *argv[]) { } // qputenv("XDG_RUNTIME_DIR", "/run/user/0"); + openlog("ATB-UPDATE", LOG_PERROR | LOG_PID | LOG_CONS, LOG_USER); + QApplication a(argc, argv); QApplication::setApplicationName("ATBUpdateTool"); QApplication::setApplicationVersion(APP_VERSION); if (!messageHandlerInstalled()) { // change internal qt-QDebug-handling atbInstallMessageHandler(atbDebugOutput); - setDebugLevel(QtMsgType::QtInfoMsg); - //setDebugLevel(QtMsgType::QtDebugMsg); + setDebugLevel(LOG_NOTICE); } QCommandLineParser parser; diff --git a/message_handler.cpp b/message_handler.cpp index 7be2d67..996bd2f 100755 --- a/message_handler.cpp +++ b/message_handler.cpp @@ -6,13 +6,13 @@ #include #include -#define OUTPUT_LEN (1024*8) +static char const *DBG_NAME[] = { "DBG ", "WARN ", "CRIT ", "FATAL", "INFO " }; static bool installedMsgHandler = false; -static QtMsgType debugLevel = QtInfoMsg; +static int debugLevel = LOG_NOTICE; -QtMsgType getDebugLevel() { return debugLevel; } -void setDebugLevel(QtMsgType newDebugLevel) { +int getDebugLevel() { return debugLevel; } +void setDebugLevel(int newDebugLevel) { debugLevel = newDebugLevel; } @@ -45,75 +45,51 @@ QtMessageHandler atbInstallMessageHandler(QtMessageHandler handler) { /// #if (QT_VERSION > QT_VERSION_CHECK(5, 0, 0) && QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) void atbDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) { - // static constexpr const char *format = "dd.MM.yyyy hh:mm:ss"; - QByteArray localMsg = msg.toLocal8Bit(); - QString fileName(context.file ? context.file : "N/A"); - QString function(context.function ? context.function : "N/A"); - char buf[OUTPUT_LEN]{}; - memset(buf, 0x00, sizeof(buf)); - QString const datetime = QDateTime::currentDateTime().toString(Qt::ISODateWithMs); - switch (type) { - case QtDebugMsg: { - if (debugLevel == QtDebugMsg) { - snprintf(buf, sizeof(buf)-1, "%s DEBG [%s:%s:%04u] %s", - datetime.toStdString().c_str(), - function.toStdString().c_str(), - fileName.toStdString().c_str(), - context.line, - localMsg.constData()); - fprintf(stderr, "%s\n", buf); + Q_UNUSED(context); + QString const localMsg = QString(DBG_NAME[type]) + msg.toLocal8Bit(); + + switch (debugLevel) { + case LOG_DEBUG: { // debug-level message + syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str()); + } break; + case LOG_INFO: { // informational message + if (type != QtDebugMsg) { + syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str()); } } break; - case QtInfoMsg: { - if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg) { - snprintf(buf, sizeof(buf)-1, "%s DEBG [%s:%s:%04u] %s", - datetime.toStdString().c_str(), - function.toStdString().c_str(), - fileName.toStdString().c_str(), - context.line, - localMsg.constData()); - fprintf(stderr, "%s\n", buf); + case LOG_NOTICE: { // normal, but significant, condition + if (type != QtDebugMsg) { + syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str()); } } break; - case QtWarningMsg: { - if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg || debugLevel == QtWarningMsg) { - snprintf(buf, sizeof(buf)-1, "%s DEBG [%s:%s:%04u] %s", - datetime.toStdString().c_str(), - function.toStdString().c_str(), - fileName.toStdString().c_str(), - context.line, - localMsg.constData()); - fprintf(stderr, "%s\n", buf); + case LOG_WARNING: { // warning conditions + if (type != QtInfoMsg && type != QtDebugMsg) { + syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str()); } } break; - case QtCriticalMsg: { - if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg - || debugLevel == QtWarningMsg || debugLevel == QtCriticalMsg) { - snprintf(buf, sizeof(buf)-1, "%s DEBG [%s:%s:%04u] %s", - datetime.toStdString().c_str(), - function.toStdString().c_str(), - fileName.toStdString().c_str(), - context.line, - localMsg.constData()); - fprintf(stderr, "%s\n", buf); + case LOG_ERR: { // error conditions + if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) { + syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str()); } } break; - case QtFatalMsg: { - if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg - || debugLevel == QtWarningMsg || debugLevel == QtCriticalMsg - || debugLevel == QtFatalMsg) { - snprintf(buf, sizeof(buf)-1, "%s DEBG [%s:%s:%04u] %s", - datetime.toStdString().c_str(), - function.toStdString().c_str(), - fileName.toStdString().c_str(), - context.line, - localMsg.constData()); - fprintf(stderr, "%s\n", buf); + case LOG_CRIT: { // critical conditions + if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) { + syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str()); + } + } break; + case LOG_ALERT: { // action must be taken immediately + if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) { + syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str()); + } + } break; + case LOG_EMERG: { // system is unusable + if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) { + syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str()); } } break; default: { - fprintf(stderr, "%s No ErrorLevel defined! %s\n", - datetime.toStdString().c_str(), msg.toStdString().c_str()); + //fprintf(stderr, "%s No ErrorLevel defined! %s\n", + // datetime.toStdString().c_str(), msg.toStdString().c_str()); } } } diff --git a/message_handler.h b/message_handler.h index 18e923b..98c4d7e 100755 --- a/message_handler.h +++ b/message_handler.h @@ -2,9 +2,12 @@ #define MESSAGE_HANDLER_H_INCLUDED #include +#ifdef __linux__ +#include +#endif -QtMsgType getDebugLevel(); -void setDebugLevel(QtMsgType newDebugLevel); +int getDebugLevel(); +void setDebugLevel(int newDebugLevel); bool messageHandlerInstalled(); QtMessageHandler atbInstallMessageHandler(QtMessageHandler handler);