From 78beb6acca90fb2f2490e2b60d2103a25f1508fe Mon Sep 17 00:00:00 2001
From: Dominik Holland <dominik.holland@pelagicore.com>
Date: Wed, 28 Aug 2019 14:53:22 +0200
Subject: [PATCH 1/2] Add a option to configure the behavior of very long log
 messages passed to DLT

This uses a new config option in QtGeniviExtras and makes it possible
to set this in the am-config.yaml

Change-Id: I7e13e9114dd0e281cdd8bf85e517bd5654f49c64
---
 doc/configuration.qdoc                  |  8 ++++++++
 src/common-lib/common-lib.pro           |  3 +++
 src/common-lib/logging.cpp              | 25 +++++++++++++++++++++++++
 src/common-lib/logging.h                |  3 +++
 src/launcher-lib/launchermain.cpp       |  8 ++++++++
 src/launcher-lib/launchermain.h         |  2 ++
 src/main-lib/defaultconfiguration.cpp   |  9 +++++++++
 src/main-lib/defaultconfiguration.h     |  1 +
 src/main-lib/main.cpp                   |  1 +
 src/manager-lib/nativeruntime.cpp       |  6 +++++-
 src/tools/launcher-qml/launcher-qml.cpp |  2 ++
 11 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/doc/configuration.qdoc b/doc/configuration.qdoc
index 72aded9d..3e625e8e 100644
--- a/doc/configuration.qdoc
+++ b/doc/configuration.qdoc
@@ -314,6 +314,14 @@ all your imports paths and file references relative to your main config file.
     \li If provided, it will be used as the automotive DLT application description. This allows to
         augment the short DLT application id with a more verbose definition. Note that a default
         description is used until this configuration option has been applied.
+\row
+    \li \b -
+    \br \e logging/dlt/longMessageBehavior
+    \li string
+    \li Configures how very long messages (more than ~1000 characters) should be handled when using
+        automotive DLT. The default behavior is to truncate the message to the maximum possible
+        size. Other options are \c split to cut the message into several smaller messages, or
+        \c pass to pass it as is to DLT.
 \row
     \li \b --no-dlt-logging
     \br \e -
diff --git a/src/common-lib/common-lib.pro b/src/common-lib/common-lib.pro
index 2ee3a8eb..7faaa348 100644
--- a/src/common-lib/common-lib.pro
+++ b/src/common-lib/common-lib.pro
@@ -13,6 +13,9 @@ qtHaveModule(qml):QT *= qml qml-private
 versionAtLeast(QT.geniviextras.VERSION, 1.1.0) {
     DEFINES += AM_GENIVIEXTRAS_LAZY_INIT
 }
+versionAtLeast(QT.geniviextras.VERSION, 5.12.4) {
+    DEFINES += AM_GENIVIEXTRAS_MSG_BEHAVIOR
+}
 
 CONFIG *= static internal_module
 
diff --git a/src/common-lib/logging.cpp b/src/common-lib/logging.cpp
index f14629d0..b4be7fed 100644
--- a/src/common-lib/logging.cpp
+++ b/src/common-lib/logging.cpp
@@ -122,6 +122,7 @@ QStringList Logging::s_rules;
 QtMessageHandler Logging::s_defaultQtHandler = nullptr;
 QByteArray Logging::s_applicationId = QByteArray();
 QVariant Logging::s_useAMConsoleLoggerConfig = QVariant();
+QString Logging::s_dltLongMessageBehavior = QString();
 
 
 static void colorLogToStderr(QtMsgType msgType, const QMessageLogContext &context, const QString &message)
@@ -410,6 +411,30 @@ void Logging::setDltApplicationId(const QByteArray &dltAppId, const QByteArray &
 #endif
 }
 
