Skip to content

Commit 89a5771

Browse files
committed
Modify LogManager::qtLogger. Add Qt category paramter. QLoggingCategory can use adapters based on category.
1 parent 1dc0b05 commit 89a5771

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

src/log4qt/helpers/patternformatter.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -664,14 +664,7 @@ QString LoggepatternConverter::convert(const LoggingEvent &loggingEvent) const
664664
if (!loggingEvent.logger())
665665
return QString();
666666
QString name;
667-
668-
if (loggingEvent.logger() == LogManager::instance()->qtLogger()) // is qt logger
669-
if (loggingEvent.categoryName().isEmpty())
670-
name = LogManager::instance()->qtLogger()->name();
671-
else
672-
name = loggingEvent.categoryName();
673-
else
674-
name = loggingEvent.logger()->name();
667+
name = loggingEvent.logger()->name();
675668

676669
if (mPrecision <= 0 || (name.isEmpty()))
677670
return name;

src/log4qt/logmanager.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,19 @@ void LogManager::qtMessageHandler(QtMsgType type, const QMessageLogContext &cont
422422
default:
423423
level = Level::TRACE_INT;
424424
}
425-
LoggingEvent loggingEvent = LoggingEvent(instance()->qtLogger(),
425+
426+
QString categoryName = QStringLiteral("Qt");
427+
if(context.category)
428+
categoryName += QStringLiteral("::") + context.category;
429+
categoryName = OptionConverter::classNameJavaToCpp(categoryName);
430+
Logger* logger = instance()->qtLogger(categoryName);
431+
LoggingEvent loggingEvent = LoggingEvent(logger,
426432
level,
427433
message,
428434
MessageContext(context.file, context.line, context.function),
429-
QStringLiteral("Qt ") % context.category);
435+
categoryName);
430436

431-
instance()->qtLogger()->log(loggingEvent);
437+
instance()->qtLogger(categoryName)->log(loggingEvent);
432438

433439

434440
// Qt fatal behaviour copied from global.cpp qt_message_output()
@@ -440,6 +446,7 @@ void LogManager::qtMessageHandler(QtMsgType type, const QMessageLogContext &cont
440446
// } end
441447
}
442448

449+
443450
#ifdef Q_OS_WIN
444451
static inline void convert_to_wchar_t_elided(wchar_t *d, size_t space, const char *s) Q_DECL_NOEXCEPT
445452
{

src/log4qt/logmanager.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class LOG4QT_EXPORT LogManager
116116
*
117117
* \sa setHandleQtMessages()
118118
*/
119-
static Logger *qtLogger();
119+
static Logger *qtLogger(const QString& category = QStringLiteral("Qt"));
120120

121121
static Logger *rootLogger();
122122
static QList<Logger *> loggers();
@@ -333,9 +333,9 @@ inline Logger *LogManager::logLogger()
333333
return logger(QStringLiteral("Log4Qt"));
334334
}
335335

336-
inline Logger *LogManager::qtLogger()
336+
inline Logger *LogManager::qtLogger(const QString &category)
337337
{
338-
return logger(QStringLiteral("Qt"));
338+
return logger(category);
339339
}
340340

341341
inline void LogManager::setHandleQtMessages(bool handleQtMessages)

tests/log4qttest/log4qttest.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ void Log4QtTest::PatternFormatter_data()
280280
<< relative_string + " [main] DEBUG Test::TestLog4Qt foo.cpp:100 - foo() NDC - This is the message" + eol
281281
<< 0;
282282
QTest::newRow("TTCC conversion with file, line and method - Qt logger")
283-
<< LoggingEvent(LogManager::instance()->qtLogger(),
283+
<< LoggingEvent(LogManager::instance()->qtLogger(QStringLiteral("Qt::category")),
284284
Level(Level::DEBUG_INT),
285285
QStringLiteral("This is the message"),
286286
QStringLiteral("NDC"),
@@ -291,7 +291,21 @@ void Log4QtTest::PatternFormatter_data()
291291
QStringLiteral("Qt category")
292292
)
293293
<< "%r [%t] %p %c %F:%L-%M %x - %m%n"
294-
<< relative_string + " [main] DEBUG Qt category foo.cpp:100-foo() NDC - This is the message" + eol
294+
<< relative_string + " [main] DEBUG Qt::category foo.cpp:100-foo() NDC - This is the message" + eol
295+
<< 0;
296+
QTest::newRow("TTCC conversion with file, line and method - Qt logger java category name")
297+
<< LoggingEvent(LogManager::instance()->qtLogger(QStringLiteral("Qt.category")),
298+
Level(Level::DEBUG_INT),
299+
QStringLiteral("This is the message"),
300+
QStringLiteral("NDC"),
301+
properties,
302+
QStringLiteral("main"),
303+
relative_timestamp,
304+
MessageContext("foo.cpp", 100, "foo()"),
305+
QStringLiteral("Qt.category")
306+
)
307+
<< "%r [%t] %p %c %F:%L-%M %x - %m%n"
308+
<< relative_string + " [main] DEBUG Qt::category foo.cpp:100-foo() NDC - This is the message" + eol
295309
<< 0;
296310
QTest::newRow("TTCC conversion with file, line and method - Qt logger no category")
297311
<< LoggingEvent(LogManager::instance()->qtLogger(),

0 commit comments

Comments
 (0)