fixed handling of debug levels

This commit is contained in:
Gerhard Hoffmann 2023-06-16 16:49:20 +02:00
parent dc90705f24
commit c8487a7541

View File

@ -3,8 +3,7 @@
#include <QDateTime> #include <QDateTime>
#include <cstring> #include <cstring>
#define OUTPUT_LEN (20) #define OUTPUT_LEN (512)
static bool installedMsgHandler = false; static bool installedMsgHandler = false;
static QtMsgType debugLevel = QtInfoMsg; static QtMsgType debugLevel = QtInfoMsg;
@ -41,49 +40,10 @@ QtMessageHandler atbInstallMessageHandler(QtMessageHandler handler) {
/// return app.exec(); /// return app.exec();
/// } /// }
/// ///
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) #if (QT_VERSION > QT_VERSION_CHECK(5, 0, 0) && QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
void atbDebugOutput(QtMsgType type, const char *msg) {
switch (type) {
case QtDebugMsg: {
if (debugLevel <= QtDebugMsg) {
fprintf(stderr, "%*.*s Debug: %s\n", OUTPUT_LEN, OUTPUT_LEN,
QDateTime::currentDateTime().toString(Qt::ISODate).toStdString().c_str(), msg);
}
} break;
case QtInfoMsg: {
if (debugLevel <= QtInfoMsg) {
fprintf(stderr, "%*.*s Info: %s\n", OUTPUT_LEN, OUTPUT_LEN,
QDateTime::currentDateTime().toString(Qt::ISODate).toStdString().c_str(), msg);
}
} break;
case QtWarningMsg: {
if (debugLevel <= QtWarningMsg) {
fprintf(stderr, "%*.*s Warning: %s\n", OUTPUT_LEN, OUTPUT_LEN,
QDateTime::currentDateTime().toString(Qt::ISODate).toStdString().c_str(), msg);
}
} break;
case QtCriticalMsg: {
if (debugLevel <= QtCriticalMsg) {
fprintf(stderr, "%*.*s Critical: %s\n", OUTPUT_LEN, OUTPUT_LEN,
QDateTime::currentDateTime().toString(Qt::ISODate).toStdString().c_str(), msg);
}
} break;
case QtFatalMsg: {
if (debugLevel <= QtFatalMsg) {
fprintf(stderr, "%*.*s Fatal: %s\n", OUTPUT_LEN, OUTPUT_LEN,
QDateTime::currentDateTime().toString(Qt::ISODate).toStdString().c_str(), msg);
}
// abort();
} break;
default: {
fprintf(stderr, "%*.*s No ErrorLevel defined! %s\n", OUTPUT_LEN, OUTPUT_LEN,
QDateTime::currentDateTime().toString(Qt::ISODate).toStdString().c_str(), msg);
}
}
}
#elif 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"; static constexpr const char *format = "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 : ""; const char *file = context.file ? context.file : "";
const char *function = context.function ? context.function : ""; const char *function = context.function ? context.function : "";
@ -97,54 +57,65 @@ void atbDebugOutput(QtMsgType type, const QMessageLogContext &context, const QSt
} }
qint64 const currentMSecsSinceEpoch = QDateTime::currentMSecsSinceEpoch(); qint64 const currentMSecsSinceEpoch = QDateTime::currentMSecsSinceEpoch();
int const fractional_part = currentMSecsSinceEpoch % 1000; int const fractional_part = currentMSecsSinceEpoch % 1000;
char buf[OUTPUT_LEN]{};
memset(buf, 0x00, sizeof(buf));
QDateTime const datetime = QDateTime::fromMSecsSinceEpoch(currentMSecsSinceEpoch); QDateTime const datetime = QDateTime::fromMSecsSinceEpoch(currentMSecsSinceEpoch);
switch (type) { switch (type) {
case QtDebugMsg: { case QtDebugMsg: {
if (debugLevel <= QtDebugMsg) { if (debugLevel == QtDebugMsg) {
// fprintf(stderr, "%*.*s CTX %s (%s:%u) ->\n", OUTPUT_LEN, OUTPUT_LEN, snprintf(buf, sizeof(buf)-1, "%30.30s (%20.20s:%04u) %s.%03d DEBG %s\n",
// "", function, file, context.line); function, file, context.line,
//fprintf(stderr, "%*.*s.%03d DEBG %s\n", OUTPUT_LEN, OUTPUT_LEN, datetime.time().toString(format).toStdString().c_str(),
// datetime.toString(format).toStdString().c_str(), fractional_part, fractional_part,
// localMsg.constData()); localMsg.constData());
fprintf(stderr, "%*.*s.%03d DEBUG %s (%s:%u)\n", OUTPUT_LEN, OUTPUT_LEN,
datetime.toString(format).toStdString().c_str(), fractional_part,
localMsg.constData(), file, context.line);
} }
} break; } break;
case QtInfoMsg: { case QtInfoMsg: {
if (debugLevel <= QtInfoMsg) { if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg) {
fprintf(stderr, "%*.*s.%03d INFO %s (%s:%u)\n", OUTPUT_LEN, OUTPUT_LEN, snprintf(buf, sizeof(buf)-1, "%30.30s (%20.20s:%04u) %s.%03d INFO %s\n",
datetime.toString(format).toStdString().c_str(), fractional_part, function, file, context.line,
localMsg.constData(), file, context.line); datetime.time().toString(format).toStdString().c_str(),
fractional_part,
localMsg.constData());
} }
} break; } break;
case QtWarningMsg: { case QtWarningMsg: {
if (debugLevel <= QtWarningMsg) { if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg || debugLevel == QtWarningMsg) {
fprintf(stderr, "%*.*s.%03d WARN %s (%s:%u)\n", OUTPUT_LEN, OUTPUT_LEN, snprintf(buf, sizeof(buf)-1, "%30.30s (%20.20s:%04u) %s.%03d WARN %s\n",
datetime.toString(format).toStdString().c_str(), fractional_part, function, file, context.line,
localMsg.constData(), file, context.line); datetime.time().toString(format).toStdString().c_str(),
fractional_part,
localMsg.constData());
} }
} break; } break;
case QtCriticalMsg: { case QtCriticalMsg: {
if (debugLevel <= QtCriticalMsg) { if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg
fprintf(stderr, "%*.*s.%03d CRIT %s (%s:%u)\n", OUTPUT_LEN, OUTPUT_LEN, || debugLevel == QtWarningMsg || debugLevel == QtCriticalMsg) {
datetime.toString(format).toStdString().c_str(), fractional_part, snprintf(buf, sizeof(buf)-1, "%30.30s (%20.20s:%04u) %s.%03d CRIT %s\n",
localMsg.constData(), file, context.line); function, file, context.line,
datetime.time().toString(format).toStdString().c_str(),
fractional_part,
localMsg.constData());
} }
} break; } break;
case QtFatalMsg: { case QtFatalMsg: {
if (debugLevel <= QtFatalMsg) { if (debugLevel == QtInfoMsg || debugLevel == QtDebugMsg
fprintf(stderr, "%*.*s.%03d FATAL %s (%s:%u)\n", OUTPUT_LEN, OUTPUT_LEN, || debugLevel == QtWarningMsg || debugLevel == QtCriticalMsg
datetime.toString(format).toStdString().c_str(), fractional_part, || debugLevel == QtFatalMsg) {
localMsg.constData(), file, context.line); 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());
} }
} break; } break;
default: { default: {
fprintf(stderr, "%*.*s.%03d No ErrorLevel defined! %s\n", OUTPUT_LEN, OUTPUT_LEN, fprintf(stderr, "%*.*s.%03d No ErrorLevel defined! %s\n", OUTPUT_LEN, OUTPUT_LEN,
datetime.toString(format).toStdString().c_str(), fractional_part, datetime.time().toString(format).toStdString().c_str(), fractional_part,
msg.toStdString().c_str()); msg.toStdString().c_str());
} }
} }
fprintf(stderr, "%s\n", buf);
} }
#endif #endif