Using syslog for debugging.

This commit is contained in:
Gerhard Hoffmann 2023-08-06 07:34:17 +02:00
parent cf9033e898
commit 4594c913e0
3 changed files with 46 additions and 66 deletions

View File

@ -46,14 +46,15 @@ int main(int argc, char *argv[]) {
} }
// qputenv("XDG_RUNTIME_DIR", "/run/user/0"); // qputenv("XDG_RUNTIME_DIR", "/run/user/0");
openlog("ATB-UPDATE", LOG_PERROR | LOG_PID | LOG_CONS, LOG_USER);
QApplication a(argc, argv); QApplication a(argc, argv);
QApplication::setApplicationName("ATBUpdateTool"); QApplication::setApplicationName("ATBUpdateTool");
QApplication::setApplicationVersion(APP_VERSION); QApplication::setApplicationVersion(APP_VERSION);
if (!messageHandlerInstalled()) { // change internal qt-QDebug-handling if (!messageHandlerInstalled()) { // change internal qt-QDebug-handling
atbInstallMessageHandler(atbDebugOutput); atbInstallMessageHandler(atbDebugOutput);
setDebugLevel(QtMsgType::QtInfoMsg); setDebugLevel(LOG_NOTICE);
//setDebugLevel(QtMsgType::QtDebugMsg);
} }
QCommandLineParser parser; QCommandLineParser parser;

View File

@ -6,13 +6,13 @@
#include <QFileInfo> #include <QFileInfo>
#include <QMessageLogContext> #include <QMessageLogContext>
#define OUTPUT_LEN (1024*8)
static char const *DBG_NAME[] = { "DBG ", "WARN ", "CRIT ", "FATAL", "INFO " };
static bool installedMsgHandler = false; static bool installedMsgHandler = false;
static QtMsgType debugLevel = QtInfoMsg; static int debugLevel = LOG_NOTICE;
QtMsgType getDebugLevel() { return debugLevel; } int getDebugLevel() { return debugLevel; }
void setDebugLevel(QtMsgType newDebugLevel) { void setDebugLevel(int newDebugLevel) {
debugLevel = 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)) #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) { void atbDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
// static constexpr const char *format = "dd.MM.yyyy hh:mm:ss"; Q_UNUSED(context);
QByteArray localMsg = msg.toLocal8Bit(); QString const localMsg = QString(DBG_NAME[type]) + msg.toLocal8Bit();
QString fileName(context.file ? context.file : "N/A");
QString function(context.function ? context.function : "N/A"); switch (debugLevel) {
char buf[OUTPUT_LEN]{}; case LOG_DEBUG: { // debug-level message
memset(buf, 0x00, sizeof(buf)); syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
QString const datetime = QDateTime::currentDateTime().toString(Qt::ISODateWithMs); } break;
switch (type) { case LOG_INFO: { // informational message
case QtDebugMsg: { if (type != QtDebugMsg) {
if (debugLevel == QtDebugMsg) { syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
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);
} }
} break; } break;
case QtInfoMsg: { case LOG_NOTICE: { // normal, but significant, condition
if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg) { if (type != QtDebugMsg) {
snprintf(buf, sizeof(buf)-1, "%s DEBG [%s:%s:%04u] %s", syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
datetime.toStdString().c_str(),
function.toStdString().c_str(),
fileName.toStdString().c_str(),
context.line,
localMsg.constData());
fprintf(stderr, "%s\n", buf);
} }
} break; } break;
case QtWarningMsg: { case LOG_WARNING: { // warning conditions
if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg || debugLevel == QtWarningMsg) { if (type != QtInfoMsg && type != QtDebugMsg) {
snprintf(buf, sizeof(buf)-1, "%s DEBG [%s:%s:%04u] %s", syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
datetime.toStdString().c_str(),
function.toStdString().c_str(),
fileName.toStdString().c_str(),
context.line,
localMsg.constData());
fprintf(stderr, "%s\n", buf);
} }
} break; } break;
case QtCriticalMsg: { case LOG_ERR: { // error conditions
if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) {
|| debugLevel == QtWarningMsg || debugLevel == QtCriticalMsg) { syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
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);
} }
} break; } break;
case QtFatalMsg: { case LOG_CRIT: { // critical conditions
if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) {
|| debugLevel == QtWarningMsg || debugLevel == QtCriticalMsg syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
|| debugLevel == QtFatalMsg) { }
snprintf(buf, sizeof(buf)-1, "%s DEBG [%s:%s:%04u] %s", } break;
datetime.toStdString().c_str(), case LOG_ALERT: { // action must be taken immediately
function.toStdString().c_str(), if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) {
fileName.toStdString().c_str(), syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
context.line, }
localMsg.constData()); } break;
fprintf(stderr, "%s\n", buf); case LOG_EMERG: { // system is unusable
if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) {
syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
} }
} break; } break;
default: { default: {
fprintf(stderr, "%s No ErrorLevel defined! %s\n", //fprintf(stderr, "%s No ErrorLevel defined! %s\n",
datetime.toStdString().c_str(), msg.toStdString().c_str()); // datetime.toStdString().c_str(), msg.toStdString().c_str());
} }
} }
} }

View File

@ -2,9 +2,12 @@
#define MESSAGE_HANDLER_H_INCLUDED #define MESSAGE_HANDLER_H_INCLUDED
#include <QtGlobal> #include <QtGlobal>
#ifdef __linux__
#include <syslog.h>
#endif
QtMsgType getDebugLevel(); int getDebugLevel();
void setDebugLevel(QtMsgType newDebugLevel); void setDebugLevel(int newDebugLevel);
bool messageHandlerInstalled(); bool messageHandlerInstalled();
QtMessageHandler atbInstallMessageHandler(QtMessageHandler handler); QtMessageHandler atbInstallMessageHandler(QtMessageHandler handler);