2023-04-05 14:43:27 +02:00
|
|
|
#include "message_handler.h"
|
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <cstring>
|
|
|
|
|
2023-06-16 16:49:20 +02:00
|
|
|
#define OUTPUT_LEN (512)
|
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 = "hh:mm:ss";
|
|
|
|
// static constexpr const char *format = "dd.MM.yyyy hh:mm:ss";
|
2023-04-05 14:43:27 +02:00
|
|
|
QByteArray localMsg = msg.toLocal8Bit();
|
|
|
|
const char *file = context.file ? context.file : "";
|
|
|
|
const char *function = context.function ? context.function : "";
|
|
|
|
const char *p = std::strstr(function, "::");
|
|
|
|
if (p) {
|
|
|
|
function = p + 2;
|
|
|
|
}
|
|
|
|
char const* output = std::strrchr(file, '/');
|
|
|
|
if (output) {
|
|
|
|
file = output + 1;
|
|
|
|
}
|
|
|
|
qint64 const currentMSecsSinceEpoch = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
int const fractional_part = currentMSecsSinceEpoch % 1000;
|
2023-06-16 16:49:20 +02:00
|
|
|
char buf[OUTPUT_LEN]{};
|
|
|
|
memset(buf, 0x00, sizeof(buf));
|
2023-04-05 14:43:27 +02:00
|
|
|
QDateTime const datetime = QDateTime::fromMSecsSinceEpoch(currentMSecsSinceEpoch);
|
|
|
|
switch (type) {
|
|
|
|
case QtDebugMsg: {
|
2023-06-16 16:49:20 +02:00
|
|
|
if (debugLevel == QtDebugMsg) {
|
|
|
|
snprintf(buf, sizeof(buf)-1, "%30.30s (%20.20s:%04u) %s.%03d DEBG %s\n",
|
|
|
|
function, file, context.line,
|
|
|
|
datetime.time().toString(format).toStdString().c_str(),
|
|
|
|
fractional_part,
|
|
|
|
localMsg.constData());
|
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, "%30.30s (%20.20s:%04u) %s.%03d INFO %s\n",
|
|
|
|
function, file, context.line,
|
|
|
|
datetime.time().toString(format).toStdString().c_str(),
|
|
|
|
fractional_part,
|
|
|
|
localMsg.constData());
|
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, "%30.30s (%20.20s:%04u) %s.%03d WARN %s\n",
|
|
|
|
function, file, context.line,
|
|
|
|
datetime.time().toString(format).toStdString().c_str(),
|
|
|
|
fractional_part,
|
|
|
|
localMsg.constData());
|
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, "%30.30s (%20.20s:%04u) %s.%03d CRIT %s\n",
|
|
|
|
function, file, context.line,
|
|
|
|
datetime.time().toString(format).toStdString().c_str(),
|
|
|
|
fractional_part,
|
|
|
|
localMsg.constData());
|
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, "%30.30s (%20.20s:%04u) %s.%03d FATAL %s\n",
|
|
|
|
function, file, context.line,
|
|
|
|
datetime.time().toString(format).toStdString().c_str(),
|
|
|
|
fractional_part,
|
|
|
|
localMsg.constData());
|
2023-04-05 14:43:27 +02:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
fprintf(stderr, "%*.*s.%03d No ErrorLevel defined! %s\n", OUTPUT_LEN, OUTPUT_LEN,
|
2023-06-16 16:49:20 +02:00
|
|
|
datetime.time().toString(format).toStdString().c_str(), fractional_part,
|
2023-04-05 14:43:27 +02:00
|
|
|
msg.toStdString().c_str());
|
|
|
|
}
|
|
|
|
}
|
2023-06-16 16:49:20 +02:00
|
|
|
fprintf(stderr, "%s\n", buf);
|
2023-04-05 14:43:27 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|