+QString Logging::dltLongMessageBehavior()
+{
+    return s_dltLongMessageBehavior;
+}
+
+void Logging::setDltLongMessageBehavior(const QString &behaviorString)
+{
+#if defined(AM_GENIVIEXTRAS_MSG_BEHAVIOR)
+    if (!s_dltEnabled)
+        return;
+
+    s_dltLongMessageBehavior = behaviorString;
+    QDltRegistration::LongMessageBehavior behavior = QDltRegistration::LongMessageBehavior::Truncate;
+    if (behaviorString == qL1S("split"))
+        behavior = QDltRegistration::LongMessageBehavior::Split;
+    else if (behaviorString == qL1S("pass"))
+        behavior = QDltRegistration::LongMessageBehavior::Pass;
+
+    globalDltRegistration()->setLongMessageBehavior(behavior);
+#else
+    Q_UNUSED(behaviorString)
+#endif
+}
+
 void Logging::logToDlt(QtMsgType msgType, const QMessageLogContext &context, const QString &message)
 {
 #if defined(QT_GENIVIEXTRAS_LIB)
diff --git a/src/common-lib/logging.h b/src/common-lib/logging.h
index 7671a6cb..b986f7a8 100644
--- a/src/common-lib/logging.h
+++ b/src/common-lib/logging.h
@@ -79,6 +79,8 @@ public:
     static void registerUnregisteredDltContexts();
     static void setSystemUiDltId(const QByteArray &dltAppId, const QByteArray &dltAppDescription);
     static void setDltApplicationId(const QByteArray &dltAppId, const QByteArray &dltAppDescription);
+    static QString dltLongMessageBehavior();
+    static void setDltLongMessageBehavior(const QString &behaviorString);
 
     static void logToDlt(QtMsgType msgType, const QMessageLogContext &context, const QString &message);
 
@@ -90,6 +92,7 @@ private:
     static QtMessageHandler s_defaultQtHandler;
     static QByteArray s_applicationId;
     static QVariant s_useAMConsoleLoggerConfig;
+    static QString s_dltLongMessageBehavior;
 };
 
 void am_trace(QDebug);
diff --git a/src/launcher-lib/launchermain.cpp b/src/launcher-lib/launchermain.cpp
index 5b573c4e..e2be9e8a 100644
--- a/src/launcher-lib/launchermain.cpp
+++ b/src/launcher-lib/launchermain.cpp
@@ -123,6 +123,11 @@ QVariant LauncherMain::useAMConsoleLogger() const
     return m_useAMConsoleLogger;
 }
 
+QString LauncherMain::dltLongMessageBehavior() const
+{
+    return m_dltLongMessageBehavior;
+}
+
 QString LauncherMain::p2pDBusName() const
 {
     return qSL("am");
@@ -217,6 +222,9 @@ void LauncherMain::loadConfiguration(const QByteArray &configYaml) Q_DECL_NOEXCE
     m_iconThemeName = uiConfig.value(qSL("iconThemeName")).toString();
     m_iconThemeSearchPaths = uiConfig.value(qSL("iconThemeSearchPaths")).toStringList();
 
+    QVariantMap dltConfig = m_configuration.value(qSL("dlt")).toMap();
+    m_dltLongMessageBehavior = uiConfig.value(qSL("longMessageBehavior")).toString();
+
     // un-comment this if things go south:
     //qWarning() << "### LOG " << m_loggingRules;
     //qWarning() << "### DBUS" << dbusConfig;
diff --git a/src/launcher-lib/launchermain.h b/src/launcher-lib/launchermain.h
index 7ff1dd31..2aca1a4f 100644
--- a/src/launcher-lib/launchermain.h
+++ b/src/launcher-lib/launchermain.h
@@ -86,6 +86,7 @@ public:
     QVariantMap systemProperties() const;
     QStringList loggingRules() const;
     QVariant useAMConsoleLogger() const;
+    QString dltLongMessageBehavior() const;
 
     QString p2pDBusName() const;
     QString notificationDBusName() const;
@@ -120,6 +121,7 @@ private:
     QString m_iconThemeName;
     QStringList m_iconThemeSearchPaths;
     QVariant m_useAMConsoleLogger;
+    QString m_dltLongMessageBehavior;
     WaylandQtAMClientExtension *m_waylandExtension = nullptr;
 };
 
