From cb3d136251e3b80b4eb676946d475fbbb50ba91e Mon Sep 17 00:00:00 2001 From: Kang Lin Date: Thu, 29 Sep 2022 12:36:36 +0800 Subject: [PATCH] Modify LogManager::qtLogger. Add Qt category paramter. QLoggingCategory can use adapters based on category. --- src/log4qt/helpers/patternformatter.cpp | 9 +-------- src/log4qt/logmanager.cpp | 14 ++++++++++---- src/log4qt/logmanager.h | 6 +++--- tests/log4qttest/log4qttest.cpp | 18 ++++++++++++++++-- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/log4qt/helpers/patternformatter.cpp b/src/log4qt/helpers/patternformatter.cpp index 0e91482b..6f3801fe 100644 --- a/src/log4qt/helpers/patternformatter.cpp +++ b/src/log4qt/helpers/patternformatter.cpp @@ -664,14 +664,7 @@ QString LoggepatternConverter::convert(const LoggingEvent &loggingEvent) const if (!loggingEvent.logger()) return QString(); QString name; - - if (loggingEvent.logger() == LogManager::instance()->qtLogger()) // is qt logger - if (loggingEvent.categoryName().isEmpty()) - name = LogManager::instance()->qtLogger()->name(); - else - name = loggingEvent.categoryName(); - else - name = loggingEvent.logger()->name(); + name = loggingEvent.logger()->name(); if (mPrecision <= 0 || (name.isEmpty())) return name; diff --git a/src/log4qt/logmanager.cpp b/src/log4qt/logmanager.cpp index 4d281d98..b86f7949 100644 --- a/src/log4qt/logmanager.cpp +++ b/src/log4qt/logmanager.cpp @@ -422,13 +422,18 @@ void LogManager::qtMessageHandler(QtMsgType type, const QMessageLogContext &cont default: level = Level::TRACE_INT; } - LoggingEvent loggingEvent = LoggingEvent(instance()->qtLogger(), + + QString categoryName = QStringLiteral("Qt"); + if(context.category) + categoryName += QStringLiteral("::") + context.category; + categoryName = OptionConverter::classNameJavaToCpp(categoryName); + Logger* logger = instance()->qtLogger(categoryName); + LoggingEvent loggingEvent = LoggingEvent(logger, level, message, MessageContext(context.file, context.line, context.function), - QStringLiteral("Qt ") % context.category); - - instance()->qtLogger()->log(loggingEvent); + categoryName); + logger->log(loggingEvent); // Qt fatal behaviour copied from global.cpp qt_message_output() @@ -440,6 +445,7 @@ void LogManager::qtMessageHandler(QtMsgType type, const QMessageLogContext &cont // } end } + #ifdef Q_OS_WIN static inline void convert_to_wchar_t_elided(wchar_t *d, size_t space, const char *s) Q_DECL_NOEXCEPT { diff --git a/src/log4qt/logmanager.h b/src/log4qt/logmanager.h index 9f3c00d7..373bae44 100644 --- a/src/log4qt/logmanager.h +++ b/src/log4qt/logmanager.h @@ -116,7 +116,7 @@ class LOG4QT_EXPORT LogManager * * \sa setHandleQtMessages() */ - static Logger *qtLogger(); + static Logger *qtLogger(const QString& category = QStringLiteral("Qt")); static Logger *rootLogger(); static QList loggers(); @@ -333,9 +333,9 @@ inline Logger *LogManager::logLogger() return logger(QStringLiteral("Log4Qt")); } -inline Logger *LogManager::qtLogger() +inline Logger *LogManager::qtLogger(const QString &category) { - return logger(QStringLiteral("Qt")); + return logger(category); } inline void LogManager::setHandleQtMessages(bool handleQtMessages) diff --git a/tests/log4qttest/log4qttest.cpp b/tests/log4qttest/log4qttest.cpp index 891ae0a6..5ec09716 100644 --- a/tests/log4qttest/log4qttest.cpp +++ b/tests/log4qttest/log4qttest.cpp @@ -280,7 +280,7 @@ void Log4QtTest::PatternFormatter_data() << relative_string + " [main] DEBUG Test::TestLog4Qt foo.cpp:100 - foo() NDC - This is the message" + eol << 0; QTest::newRow("TTCC conversion with file, line and method - Qt logger") - << LoggingEvent(LogManager::instance()->qtLogger(), + << LoggingEvent(LogManager::instance()->qtLogger(QStringLiteral("Qt::category")), Level(Level::DEBUG_INT), QStringLiteral("This is the message"), QStringLiteral("NDC"), @@ -291,7 +291,21 @@ void Log4QtTest::PatternFormatter_data() QStringLiteral("Qt category") ) << "%r [%t] %p %c %F:%L-%M %x - %m%n" - << relative_string + " [main] DEBUG Qt category foo.cpp:100-foo() NDC - This is the message" + eol + << relative_string + " [main] DEBUG Qt::category foo.cpp:100-foo() NDC - This is the message" + eol + << 0; + QTest::newRow("TTCC conversion with file, line and method - Qt logger java category name") + << LoggingEvent(LogManager::instance()->qtLogger(QStringLiteral("Qt.category")), + Level(Level::DEBUG_INT), + QStringLiteral("This is the message"), + QStringLiteral("NDC"), + properties, + QStringLiteral("main"), + relative_timestamp, + MessageContext("foo.cpp", 100, "foo()"), + QStringLiteral("Qt.category") + ) + << "%r [%t] %p %c %F:%L-%M %x - %m%n" + << relative_string + " [main] DEBUG Qt::category foo.cpp:100-foo() NDC - This is the message" + eol << 0; QTest::newRow("TTCC conversion with file, line and method - Qt logger no category") << LoggingEvent(LogManager::instance()->qtLogger(),