Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qlogging.cpp File Reference

(a9ced75ee065c60442a9da81487fd9eb8dcdc6af)

#include "qlogging.h"
#include "qlogging_p.h"
#include "qbytearray.h"
#include "qlist.h"
#include "qcoreapplication.h"
#include "private/qcoreapplication_p.h"
#include "qdatetime.h"
#include "qdebug.h"
#include "qgettid_p.h"
#include "private/qlocking_p.h"
#include "qloggingcategory.h"
#include "private/qloggingregistry_p.h"
#include "qmutex.h"
#include "qscopeguard.h"
#include "qstring.h"
#include "qtcore_tracepoints_p.h"
#include "qthread.h"
#include "qvarlengtharray.h"
#include <cstdlib>
#include <algorithm>
#include <chrono>
#include <memory>
#include <vector>
#include <stdio.h>
Include dependency graph for qlogging.cpp:

Go to the source code of this file.

Classes

struct  QMessagePattern
struct  QMessagePattern::BacktraceParams

Namespaces

namespace  QtPrivate

Macros

#define IF_TOKEN(LEVEL)
#define HANDLE_IF_TOKEN(LEVEL)

Functions

 Q_TRACE_POINT (qtcore, qt_message_print, int type, const char *category, const char *function, const char *file, int line, const QString &message)
template<typename String>
static void qt_maybe_message_fatal (QtMsgType, const QMessageLogContext &context, String &&message)
 \inmodule QtCore \title Qt Logging Types
static void qt_message_print (QtMsgType, const QMessageLogContext &context, const QString &message)
static void preformattedMessageHandler (QtMsgType type, const QMessageLogContext &context, const QString &formattedMessage)
static QString formatLogMessage (QtMsgType type, const QMessageLogContext &context, const QString &str)
static int checked_var_value (const char *varname)
static bool isFatalCountDown (const char *varname, QBasicAtomicInt &n)
static bool isFatal (QtMsgType msgType)
static bool qt_append_thread_name_to (QString &message)
static bool systemHasStderr ()
 Returns true if writing to stderr is supported.
static bool stderrHasConsoleAttached ()
 Returns true if writing to stderr will end up in a console/terminal visible to the user.
bool QtPrivate::shouldLogToStderr ()
 Returns true if logging stderr should be ensured.
static Q_NEVER_INLINE void qt_message (QtMsgType msgType, const QMessageLogContext &context, const char *msg, va_list ap)
static bool isDefaultCategory (const char *category)
Q_AUTOTEST_EXPORT QByteArray qCleanupFuncinfo (QByteArray info)
 Q_DECLARE_TYPEINFO (QMessagePattern::BacktraceParams, Q_RELOCATABLE_TYPE)
static void qDefaultMessageHandler (QtMsgType type, const QMessageLogContext &context, const QString &buf)
static void stderr_message_handler (QtMsgType type, const QMessageLogContext &context, const QString &formattedMessage)
static bool grabMessageHandler ()
static void ungrabMessageHandler ()
void qt_message_output (QtMsgType msgType, const QMessageLogContext &context, const QString &message)
void qErrnoWarning (const char *msg,...)
void qErrnoWarning (int code, const char *msg,...)
QtMessageHandler qInstallMessageHandler (QtMessageHandler h)
void qSetMessagePattern (const QString &pattern)
static void copyInternalContext (QInternalMessageLogContext *self, const QMessageLogContext &logContext) noexcept

Variables

static Q_CONSTINIT QBasicAtomicInt fatalCriticalsCount = Q_BASIC_ATOMIC_INITIALIZER(0)
static Q_CONSTINIT QBasicAtomicInt fatalWarningsCount = Q_BASIC_ATOMIC_INITIALIZER(0)
static const char categoryTokenC [] = "%{category}"
static const char typeTokenC [] = "%{type}"
static const char messageTokenC [] = "%{message}"
static const char fileTokenC [] = "%{file}"
static const char lineTokenC [] = "%{line}"
static const char functionTokenC [] = "%{function}"
static const char pidTokenC [] = "%{pid}"
static const char appnameTokenC [] = "%{appname}"
static const char threadidTokenC [] = "%{threadid}"
static const char threadnameTokenC [] = "%{threadname}"
static const char qthreadptrTokenC [] = "%{qthreadptr}"
static const char timeTokenC [] = "%{time"
static const char backtraceTokenC [] = "%{backtrace"
static const char ifCategoryTokenC [] = "%{if-category}"
static const char ifDebugTokenC [] = "%{if-debug}"
static const char ifInfoTokenC [] = "%{if-info}"
static const char ifWarningTokenC [] = "%{if-warning}"
static const char ifCriticalTokenC [] = "%{if-critical}"
static const char ifFatalTokenC [] = "%{if-fatal}"
static const char endifTokenC [] = "%{endif}"
static const char emptyTokenC [] = ""
static Q_CONSTINIT QBasicAtomicPointer< void(QtMsgType, const QMessageLogContext &, const QString &)> messageHandler = Q_BASIC_ATOMIC_INITIALIZER(nullptr)
static constexpr SystemMessageSink systemMessageSink
static Q_CONSTINIT thread_local bool msgHandlerGrabbed = false