diff --git a/src/main-lib/defaultconfiguration.cpp b/src/main-lib/defaultconfiguration.cpp
index b3469534..24b32a83 100644
--- a/src/main-lib/defaultconfiguration.cpp
+++ b/src/main-lib/defaultconfiguration.cpp
@@ -148,6 +148,10 @@ void DefaultConfiguration::parseWithArguments(const QStringList &arguments, QStr
         *deploymentWarnings << qL1S("No --installed-apps-manifest-dir command line parameter or"
                 " applications/installedAppsManifestDir configuration key specified. It won't be possible to install,"
                 " remove or access installable applications.");
+
+    static const QStringList validDltLMBValues({qL1S("split"), qL1S("truncate"), qL1S("pass")});
+    if (!dltLongMessageBehavior().isEmpty() && !validDltLMBValues.contains(dltLongMessageBehavior()))
+        *deploymentWarnings << qL1S("Invalid dltLongMessageBehavior");
 }
 
 QString DefaultConfiguration::mainQmlFile() const
@@ -345,6 +349,11 @@ QString DefaultConfiguration::dltDescription() const
     return value<QString>(nullptr, { "logging", "dlt", "description" });
 }
 
+QString DefaultConfiguration::dltLongMessageBehavior() const
+{
+    return value<QString>(nullptr, { "logging", "dlt", "longMessageBehavior" });
+}
+
 QVariantMap DefaultConfiguration::openGLConfiguration() const
 {
     return value<QVariant>(nullptr, { "ui", "opengl" }).toMap();
diff --git a/src/main-lib/defaultconfiguration.h b/src/main-lib/defaultconfiguration.h
index 0a21394f..10382c7b 100644
--- a/src/main-lib/defaultconfiguration.h
+++ b/src/main-lib/defaultconfiguration.h
@@ -96,6 +96,7 @@ public:
     bool enableTouchEmulation() const;
     QString dltId() const;
     QString dltDescription() const;
+    QString dltLongMessageBehavior() const;
 
     QVariantMap openGLConfiguration() const;
 
diff --git a/src/main-lib/main.cpp b/src/main-lib/main.cpp
index 37fcf4a7..7013a898 100644
--- a/src/main-lib/main.cpp
+++ b/src/main-lib/main.cpp
@@ -210,6 +210,7 @@ void Main::setup(const DefaultConfiguration *cfg, const QStringList &deploymentW
     setupQmlDebugging(cfg->qmlDebugging());
     if (!cfg->dltId().isEmpty() || !cfg->dltDescription().isEmpty())
         Logging::setSystemUiDltId(cfg->dltId().toLocal8Bit(), cfg->dltDescription().toLocal8Bit());
+    Logging::setDltLongMessageBehavior(cfg->dltLongMessageBehavior());
     Logging::registerUnregisteredDltContexts();
 
     // dump accumulated warnings, now that logging rules are set
diff --git a/src/manager-lib/nativeruntime.cpp b/src/manager-lib/nativeruntime.cpp
index 3c1865b6..d52e5688 100644
--- a/src/manager-lib/nativeruntime.cpp
+++ b/src/manager-lib/nativeruntime.cpp
@@ -259,8 +259,12 @@ bool NativeRuntime::start()
         { qSL("org.freedesktop.Notifications"), NotificationManager::instance()->property("_am_dbus_name").toString()}
     };
 
+    QVariantMap dltConfig = {
+        { qSL("longMessageBehavior"),  Logging::dltLongMessageBehavior() }
+    };
+
     QVariantMap loggingConfig = {
-        { qSL("dlt"), Logging::isDltEnabled() },
+        { qSL("dlt"),  dltConfig },
         { qSL("rules"), Logging::filterRules() },
         { qSL("useAMConsoleLogger"), Logging::useAMConsoleLogger() }
     };
diff --git a/src/tools/launcher-qml/launcher-qml.cpp b/src/tools/launcher-qml/launcher-qml.cpp
index 7b25117f..2ad635da 100644
--- a/src/tools/launcher-qml/launcher-qml.cpp
+++ b/src/tools/launcher-qml/launcher-qml.cpp
@@ -150,6 +150,8 @@ int main(int argc, char *argv[])
         a.setupIconTheme(a.iconThemeSearchPaths(), a.iconThemeName());
         a.registerWaylandExtensions();
 
+        Logging::setDltLongMessageBehavior(a.dltLongMessageBehavior());
+
         StartupTimer::instance()->checkpoint("after basic initialization");
 
         if (!directLoad.isEmpty()) {
-- 
2.20.1

