summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Holland <dominik.holland@pelagicore.com>2015-08-03 15:40:38 +0200
committerDominik Holland <dominik.holland@pelagicore.com>2015-08-03 15:40:38 +0200
commitf6142d4216eb6952c6d431968a890263282e01ad (patch)
treefc2b885d70c12b924c62a632dc59446b2d1713f1
parentb981a37047f7a4dbefe46186f2b7869ac06cdb33 (diff)
downloadqtivi-f6142d4216eb6952c6d431968a890263282e01ad.tar.gz
Added support to handle DLTs control messages
Once we get a dlt control messages and the logLevel for a dlt context was changed the QtLoggingCategory will be updated as well. This makes it possible to use <LoogingCategory>().isDebugEnabled() for not performant operations to generate more debug output.
-rw-r--r--TODO1
-rw-r--r--examples/geniviextras/qdlt/main.cpp12
-rw-r--r--src/geniviextras/qdlt.h2
-rw-r--r--src/geniviextras/qdltregistration.cpp67
-rw-r--r--src/geniviextras/qdltregistration.h5
-rw-r--r--src/geniviextras/qdltregistration_p.h10
-rw-r--r--tests/auto/dlt/tst_dlt.cpp2
7 files changed, 83 insertions, 16 deletions
diff --git a/TODO b/TODO
index 8064757..31dc2bf 100644
--- a/TODO
+++ b/TODO
@@ -8,7 +8,6 @@ API:
* The User should be able to select which Socket to be used, or to log to a file
* Add a way to use the DltRegistration messageHandle only as a proxy and also activate the normal stdout logging
-* React on control messages and control the registered QtLoggingCategories with this information
* Add API for registering injection messages and provide a signal when the messages arrive
* Look into reusing QLoggingCategory::defaultCategory instead of the FALLBACK_CATEGORY
* Extend macro to also support the new Q_LOGGING_CATEGORY macro with msgType
diff --git a/examples/geniviextras/qdlt/main.cpp b/examples/geniviextras/qdlt/main.cpp
index 546f58a..0a32de9 100644
--- a/examples/geniviextras/qdlt/main.cpp
+++ b/examples/geniviextras/qdlt/main.cpp
@@ -34,6 +34,8 @@
#include <QTimer>
#include <QtGeniviExtras/QtDlt>
+#include <iostream>
+
#include "loggingcategories.h"
int main(int argc, char *argv[])
@@ -46,9 +48,13 @@ int main(int argc, char *argv[])
timer.connect(&timer, &QTimer::timeout, [] {
static int counter = 0;
counter++;
- qCCritical(FOO) << "FOO CATEGORY";
- qCWarning(BAR) << "BAR CATEGORY";
- qCritical() << "FALLBACK";
+ qCCritical(FOO) << "FOO CATEGORY" << counter;
+ qCWarning(BAR) << "BAR CATEGORY" << counter;
+ qCritical() << "FALLBACK" << counter;
+ if (FOO().isDebugEnabled()) {
+ std::cout << "LONG TAKING OPERATION ONLY ENABLED IN DEBUG" << std::endl;
+ qCDebug(FOO) << "Debug Statement" << counter;
+ }
});
timer.setInterval(1000);
timer.start();
diff --git a/src/geniviextras/qdlt.h b/src/geniviextras/qdlt.h
index e95e4f3..af1f9c4 100644
--- a/src/geniviextras/qdlt.h
+++ b/src/geniviextras/qdlt.h
@@ -52,7 +52,7 @@ QDLT_REGISTER_LOGGING_CATEGORY(CATEGORY, CATEGORYNAME, DLT_CTX_NAME, DLT_CTX_DES
#define QDLT_REGISTER_LOGGING_CATEGORY(CATEGORY, CATEGORYNAME, DLT_CTX_NAME, DLT_CTX_DESCRIPTION) \
struct QDlt ## CATEGORY ## Registrator { \
- QDlt ## CATEGORY ## Registrator() { globalDltRegistration()->registerCategory(CATEGORYNAME , DLT_CTX_NAME, DLT_CTX_DESCRIPTION); } \
+ QDlt ## CATEGORY ## Registrator() { globalDltRegistration()->registerCategory(&CATEGORY() , DLT_CTX_NAME, DLT_CTX_DESCRIPTION); } \
}; \
static QDlt ## CATEGORY ## Registrator qdlt ## CATEGORY ## registrator; \
diff --git a/src/geniviextras/qdltregistration.cpp b/src/geniviextras/qdltregistration.cpp
index 672ab42..742ffaa 100644
--- a/src/geniviextras/qdltregistration.cpp
+++ b/src/geniviextras/qdltregistration.cpp
@@ -36,6 +36,13 @@
#include "qdltregistration_p.h"
#include <QDebug>
+#include <QLoggingCategory>
+
+void qtGeniviLogLevelChangedHandler(char context_id[], uint8_t log_level, uint8_t trace_status)
+{
+ globalDltRegistration()->d_ptr->dltLogLevelChanged(context_id, log_level, trace_status);
+}
+
Q_GLOBAL_STATIC(QDltRegistration, dltRegistration)
@@ -49,10 +56,13 @@ QDltRegistrationPrivate::QDltRegistrationPrivate()
{
}
-void QDltRegistrationPrivate::registerCategory(const char *categoryName, DltContext *dltContext, const char *dltCtxName, const char *dltCtxDescription)
+void QDltRegistrationPrivate::registerCategory(const QLoggingCategory* category, DltContext *dltContext, const char *dltCtxName, const char *dltCtxDescription)
{
DLT_REGISTER_CONTEXT(*dltContext, dltCtxName, dltCtxDescription);
- m_map.insert(QString::fromLatin1(categoryName), dltContext);
+ m_categoryName2DltContext.insert(QString::fromLatin1(category->categoryName()), dltContext);
+ m_ctxName2Category.insert(QString::fromLatin1(dltCtxName), const_cast<QLoggingCategory*>(category));
+ //TODO move to lamda once c++11 is ok to be used
+ DLT_REGISTER_LOG_LEVEL_CHANGED_CALLBACK(*dltContext, &qtGeniviLogLevelChangedHandler);
}
void QDltRegistrationPrivate::setDefaultContext(DltContext *dltContext)
@@ -63,13 +73,58 @@ void QDltRegistrationPrivate::setDefaultContext(DltContext *dltContext)
DltContext *QDltRegistrationPrivate::context(const char *categoryName)
{
const QString category = QString::fromLatin1(categoryName);
- if (!m_map.contains(category) && m_defaultContext)
+ if (!m_categoryName2DltContext.contains(category) && m_defaultContext)
return m_defaultContext;
- return m_map.value(category);
+ return m_categoryName2DltContext.value(category);
}
+void QDltRegistrationPrivate::dltLogLevelChanged(char context_id[], uint8_t log_level, uint8_t trace_status)
+{
+ Q_UNUSED(trace_status)
+
+ const QString contextName = QString::fromLatin1(context_id);
+ if (m_ctxName2Category.contains(contextName))
+ {
+ QList<QtMsgType> msgTypes;
+ //Enable all QtLoggingCategories with a lower severity than the DLT level
+ switch (log_level) {
+ case DLT_LOG_VERBOSE:
+ case DLT_LOG_DEBUG: msgTypes << QtDebugMsg << QtWarningMsg << QtCriticalMsg << QtFatalMsg;
+#if QT_VERSION >= 0x050500
+ msgTypes << QtInfoMsg;
+#endif
+ break;
+ case DLT_LOG_INFO: msgTypes << QtWarningMsg << QtCriticalMsg << QtFatalMsg;
+#if QT_VERSION >= 0x050500
+ msgTypes << QtInfoMsg;
+#endif
+ break;
+ case DLT_LOG_WARN: msgTypes << QtCriticalMsg << QtFatalMsg;
+#if QT_VERSION >= 0x050500
+ msgTypes << QtInfoMsg;
+#endif
+ break;
+ case DLT_LOG_ERROR: msgTypes << QtCriticalMsg << QtFatalMsg; break;
+ case DLT_LOG_FATAL: msgTypes << QtFatalMsg; break;
+ case DLT_LOG_OFF: msgTypes = QList<QtMsgType>(); break;
+ }
+
+ QtMsgType end = QtFatalMsg;
+#if QT_VERSION >= 0x050500
+ end = QtInfoMsg;
+#endif
+
+ for(int i = (int)QtDebugMsg; i <= (int)end; i++) {
+ QtMsgType type = (QtMsgType)i;
+ bool enabled = true;
+ if (!msgTypes.contains(type))
+ enabled = !enabled;
+ m_ctxName2Category.value(contextName)->setEnabled(type, enabled);
+ }
+ }
+}
QDltRegistration::QDltRegistration()
: d_ptr(new QDltRegistrationPrivate())
@@ -88,11 +143,11 @@ void QDltRegistration::registerApplication(const char *dltAppID, const char *dlt
DLT_REGISTER_APP(dltAppID, dltAppDescription);
}
-void QDltRegistration::registerCategory(const char *categoryName, const char *dltCtxName, const char *dltCtxDescription)
+void QDltRegistration::registerCategory(const QLoggingCategory* category, const char *dltCtxName, const char *dltCtxDescription)
{
Q_D(QDltRegistration);
//TODO memory leak
- d->registerCategory(categoryName, new DltContext, dltCtxName, dltCtxDescription);
+ d->registerCategory(category, new DltContext, dltCtxName, dltCtxDescription);
}
void QDltRegistration::setDefaultContext(const char *categoryName)
diff --git a/src/geniviextras/qdltregistration.h b/src/geniviextras/qdltregistration.h
index 8481c23..81bc476 100644
--- a/src/geniviextras/qdltregistration.h
+++ b/src/geniviextras/qdltregistration.h
@@ -38,6 +38,7 @@
#include <QString>
#include <QMap>
#include <QtGeniviExtras/qgeniviextrasglobal.h>
+#include <stdint.h>
class QDltRegistrationPrivate;
@@ -50,13 +51,15 @@ public:
void registerApplication(const char *dltAppID, const char *dltAppDescription);
void unregisterApplication();
- void registerCategory(const char* categoryName, const char* dltCtxName, const char* dltCtxDescription);
+ void registerCategory(const QLoggingCategory *category, const char* dltCtxName, const char* dltCtxDescription);
void setDefaultContext(const char* categoryName);
static void messageHandler(QtMsgType msgTypes, const QMessageLogContext &msgCtx, const QString &msg);
private:
QDltRegistrationPrivate* const d_ptr;
Q_DECLARE_PRIVATE(QDltRegistration)
+
+ friend void qtGeniviLogLevelChangedHandler(char context_id[], uint8_t log_level, uint8_t trace_status);
};
Q_GENIVIEXTRAS_EXPORT extern QDltRegistration* globalDltRegistration();
diff --git a/src/geniviextras/qdltregistration_p.h b/src/geniviextras/qdltregistration_p.h
index f3f71f4..b2cb93f 100644
--- a/src/geniviextras/qdltregistration_p.h
+++ b/src/geniviextras/qdltregistration_p.h
@@ -33,20 +33,24 @@
****************************************************************************/
#include <QString>
-#include <QMap>
+#include <QHash>
#include <dlt/dlt.h>
+void qtGeniviLogLevelChangedHandler(char context_id[], uint8_t log_level, uint8_t trace_status);
+
class QDltRegistrationPrivate {
public:
QDltRegistrationPrivate();
- void registerCategory(const char* categoryName, DltContext* dltContext, const char* dltCtxName, const char* dltCtxDescription);
+ void registerCategory(const QLoggingCategory* category, DltContext *dltContext, const char *dltCtxName, const char *dltCtxDescription);
void setDefaultContext(DltContext* dltContext);
DltContext* context(const char* categoryName);
+ void dltLogLevelChanged(char context_id[], uint8_t log_level, uint8_t trace_status);
QString m_dltAppID;
DltContext* m_defaultContext;
- QMap<QString, DltContext*> m_map;
+ QHash<QString, DltContext*> m_categoryName2DltContext;
+ QHash<QString, QLoggingCategory*> m_ctxName2Category;
};
diff --git a/tests/auto/dlt/tst_dlt.cpp b/tests/auto/dlt/tst_dlt.cpp
index f877722..22e2048 100644
--- a/tests/auto/dlt/tst_dlt.cpp
+++ b/tests/auto/dlt/tst_dlt.cpp
@@ -139,7 +139,7 @@ void QDltTest::testLogging()
{
QDltRegistration* registration = globalDltRegistration();
registration->registerApplication("APP1", "Description for APP");
- registration->registerCategory(TEST1().categoryName(), "TES1", "Test Category One");
+ registration->registerCategory(&TEST1(), "TES1", "Test Category One");
QString msg = QLatin1Literal("TEST");
QString expectedMsg = QString("%1: \"%2\"").arg(TEST1().categoryName()).arg(msg);