diff options
Diffstat (limited to 'src/geniviextras/qdltregistration.cpp')
-rw-r--r-- | src/geniviextras/qdltregistration.cpp | 77 |
1 files changed, 53 insertions, 24 deletions
diff --git a/src/geniviextras/qdltregistration.cpp b/src/geniviextras/qdltregistration.cpp index 43e230f..47abe21 100644 --- a/src/geniviextras/qdltregistration.cpp +++ b/src/geniviextras/qdltregistration.cpp @@ -55,7 +55,13 @@ QT_BEGIN_NAMESPACE void qtGeniviLogLevelChangedHandler(char context_id[], uint8_t log_level, uint8_t trace_status) { - globalDltRegistration()->d_ptr->dltLogLevelChanged(context_id, log_level, trace_status); + auto d = globalDltRegistration()->d_ptr; + d->m_mutex.lock(); + const QLoggingCategory *category = d->dltLogLevelChanged(context_id, log_level, trace_status); + d->m_mutex.unlock(); + + if (category) + emit globalDltRegistration()->logLevelChanged(category); } Q_GLOBAL_STATIC(QDltRegistration, dltRegistration) @@ -93,8 +99,6 @@ void QDltRegistrationPrivate::registerCategory(const QLoggingCategory *category, void QDltRegistrationPrivate::registerCategory(CategoryInfo &info) { - Q_ASSERT_X(m_dltAppRegistered, "registerCategory", "A DLT Application needs to be registered before registering a Logging Category"); - #if GENIVIEXTRAS_DEBUG std::cout << "REGISTERING CONTEXT " << info.m_ctxName.constData() << std::endl; #endif @@ -114,6 +118,14 @@ void QDltRegistrationPrivate::registerApplication() m_dltAppRegistered = true; } +void QDltRegistrationPrivate::unregisterApplication() +{ + if (m_dltAppRegistered) + DLT_UNREGISTER_APP(); + + m_dltAppRegistered = false; +} + void QDltRegistrationPrivate::setDefaultCategory(const QString &category) { Q_ASSERT_X(m_categoryInfoHash.contains(category), "setDefaultContext", "The category needs to be registered as a dlt logging category before it can be set as a default context"); @@ -136,11 +148,12 @@ DltContext *QDltRegistrationPrivate::context(const char *categoryName) return info.m_context; } -void QDltRegistrationPrivate::dltLogLevelChanged(char context_id[], uint8_t log_level, uint8_t trace_status) +const QLoggingCategory * QDltRegistrationPrivate::dltLogLevelChanged(char context_id[], uint8_t log_level, uint8_t trace_status) { - Q_Q(QDltRegistration); Q_UNUSED(trace_status) + const QLoggingCategory *changedCategory = nullptr; + for (auto it = m_categoryInfoHash.begin(); it != m_categoryInfoHash.end(); ++it) { if (it.value().m_ctxName != context_id) continue; @@ -183,10 +196,13 @@ void QDltRegistrationPrivate::dltLogLevelChanged(char context_id[], uint8_t log_ QLoggingCategory* category = it.value().m_category; if (category->isEnabled(type) != enabled) { category->setEnabled(type, enabled); - emit q->logLevelChanged(category); + changedCategory = category; } } + break; } + + return changedCategory; } DltLogLevelType QDltRegistrationPrivate::category2dltLevel(const QLoggingCategory *category) @@ -207,6 +223,20 @@ DltLogLevelType QDltRegistrationPrivate::category2dltLevel(const QLoggingCategor return logLevel; } +DltLogLevelType QDltRegistrationPrivate::severity2dltLevel(QtMsgType type) +{ + switch (type) { + case QtDebugMsg: return DLT_LOG_DEBUG; +#if QT_VERSION >= 0x050500 + case QtInfoMsg: return DLT_LOG_INFO; +#endif + case QtWarningMsg: return DLT_LOG_WARN; + case QtCriticalMsg: return DLT_LOG_ERROR; + case QtFatalMsg: return DLT_LOG_FATAL; + } + return DLT_LOG_OFF; +} + /*! \class QDltRegistration \inmodule QtGeniviExtras @@ -245,8 +275,10 @@ void QDltRegistration::registerApplication(const char *dltAppID, const char *dlt { Q_D(QDltRegistration); bool registerCategories = false; + QMutexLocker l(&d->m_mutex); + if (d->m_dltAppRegistered) { - unregisterApplication(); + d->unregisterApplication(); registerCategories = true; } @@ -277,6 +309,7 @@ void QDltRegistration::registerCategory(const QLoggingCategory *category, const Q_D(QDltRegistration); Q_ASSERT(category); Q_ASSERT(strlen(category->categoryName()) != 0); + QMutexLocker l(&d->m_mutex); d->registerCategory(category, new DltContext, dltCtxName, dltCtxDescription); } @@ -292,6 +325,7 @@ void QDltRegistration::registerCategory(const QLoggingCategory *category, const void QDltRegistration::setDefaultContext(const char *categoryName) { Q_D(QDltRegistration); + QMutexLocker l(&d->m_mutex); d->setDefaultCategory(QString::fromLatin1(categoryName)); } @@ -307,6 +341,7 @@ void QDltRegistration::setDefaultContext(const char *categoryName) void QDltRegistration::setRegisterContextOnFirstUseEnabled(bool enabled) { Q_D(QDltRegistration); + QMutexLocker l(&d->m_mutex); d->m_registerOnFirstUse = enabled; } @@ -325,6 +360,7 @@ void QDltRegistration::registerUnregisteredContexts() std::cout << "REGISTERING UNREGISTERED CONTEXTS" << std::endl; #endif Q_D(QDltRegistration); + QMutexLocker l(&d->m_mutex); if (!d->m_dltAppRegistered) d->registerApplication(); for (auto it = d->m_categoryInfoHash.begin(); it != d->m_categoryInfoHash.end(); ++it) { @@ -341,10 +377,8 @@ void QDltRegistration::registerUnregisteredContexts() void QDltRegistration::unregisterApplication() { Q_D(QDltRegistration); - if (d->m_dltAppRegistered) - DLT_UNREGISTER_APP(); - - d->m_dltAppRegistered = false; + QMutexLocker l(&d->m_mutex); + d->unregisterApplication(); } /*! @@ -361,25 +395,20 @@ void QDltRegistration::unregisterApplication() qInstallMessageHandler(QDltRegistration::messageHandler); \endcode */ -void QDltRegistration::messageHandler(QtMsgType msgTypes, const QMessageLogContext &msgCtx, const QString &msg) +void QDltRegistration::messageHandler(QtMsgType msgType, const QMessageLogContext &msgCtx, const QString &msg) { + if (!globalDltRegistration()) + return; + + QMutexLocker l(&globalDltRegistration()->d_ptr->m_mutex); + DltContext *dltCtx = globalDltRegistration()->d_ptr->context(msgCtx.category); if (!dltCtx) return; - DltLogLevelType logLevel = DLT_LOG_OFF; - - switch (msgTypes) { - case QtDebugMsg: logLevel = DLT_LOG_DEBUG; break; -#if QT_VERSION >= 0x050500 - case QtInfoMsg: logLevel = DLT_LOG_INFO; break; -#endif - case QtWarningMsg: logLevel = DLT_LOG_WARN; break; - case QtCriticalMsg: logLevel = DLT_LOG_ERROR; break; - case QtFatalMsg: logLevel = DLT_LOG_FATAL; break; - } + DltLogLevelType logLevel = globalDltRegistration()->d_ptr->severity2dltLevel(msgType); - DLT_LOG(*dltCtx, logLevel, DLT_STRING(qPrintable(qFormatLogMessage(msgTypes, msgCtx, msg)))); + DLT_LOG(*dltCtx, logLevel, DLT_STRING(qPrintable(qFormatLogMessage(msgType, msgCtx, msg)))); } /*! |