diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-02-20 16:11:45 +0100 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-02-20 16:11:45 +0100 |
| commit | c3ea69c02a5c877fa86ccbeee5c4efa11ae2ee5c (patch) | |
| tree | a60f1ca72a72848fb0ee2431944f36533aac1d5b /demo/main.cpp | |
| parent | 6c142a234673e442561b2c050b727aa4400177d8 (diff) | |
| download | BobinkQtOpcUa-c3ea69c02a5c877fa86ccbeee5c4efa11ae2ee5c.tar.gz BobinkQtOpcUa-c3ea69c02a5c877fa86ccbeee5c4efa11ae2ee5c.zip | |
Add custom log handler and expand demo node coverage
Replace default Qt message pattern with a custom handler that matches
open62541 server log format: timestamps with UTC offset, colored
type/category tags padded to 20 chars, and shortened category names.
Add sbyte, byte, datetime, guid, and bytestring node types to all
three demo pages. Add Guid type coercion to writeValue.
Diffstat (limited to 'demo/main.cpp')
| -rw-r--r-- | demo/main.cpp | 81 |
1 files changed, 70 insertions, 11 deletions
diff --git a/demo/main.cpp b/demo/main.cpp index 6d0b9be..689b998 100644 --- a/demo/main.cpp +++ b/demo/main.cpp @@ -2,24 +2,83 @@ * @file main.cpp * @brief Entry point for the Bobink demo application. */ +#include <QDateTime> #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QtQml/QQmlExtensionPlugin> -Q_IMPORT_QML_PLUGIN(BobinkPlugin) +Q_IMPORT_QML_PLUGIN (BobinkPlugin) -int main(int argc, char *argv[]) +/** @brief Custom log handler matching open62541 server log format. */ +static void +logHandler (QtMsgType type, const QMessageLogContext &ctx, const QString &msg) { - // Load the locally-built OpcUa backend plugin (open62541). - QCoreApplication::addLibraryPath(QStringLiteral(QTOPCUA_PLUGIN_PATH)); + // Color only the type/category tag. + const char *color = ""; + const char *label = "debug"; + switch (type) + { + case QtDebugMsg: + label = "debug"; + break; + case QtInfoMsg: + color = "\x1b[32m"; + label = "info"; + break; + case QtWarningMsg: + color = "\x1b[33m"; + label = "warning"; + break; + case QtCriticalMsg: + color = "\x1b[31m"; + label = "critical"; + break; + case QtFatalMsg: + color = "\x1b[1;31m"; + label = "fatal"; + break; + } - QGuiApplication app(argc, argv); + // Shorten "qt.opcua.plugins.open62541.sdk.client" → "client". + QLatin1StringView cat (ctx.category ? ctx.category : "default"); + qsizetype dot = cat.lastIndexOf (QLatin1Char ('.')); + if (dot >= 0) + cat = cat.sliced (dot + 1); - QQmlApplicationEngine engine; - QObject::connect ( - &engine, &QQmlApplicationEngine::objectCreationFailed, &app, - [] () { QCoreApplication::exit (1); }, Qt::QueuedConnection); + // "debug/client", "warning/network", etc. — padded to 20 chars. + QByteArray tag + = QByteArray (label) + '/' + QByteArray (cat.data (), cat.size ()); - engine.loadFromModule("BobinkDemo", "Main"); - return app.exec(); + // Format UTC offset as "(UTC+0100)" to match open62541 server logs. + QDateTime now = QDateTime::currentDateTime (); + qint32 offset = now.offsetFromUtc (); + QChar sign = offset >= 0 ? u'+' : u'-'; + offset = qAbs (offset); + QString ts = now.toString (u"yyyy-MM-dd HH:mm:ss.zzz") + + QStringLiteral (" (UTC%1%2%3)") + .arg (sign) + .arg (offset / 3600, 2, 10, QLatin1Char ('0')) + .arg ((offset % 3600) / 60, 2, 10, QLatin1Char ('0')); + + fprintf (stderr, "[%s] %s%-20.*s\x1b[0m %s\n", qPrintable (ts), color, + static_cast<int> (tag.size ()), tag.data (), qPrintable (msg)); +} + +int +main (int argc, char *argv[]) +{ + // Load the locally-built OpcUa backend plugin (open62541). + QCoreApplication::addLibraryPath (QStringLiteral (QTOPCUA_PLUGIN_PATH)); + + qInstallMessageHandler (logHandler); + + QGuiApplication app (argc, argv); + + QQmlApplicationEngine engine; + QObject::connect ( + &engine, &QQmlApplicationEngine::objectCreationFailed, &app, + [] () { QCoreApplication::exit (1); }, Qt::QueuedConnection); + + engine.loadFromModule ("BobinkDemo", "Main"); + return app.exec (); } |
