Use Qt to format debug-info. Note that the full info is visible only in debug-mode.

This commit is contained in:
Gerhard Hoffmann 2023-07-14 12:58:23 +02:00
parent 5149a67d4b
commit 9df425f5f8

View File

@ -2,6 +2,9 @@
#include <QDateTime> #include <QDateTime>
#include <cstring> #include <cstring>
#include <QString>
#include <QFileInfo>
#include <QMessageLogContext>
#define OUTPUT_LEN (512) #define OUTPUT_LEN (512)
@ -42,51 +45,43 @@ 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 = "hh:mm:ss";
// static constexpr const char *format = "dd.MM.yyyy hh:mm:ss"; // static constexpr const char *format = "dd.MM.yyyy hh:mm:ss";
QByteArray localMsg = msg.toLocal8Bit(); QByteArray localMsg = msg.toLocal8Bit();
const char *file = context.file ? context.file : ""; QString fileName(context.file ? context.file : "N/A");
const char *function = context.function ? context.function : ""; QString function(context.function ? context.function : "N/A");
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;
char buf[OUTPUT_LEN]{}; char buf[OUTPUT_LEN]{};
memset(buf, 0x00, sizeof(buf)); memset(buf, 0x00, sizeof(buf));
QDateTime const datetime = QDateTime::fromMSecsSinceEpoch(currentMSecsSinceEpoch); QString const datetime = QDateTime::currentDateTime().toString(Qt::ISODateWithMs);
switch (type) { switch (type) {
case QtDebugMsg: { case QtDebugMsg: {
if (debugLevel == QtDebugMsg) { if (debugLevel == QtDebugMsg) {
snprintf(buf, sizeof(buf)-1, "%30.30s (%20.20s:%04u) %s.%03d DEBG %s", snprintf(buf, sizeof(buf)-1, "%s DEBG [%s:%s:%04u] %s",
function, file, context.line, datetime.toStdString().c_str(),
datetime.time().toString(format).toStdString().c_str(), function.toStdString().c_str(),
fractional_part, fileName.toStdString().c_str(),
context.line,
localMsg.constData()); localMsg.constData());
fprintf(stderr, "%s\n", buf); fprintf(stderr, "%s\n", buf);
} }
} break; } break;
case QtInfoMsg: { case QtInfoMsg: {
if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg) { if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg) {
snprintf(buf, sizeof(buf)-1, "%30.30s (%20.20s:%04u) %s.%03d INFO %s", snprintf(buf, sizeof(buf)-1, "%s DEBG [%s:%s:%04u] %s",
function, file, context.line, datetime.toStdString().c_str(),
datetime.time().toString(format).toStdString().c_str(), function.toStdString().c_str(),
fractional_part, fileName.toStdString().c_str(),
context.line,
localMsg.constData()); localMsg.constData());
fprintf(stderr, "%s\n", buf); fprintf(stderr, "%s\n", buf);
} }
} break; } break;
case QtWarningMsg: { case QtWarningMsg: {
if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg || debugLevel == QtWarningMsg) { if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg || debugLevel == QtWarningMsg) {
snprintf(buf, sizeof(buf)-1, "%30.30s (%20.20s:%04u) %s.%03d WARN %s", snprintf(buf, sizeof(buf)-1, "%s DEBG [%s:%s:%04u] %s",
function, file, context.line, datetime.toStdString().c_str(),
datetime.time().toString(format).toStdString().c_str(), function.toStdString().c_str(),
fractional_part, fileName.toStdString().c_str(),
context.line,
localMsg.constData()); localMsg.constData());
fprintf(stderr, "%s\n", buf); fprintf(stderr, "%s\n", buf);
} }
@ -94,10 +89,11 @@ void atbDebugOutput(QtMsgType type, const QMessageLogContext &context, const QSt
case QtCriticalMsg: { case QtCriticalMsg: {
if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg
|| debugLevel == QtWarningMsg || debugLevel == QtCriticalMsg) { || debugLevel == QtWarningMsg || debugLevel == QtCriticalMsg) {
snprintf(buf, sizeof(buf)-1, "%30.30s (%20.20s:%04u) %s.%03d CRIT %s", snprintf(buf, sizeof(buf)-1, "%s DEBG [%s:%s:%04u] %s",
function, file, context.line, datetime.toStdString().c_str(),
datetime.time().toString(format).toStdString().c_str(), function.toStdString().c_str(),
fractional_part, fileName.toStdString().c_str(),
context.line,
localMsg.constData()); localMsg.constData());
fprintf(stderr, "%s\n", buf); fprintf(stderr, "%s\n", buf);
} }
@ -106,18 +102,18 @@ void atbDebugOutput(QtMsgType type, const QMessageLogContext &context, const QSt
if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg
|| debugLevel == QtWarningMsg || debugLevel == QtCriticalMsg || debugLevel == QtWarningMsg || debugLevel == QtCriticalMsg
|| debugLevel == QtFatalMsg) { || debugLevel == QtFatalMsg) {
snprintf(buf, sizeof(buf)-1, "%30.30s (%20.20s:%04u) %s.%03d FATAL %s", snprintf(buf, sizeof(buf)-1, "%s DEBG [%s:%s:%04u] %s",
function, file, context.line, datetime.toStdString().c_str(),
datetime.time().toString(format).toStdString().c_str(), function.toStdString().c_str(),
fractional_part, fileName.toStdString().c_str(),
context.line,
localMsg.constData()); localMsg.constData());
fprintf(stderr, "%s\n", buf); fprintf(stderr, "%s\n", buf);
} }
} break; } break;
default: { default: {
fprintf(stderr, "%*.*s.%03d No ErrorLevel defined! %s\n", OUTPUT_LEN, OUTPUT_LEN, fprintf(stderr, "%s No ErrorLevel defined! %s\n",
datetime.time().toString(format).toStdString().c_str(), fractional_part, datetime.toStdString().c_str(), msg.toStdString().c_str());
msg.toStdString().c_str());
} }
} }
} }