Logging: introduce QInternalMessageLogContext to hold current context

QMessageLogContext is a primitive type that may be extended in the
future with more fields (it has been at version 2 since commit
6d166c88220ee09821b65fb2b711fa77a5312971, though that did not extend the
struct's size). This introduces a QInternalMessageLogContext which is
used in before all our calls to qt_message_output().

Currently there's no difference and no way to tell that the internal
version is used.

Change-Id: I01ec3c774d9943adb903fffd17b7d5abc0052207
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
This commit is contained in:
Thiago Macieira 2024-02-27 13:31:57 -08:00
parent 7aedcdefb8
commit d9f7104166
4 changed files with 50 additions and 43 deletions

View File

@ -382,9 +382,10 @@ static void qt_message(QtMsgType msgType, const QMessageLogContext &context, con
*/ */
void QMessageLogger::debug(const char *msg, ...) const void QMessageLogger::debug(const char *msg, ...) const
{ {
QInternalMessageLogContext ctxt(context);
va_list ap; va_list ap;
va_start(ap, msg); // use variable arg list va_start(ap, msg); // use variable arg list
qt_message(QtDebugMsg, context, msg, ap); qt_message(QtDebugMsg, ctxt, msg, ap);
va_end(ap); va_end(ap);
} }
@ -397,9 +398,10 @@ void QMessageLogger::debug(const char *msg, ...) const
*/ */
void QMessageLogger::info(const char *msg, ...) const void QMessageLogger::info(const char *msg, ...) const
{ {
QInternalMessageLogContext ctxt(context);
va_list ap; va_list ap;
va_start(ap, msg); // use variable arg list va_start(ap, msg); // use variable arg list
qt_message(QtInfoMsg, context, msg, ap); qt_message(QtInfoMsg, ctxt, msg, ap);
va_end(ap); va_end(ap);
} }
@ -429,9 +431,7 @@ void QMessageLogger::debug(const QLoggingCategory &cat, const char *msg, ...) co
if (!cat.isDebugEnabled()) if (!cat.isDebugEnabled())
return; return;
QMessageLogContext ctxt; QInternalMessageLogContext ctxt(context, cat());
ctxt.copyContextFrom(context);
ctxt.category = cat.categoryName();
va_list ap; va_list ap;
va_start(ap, msg); // use variable arg list va_start(ap, msg); // use variable arg list
@ -453,9 +453,7 @@ void QMessageLogger::debug(QMessageLogger::CategoryFunction catFunc,
if (!cat.isDebugEnabled()) if (!cat.isDebugEnabled())
return; return;
QMessageLogContext ctxt; QInternalMessageLogContext ctxt(context, cat());
ctxt.copyContextFrom(context);
ctxt.category = cat.categoryName();
va_list ap; va_list ap;
va_start(ap, msg); // use variable arg list va_start(ap, msg); // use variable arg list
@ -534,9 +532,7 @@ void QMessageLogger::info(const QLoggingCategory &cat, const char *msg, ...) con
if (!cat.isInfoEnabled()) if (!cat.isInfoEnabled())
return; return;
QMessageLogContext ctxt; QInternalMessageLogContext ctxt(context, cat());
ctxt.copyContextFrom(context);
ctxt.category = cat.categoryName();
va_list ap; va_list ap;
va_start(ap, msg); // use variable arg list va_start(ap, msg); // use variable arg list
@ -558,9 +554,7 @@ void QMessageLogger::info(QMessageLogger::CategoryFunction catFunc,
if (!cat.isInfoEnabled()) if (!cat.isInfoEnabled())
return; return;
QMessageLogContext ctxt; QInternalMessageLogContext ctxt(context, cat());
ctxt.copyContextFrom(context);
ctxt.category = cat.categoryName();
va_list ap; va_list ap;
va_start(ap, msg); // use variable arg list va_start(ap, msg); // use variable arg list
@ -624,9 +618,10 @@ QDebug QMessageLogger::info(QMessageLogger::CategoryFunction catFunc) const
*/ */
void QMessageLogger::warning(const char *msg, ...) const void QMessageLogger::warning(const char *msg, ...) const
{ {
QInternalMessageLogContext ctxt(context);
va_list ap; va_list ap;
va_start(ap, msg); // use variable arg list va_start(ap, msg); // use variable arg list
qt_message(QtWarningMsg, context, msg, ap); qt_message(QtWarningMsg, ctxt, msg, ap);
va_end(ap); va_end(ap);
} }
@ -642,9 +637,7 @@ void QMessageLogger::warning(const QLoggingCategory &cat, const char *msg, ...)
if (!cat.isWarningEnabled()) if (!cat.isWarningEnabled())
return; return;
QMessageLogContext ctxt; QInternalMessageLogContext ctxt(context, cat());
ctxt.copyContextFrom(context);
ctxt.category = cat.categoryName();
va_list ap; va_list ap;
va_start(ap, msg); // use variable arg list va_start(ap, msg); // use variable arg list
@ -666,9 +659,7 @@ void QMessageLogger::warning(QMessageLogger::CategoryFunction catFunc,
if (!cat.isWarningEnabled()) if (!cat.isWarningEnabled())
return; return;
QMessageLogContext ctxt; QInternalMessageLogContext ctxt(context, cat());
ctxt.copyContextFrom(context);
ctxt.category = cat.categoryName();
va_list ap; va_list ap;
va_start(ap, msg); // use variable arg list va_start(ap, msg); // use variable arg list
@ -729,9 +720,10 @@ QDebug QMessageLogger::warning(QMessageLogger::CategoryFunction catFunc) const
*/ */
void QMessageLogger::critical(const char *msg, ...) const void QMessageLogger::critical(const char *msg, ...) const
{ {
QInternalMessageLogContext ctxt(context);
va_list ap; va_list ap;
va_start(ap, msg); // use variable arg list va_start(ap, msg); // use variable arg list
qt_message(QtCriticalMsg, context, msg, ap); qt_message(QtCriticalMsg, ctxt, msg, ap);
va_end(ap); va_end(ap);
} }
@ -747,9 +739,7 @@ void QMessageLogger::critical(const QLoggingCategory &cat, const char *msg, ...)
if (!cat.isCriticalEnabled()) if (!cat.isCriticalEnabled())
return; return;
QMessageLogContext ctxt; QInternalMessageLogContext ctxt(context, cat());
ctxt.copyContextFrom(context);
ctxt.category = cat.categoryName();
va_list ap; va_list ap;
va_start(ap, msg); // use variable arg list va_start(ap, msg); // use variable arg list
@ -771,9 +761,7 @@ void QMessageLogger::critical(QMessageLogger::CategoryFunction catFunc,
if (!cat.isCriticalEnabled()) if (!cat.isCriticalEnabled())
return; return;
QMessageLogContext ctxt; QInternalMessageLogContext ctxt(context, cat());
ctxt.copyContextFrom(context);
ctxt.category = cat.categoryName();
va_list ap; va_list ap;
va_start(ap, msg); // use variable arg list va_start(ap, msg); // use variable arg list
@ -836,9 +824,7 @@ QDebug QMessageLogger::critical(QMessageLogger::CategoryFunction catFunc) const
*/ */
void QMessageLogger::fatal(const QLoggingCategory &cat, const char *msg, ...) const noexcept void QMessageLogger::fatal(const QLoggingCategory &cat, const char *msg, ...) const noexcept
{ {
QMessageLogContext ctxt; QInternalMessageLogContext ctxt(context, cat());
ctxt.copyContextFrom(context);
ctxt.category = cat.categoryName();
va_list ap; va_list ap;
va_start(ap, msg); // use variable arg list va_start(ap, msg); // use variable arg list
@ -862,9 +848,7 @@ void QMessageLogger::fatal(QMessageLogger::CategoryFunction catFunc,
{ {
const QLoggingCategory &cat = (*catFunc)(); const QLoggingCategory &cat = (*catFunc)();
QMessageLogContext ctxt; QInternalMessageLogContext ctxt(context, cat());
ctxt.copyContextFrom(context);
ctxt.category = cat.categoryName();
va_list ap; va_list ap;
va_start(ap, msg); // use variable arg list va_start(ap, msg); // use variable arg list
@ -884,9 +868,10 @@ void QMessageLogger::fatal(QMessageLogger::CategoryFunction catFunc,
*/ */
void QMessageLogger::fatal(const char *msg, ...) const noexcept void QMessageLogger::fatal(const char *msg, ...) const noexcept
{ {
QInternalMessageLogContext ctxt(context);
va_list ap; va_list ap;
va_start(ap, msg); // use variable arg list va_start(ap, msg); // use variable arg list
QT_TERMINATE_ON_EXCEPTION(qt_message(QtFatalMsg, context, msg, ap)); QT_TERMINATE_ON_EXCEPTION(qt_message(QtFatalMsg, ctxt, msg, ap));
va_end(ap); va_end(ap);
#ifndef Q_CC_MSVC_ONLY #ifndef Q_CC_MSVC_ONLY
@ -2057,9 +2042,10 @@ static void qt_message_fatal(QtMsgType, const QMessageLogContext &context, Strin
*/ */
void qt_message_output(QtMsgType msgType, const QMessageLogContext &context, const QString &message) void qt_message_output(QtMsgType msgType, const QMessageLogContext &context, const QString &message)
{ {
qt_message_print(msgType, context, message); QInternalMessageLogContext ctx(context);
qt_message_print(msgType, ctx, message);
if (isFatal(msgType)) if (isFatal(msgType))
qt_message_fatal(msgType, context, message); qt_message_fatal(msgType, ctx, message);
} }
void qErrnoWarning(const char *msg, ...) void qErrnoWarning(const char *msg, ...)
@ -2074,7 +2060,7 @@ void qErrnoWarning(const char *msg, ...)
va_end(ap); va_end(ap);
buf += " ("_L1 + error_string + u')'; buf += " ("_L1 + error_string + u')';
QMessageLogContext context; QInternalMessageLogContext context{QMessageLogContext()};
qt_message_output(QtWarningMsg, context, buf); qt_message_output(QtWarningMsg, context, buf);
} }
@ -2088,7 +2074,7 @@ void qErrnoWarning(int code, const char *msg, ...)
va_end(ap); va_end(ap);
buf += " ("_L1 + qt_error_string(code) + u')'; buf += " ("_L1 + qt_error_string(code) + u')';
QMessageLogContext context; QInternalMessageLogContext context{QMessageLogContext()};
qt_message_output(QtWarningMsg, context, buf); qt_message_output(QtWarningMsg, context, buf);
} }

View File

@ -37,15 +37,17 @@ enum QtMsgType {
#endif #endif
}; };
class QInternalMessageLogContext;
class QMessageLogContext class QMessageLogContext
{ {
Q_DISABLE_COPY(QMessageLogContext) Q_DISABLE_COPY(QMessageLogContext)
public: public:
static constexpr int CurrentVersion = 2;
constexpr QMessageLogContext() noexcept = default; constexpr QMessageLogContext() noexcept = default;
constexpr QMessageLogContext(const char *fileName, int lineNumber, const char *functionName, const char *categoryName) noexcept constexpr QMessageLogContext(const char *fileName, int lineNumber, const char *functionName, const char *categoryName) noexcept
: line(lineNumber), file(fileName), function(functionName), category(categoryName) {} : line(lineNumber), file(fileName), function(functionName), category(categoryName) {}
int version = 2; int version = CurrentVersion;
int line = 0; int line = 0;
const char *file = nullptr; const char *file = nullptr;
const char *function = nullptr; const char *function = nullptr;
@ -54,8 +56,8 @@ public:
private: private:
QMessageLogContext &copyContextFrom(const QMessageLogContext &logContext) noexcept; QMessageLogContext &copyContextFrom(const QMessageLogContext &logContext) noexcept;
friend class QInternalMessageLogContext;
friend class QMessageLogger; friend class QMessageLogger;
friend class QDebug;
}; };
class QLoggingCategory; class QLoggingCategory;

View File

@ -16,6 +16,8 @@
// //
#include <QtCore/private/qglobal_p.h> #include <QtCore/private/qglobal_p.h>
#include "qlogging.h"
#include "qloggingcategory.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -25,6 +27,22 @@ Q_CORE_EXPORT bool shouldLogToStderr();
} }
class QInternalMessageLogContext : public QMessageLogContext
{
public:
QInternalMessageLogContext(const QMessageLogContext &logContext)
{
copyContextFrom(logContext);
}
QInternalMessageLogContext(const QMessageLogContext &logContext,
const QLoggingCategory &categoryOverride)
: QInternalMessageLogContext(logContext)
{
category = categoryOverride.categoryName();
}
};
QT_END_NAMESPACE QT_END_NAMESPACE
#endif // QLOGGING_P_H #endif // QLOGGING_P_H

View File

@ -5,6 +5,7 @@
#include "qdebug.h" #include "qdebug.h"
#include "private/qdebug_p.h" #include "private/qdebug_p.h"
#include "qmetaobject.h" #include "qmetaobject.h"
#include <private/qlogging_p.h>
#include <private/qtextstream_p.h> #include <private/qtextstream_p.h>
#include <private/qtools_p.h> #include <private/qtools_p.h>
@ -150,15 +151,15 @@ QByteArray QtDebugUtils::toPrintable(const char *data, qint64 len, qsizetype max
Flushes any pending data to be written and destroys the debug stream. Flushes any pending data to be written and destroys the debug stream.
*/ */
// Has been defined in the header / inlined before Qt 5.4
QDebug::~QDebug() QDebug::~QDebug()
{ {
if (stream && !--stream->ref) { if (stream && !--stream->ref) {
if (stream->space && stream->buffer.endsWith(u' ')) if (stream->space && stream->buffer.endsWith(u' '))
stream->buffer.chop(1); stream->buffer.chop(1);
if (stream->message_output) { if (stream->message_output) {
QInternalMessageLogContext ctxt(stream->context);
qt_message_output(stream->type, qt_message_output(stream->type,
stream->context, ctxt,
stream->buffer); stream->buffer);
} }
delete stream; delete stream;