UpdatePTUDevCtrl/message_handler.cpp

98 lines
3.2 KiB
C++
Raw Normal View History

2023-04-05 14:43:27 +02:00
#include "message_handler.h"
#include <QDateTime>
#include <cstring>
#include <QString>
#include <QFileInfo>
#include <QMessageLogContext>
2023-04-05 14:43:27 +02:00
2023-08-06 07:34:17 +02:00
static char const *DBG_NAME[] = { "DBG ", "WARN ", "CRIT ", "FATAL", "INFO " };
2023-04-05 14:43:27 +02:00
static bool installedMsgHandler = false;
2023-08-06 07:34:17 +02:00
static int debugLevel = LOG_NOTICE;
2023-04-05 14:43:27 +02:00
2023-08-06 07:34:17 +02:00
int getDebugLevel() { return debugLevel; }
void setDebugLevel(int newDebugLevel) {
2023-04-05 14:43:27 +02:00
debugLevel = newDebugLevel;
}
bool messageHandlerInstalled() {
return installedMsgHandler;
}
QtMessageHandler atbInstallMessageHandler(QtMessageHandler handler) {
installedMsgHandler = (handler != 0);
static QtMessageHandler prevHandler = nullptr;
if (handler) {
prevHandler = qInstallMessageHandler(handler);
return prevHandler;
} else {
return qInstallMessageHandler(prevHandler);
}
}
///
/// \brief Print message according to given debug level.
///
/// \note Install this function using qInstallMsgHandler().
///
/// int main(int argc, char **argv) {
/// installMsgHandler(atbDebugOutput);
/// QApplication app(argc, argv);
/// ...
/// return app.exec();
/// }
///
2023-06-16 16:49:20 +02:00
#if (QT_VERSION > QT_VERSION_CHECK(5, 0, 0) && QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
2023-04-05 14:43:27 +02:00
void atbDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
2023-08-06 07:34:17 +02:00
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 LOG_NOTICE: { // normal, but significant, condition
if (type != QtDebugMsg) {
syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
}
} break;
case LOG_WARNING: { // warning conditions
if (type != QtInfoMsg && type != QtDebugMsg) {
syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
2023-04-05 14:43:27 +02:00
}
} break;
2023-08-06 07:34:17 +02:00
case LOG_ERR: { // error conditions
if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) {
syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
2023-04-05 14:43:27 +02:00
}
} break;
2023-08-06 07:34:17 +02:00
case LOG_CRIT: { // critical conditions
if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) {
syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
2023-04-05 14:43:27 +02:00
}
} break;
2023-08-06 07:34:17 +02:00
case LOG_ALERT: { // action must be taken immediately
if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) {
syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
2023-04-05 14:43:27 +02:00
}
} break;
2023-08-06 07:34:17 +02:00
case LOG_EMERG: { // system is unusable
if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) {
syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
2023-04-05 14:43:27 +02:00
}
} break;
default: {
2023-08-06 07:34:17 +02:00
//fprintf(stderr, "%s No ErrorLevel defined! %s\n",
// datetime.toStdString().c_str(), msg.toStdString().c_str());
2023-04-05 14:43:27 +02:00
}
}
}
#endif