Skip to content

Commit

Permalink
Modify LogManager::qtLogger. Add Qt category paramter. QLoggingCatego…
Browse files Browse the repository at this point in the history
…ry can use adapters based on category.
  • Loading branch information
KangLin committed Oct 1, 2022
1 parent 1dc0b05 commit cb3d136
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
9 changes: 1 addition & 8 deletions src/log4qt/helpers/patternformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 10 additions & 4 deletions src/log4qt/logmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
{
Expand Down
6 changes: 3 additions & 3 deletions src/log4qt/logmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Logger *> loggers();
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 16 additions & 2 deletions tests/log4qttest/log4qttest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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(),
Expand Down

0 comments on commit cb3d136

Please sign in to comment.