Macro Definition Documentation

◆ HANDLE_IF_TOKEN

#define HANDLE_IF_TOKEN ( LEVEL)
Value:
} else if (token == if##LEVEL##TokenC) { \
skip = type != Qt##LEVEL##Msg;
Token token
Definition keywords.cpp:452
Definition qcompare.h:76
GLenum type

◆ IF_TOKEN

#define IF_TOKEN ( LEVEL)
Value:
else if (lexeme == QLatin1StringView(LEVEL)) { \
if (inIf) \
nestedIfError = true; \
tokens[i] = LEVEL; \
inIf = true; \
}

Function Documentation

◆ checked_var_value()

int checked_var_value ( const char * varname)
static

Definition at line 138 of file qlogging.cpp.

Referenced by isFatalCountDown().

Here is the caller graph for this function:

◆ copyInternalContext()

void copyInternalContext ( QInternalMessageLogContext * self,
const QMessageLogContext & logContext )
staticnoexcept

Definition at line 2394 of file qlogging.cpp.

◆ formatLogMessage()

QString formatLogMessage ( QtMsgType type,
const QMessageLogContext & context,
const QString & str )
static

Definition at line 1640 of file qlogging.cpp.

References appnameTokenC, QMessageLogContext::category, categoryTokenC, endifTokenC, QMessageLogContext::file, fileTokenC, functionTokenC, ifCategoryTokenC, isDefaultCategory(), lineTokenC, messageTokenC, QMessagePattern::mutex, pidTokenC, qthreadptrTokenC, threadidTokenC, threadnameTokenC, timeTokenC, and typeTokenC.

Here is the call graph for this function:

◆ grabMessageHandler()

bool grabMessageHandler ( )
static

Definition at line 2102 of file qlogging.cpp.

References msgHandlerGrabbed.

Referenced by qt_message_print().

Here is the caller graph for this function:

◆ isDefaultCategory()

bool isDefaultCategory ( const char * category)
static

Definition at line 954 of file qlogging.cpp.

Referenced by formatLogMessage(), and qt_message_print().

Here is the caller graph for this function:

◆ isFatal()

bool isFatal ( QtMsgType msgType)
static

Definition at line 186 of file qlogging.cpp.

References QtDebugMsg, and QtFatalMsg.

Referenced by qt_maybe_message_fatal().

Here is the caller graph for this function:

◆ isFatalCountDown()

bool isFatalCountDown ( const char * varname,
QBasicAtomicInt & n )
static

Definition at line 152 of file qlogging.cpp.

References checked_var_value().

Here is the call graph for this function:

◆ preformattedMessageHandler()

void preformattedMessageHandler ( QtMsgType type,
const QMessageLogContext & context,
const QString & formattedMessage )
static

Definition at line 2070 of file qlogging.cpp.

◆ Q_DECLARE_TYPEINFO()

◆ Q_TRACE_POINT()

Q_TRACE_POINT ( qtcore ,
qt_message_print ,
int type,
const char * category,
const char * function,
const char * file,
int line,
const QString & message )

◆ qCleanupFuncinfo()

Q_AUTOTEST_EXPORT QByteArray qCleanupFuncinfo ( QByteArray info)

Definition at line 962 of file qlogging.cpp.

◆ qDefaultMessageHandler()

void qDefaultMessageHandler ( QtMsgType type,
const QMessageLogContext & context,
const QString & message )
static

Definition at line 2085 of file qlogging.cpp.

References systemMessageSink.

Referenced by qInstallMessageHandler(), and qt_message_print().

Here is the caller graph for this function:

◆ qErrnoWarning() [1/2]

void qErrnoWarning ( const char * msg,
... )

Definition at line 2182 of file qlogging.cpp.

References QMessageLogContext::QMessageLogContext().

Here is the call graph for this function:

◆ qErrnoWarning() [2/2]

void qErrnoWarning ( int code,
const char * msg,
... )

Definition at line 2198 of file qlogging.cpp.

References QMessageLogContext::QMessageLogContext().

Here is the call graph for this function:

◆ qInstallMessageHandler()

QtMessageHandler qInstallMessageHandler ( QtMessageHandler h)

Definition at line 2377 of file qlogging.cpp.

References qDefaultMessageHandler().

Here is the call graph for this function:

◆ qSetMessagePattern()

void qSetMessagePattern ( const QString & pattern)

Definition at line 2386 of file qlogging.cpp.

References QMessagePattern::mutex.

◆ qt_append_thread_name_to()

bool qt_append_thread_name_to ( QString & message)
static

Definition at line 247 of file qlogging.cpp.

◆ qt_maybe_message_fatal()

template<typename String>
void qt_maybe_message_fatal ( QtMsgType msgType,
const QMessageLogContext & context,
String && message )
static

\inmodule QtCore \title Qt Logging Types

The <QtLogging> header file defines Qt logging types, functions and macros.

The <QtLogging> header file contains several types, functions and macros for logging.

The QtMsgType enum identifies the various messages that can be generated and sent to a Qt message handler; QtMessageHandler is a type definition for a pointer to a function with the signature {void myMessageHandler(QtMsgType, const QMessageLogContext &, const char *)}. qInstallMessageHandler() function can be used to install the given QtMessageHandler. QMessageLogContext class contains the line, file, and function the message was logged at. This information is created by the QMessageLogger class.

<QtLogging> also contains functions that generate messages from the given string argument: qDebug(), qInfo(), qWarning(), qCritical(), and qFatal(). These functions call the message handler with the given message.

Example:

if (!driver()->isOpen() || driver()->isOpenError()) {
qWarning("QSqlQuery::exec: database not open");
return false;
}
See also
QLoggingCategory

Definition at line 2140 of file qlogging.cpp.

References isFatal().

Here is the call graph for this function:

◆ qt_message()

Q_NEVER_INLINE void qt_message ( QtMsgType msgType,
const QMessageLogContext & context,
const char * msg,
va_list ap )
static

Definition at line 408 of file qlogging.cpp.

◆ qt_message_output()

void qt_message_output ( QtMsgType msgType,
const QMessageLogContext & context,
const QString & message )

Definition at line 2175 of file qlogging.cpp.

◆ qt_message_print()

void qt_message_print ( QtMsgType msgType,
const QMessageLogContext & context,
const QString & message )
static

Definition at line 2116 of file qlogging.cpp.

References QMessageLogContext::category, QMessageLogContext::file, QMessageLogContext::function, grabMessageHandler(), isDefaultCategory(), QMessageLogContext::line, qDefaultMessageHandler(), qt_message_print(), QtFatalMsg, stderr_message_handler(), and ungrabMessageHandler().

Referenced by qt_message_print().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stderr_message_handler()

void stderr_message_handler ( QtMsgType type,
const QMessageLogContext & context,
const QString & formattedMessage )
static

Definition at line 2027 of file qlogging.cpp.

Referenced by qt_message_print().

Here is the caller graph for this function:

◆ stderrHasConsoleAttached()

bool stderrHasConsoleAttached ( )
static

Returns true if writing to stderr will end up in a console/terminal visible to the user.

This is typically the case if the application was started from the command line.

If the application is started without a controlling console/terminal, but the parent process reads stderr and presents it to the user in some other way, the parent process may override the detection in this function by setting the QT_ASSUME_STDERR_HAS_CONSOLE environment variable to 1.

Note
Qt Creator does not implement a pseudo TTY, nor does it launch apps with the override environment variable set, but it will read stderr and print it to the user, so in effect this function cannot be used to conclude that stderr output will not be visible to the user, as even if this function returns false, the output might still end up visible to the user. For this reason, we don't guard the stderr output in the default message handler with stderrHasConsoleAttached().
See also
systemHasStderr()

Definition at line 287 of file qlogging.cpp.

References systemHasStderr().

Referenced by QtPrivate::shouldLogToStderr().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ systemHasStderr()

bool systemHasStderr ( )
static

Returns true if writing to stderr is supported.

See also
stderrHasConsoleAttached()

Definition at line 262 of file qlogging.cpp.

Referenced by stderrHasConsoleAttached().

Here is the caller graph for this function:

◆ ungrabMessageHandler()

void ungrabMessageHandler ( )
static

Definition at line 2111 of file qlogging.cpp.

References msgHandlerGrabbed.

Referenced by qt_message_print().

Here is the caller graph for this function:

Variable Documentation

◆ appnameTokenC

const char appnameTokenC = "%{appname}"
static

Definition at line 1135 of file qlogging.cpp.

Referenced by formatLogMessage(), and QMessagePattern::setPattern().

◆ backtraceTokenC

const char backtraceTokenC = "%{backtrace"
static

Definition at line 1140 of file qlogging.cpp.

Referenced by QMessagePattern::setPattern().

◆ categoryTokenC

const char categoryTokenC = "%{category}"
static

◆ emptyTokenC

const char emptyTokenC = ""
static

Definition at line 1148 of file qlogging.cpp.

◆ endifTokenC

const char endifTokenC = "%{endif}"
static

◆ fatalCriticalsCount

Q_CONSTINIT QBasicAtomicInt fatalCriticalsCount = Q_BASIC_ATOMIC_INITIALIZER(0)
static

Definition at line 184 of file qlogging.cpp.

◆ fatalWarningsCount

Q_CONSTINIT QBasicAtomicInt fatalWarningsCount = Q_BASIC_ATOMIC_INITIALIZER(0)
static

Definition at line 185 of file qlogging.cpp.

◆ fileTokenC

const char fileTokenC = "%{file}"
static

Definition at line 1131 of file qlogging.cpp.

Referenced by formatLogMessage(), and QMessagePattern::setPattern().

◆ functionTokenC

const char functionTokenC = "%{function}"
static

Definition at line 1133 of file qlogging.cpp.

Referenced by formatLogMessage(), and QMessagePattern::setPattern().

◆ ifCategoryTokenC

const char ifCategoryTokenC = "%{if-category}"
static

Definition at line 1141 of file qlogging.cpp.

Referenced by formatLogMessage(), and QMessagePattern::setDefaultPattern().

◆ ifCriticalTokenC

const char ifCriticalTokenC = "%{if-critical}"
static

Definition at line 1145 of file qlogging.cpp.

◆ ifDebugTokenC

const char ifDebugTokenC = "%{if-debug}"
static

Definition at line 1142 of file qlogging.cpp.

◆ ifFatalTokenC

const char ifFatalTokenC = "%{if-fatal}"
static

Definition at line 1146 of file qlogging.cpp.

◆ ifInfoTokenC

const char ifInfoTokenC = "%{if-info}"
static

Definition at line 1143 of file qlogging.cpp.

◆ ifWarningTokenC

const char ifWarningTokenC = "%{if-warning}"
static

Definition at line 1144 of file qlogging.cpp.

◆ lineTokenC

const char lineTokenC = "%{line}"
static

Definition at line 1132 of file qlogging.cpp.

Referenced by formatLogMessage(), and QMessagePattern::setPattern().

◆ messageHandler

Q_CONSTINIT QBasicAtomicPointer< void(QtMsgType, const QMessageLogContext &, const QString &)> messageHandler = Q_BASIC_ATOMIC_INITIALIZER(nullptr)
static

Definition at line 1763 of file qlogging.cpp.

◆ messageTokenC

const char messageTokenC = "%{message}"
static

◆ msgHandlerGrabbed

Q_CONSTINIT thread_local bool msgHandlerGrabbed = false
static

Definition at line 2100 of file qlogging.cpp.

Referenced by grabMessageHandler(), and ungrabMessageHandler().

◆ pidTokenC

const char pidTokenC = "%{pid}"
static

Definition at line 1134 of file qlogging.cpp.

Referenced by formatLogMessage(), and QMessagePattern::setPattern().

◆ qthreadptrTokenC

const char qthreadptrTokenC = "%{qthreadptr}"
static

Definition at line 1138 of file qlogging.cpp.

Referenced by formatLogMessage(), and QMessagePattern::setPattern().

◆ systemMessageSink

constexpr SystemMessageSink systemMessageSink
staticconstexpr
Initial value:
= {
nullptr
}

Definition at line 2050 of file qlogging.cpp.

Referenced by qDefaultMessageHandler().

◆ threadidTokenC

const char threadidTokenC = "%{threadid}"
static

Definition at line 1136 of file qlogging.cpp.

Referenced by formatLogMessage(), and QMessagePattern::setPattern().

◆ threadnameTokenC

const char threadnameTokenC = "%{threadname}"
static

Definition at line 1137 of file qlogging.cpp.

Referenced by formatLogMessage(), and QMessagePattern::setPattern().

◆ timeTokenC

const char timeTokenC = "%{time"
static

Definition at line 1139 of file qlogging.cpp.

Referenced by formatLogMessage(), and QMessagePattern::setPattern().

◆ typeTokenC

const char typeTokenC = "%{type}"
static

Definition at line 1129 of file qlogging.cpp.

Referenced by formatLogMessage(), and QMessagePattern::setPattern().