ATBUpdateTool/message_handler.cpp

122 lines
4.5 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
#define OUTPUT_LEN (1024)
2023-04-05 14:43:27 +02:00
static bool installedMsgHandler = false;
static QtMsgType debugLevel = QtInfoMsg;
QtMsgType getDebugLevel() { return debugLevel; }
void setDebugLevel(QtMsgType newDebugLevel) {
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-06-16 16:49:20 +02:00
// static constexpr const char *format = "dd.MM.yyyy hh:mm:ss";
2023-04-05 14:43:27 +02:00
QByteArray localMsg = msg.toLocal8Bit();
QString fileName(context.file ? context.file : "N/A");
QString function(context.function ? context.function : "N/A");
2023-06-16 16:49:20 +02:00
char buf[OUTPUT_LEN]{};
memset(buf, 0x00, sizeof(buf));
QString const datetime = QDateTime::currentDateTime().toString(Qt::ISODateWithMs);
2023-04-05 14:43:27 +02:00
switch (type) {
case QtDebugMsg: {
2023-06-16 16:49:20 +02:00
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,
2023-06-16 16:49:20 +02:00
localMsg.constData());
fprintf(stderr, "%s\n", buf);
2023-04-05 14:43:27 +02:00
}
} break;
case QtInfoMsg: {
2023-06-16 16:49:20 +02:00
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,
2023-06-16 16:49:20 +02:00
localMsg.constData());
fprintf(stderr, "%s\n", buf);
2023-04-05 14:43:27 +02:00
}
} break;
case QtWarningMsg: {
2023-06-16 16:49:20 +02:00
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,
2023-06-16 16:49:20 +02:00
localMsg.constData());
fprintf(stderr, "%s\n", buf);
2023-04-05 14:43:27 +02:00
}
} break;
case QtCriticalMsg: {
2023-06-16 16:49:20 +02:00
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,
2023-06-16 16:49:20 +02:00
localMsg.constData());
fprintf(stderr, "%s\n", buf);
2023-04-05 14:43:27 +02:00
}
} break;
case QtFatalMsg: {
2023-06-16 16:49:20 +02:00
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,
2023-06-16 16:49:20 +02:00
localMsg.constData());
fprintf(stderr, "%s\n", buf);
2023-04-05 14:43:27 +02:00
}
} break;
default: {
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