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

(f8d1b5842768c18607b3e176924bfc36a07a737f)

#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 is_fatal_count_down (QAtomicInt &n)
 
static bool isFatal (QtMsgType msgType)
 
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 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 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
[2]

◆ 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()

static int checked_var_value ( const char * varname)
static

Definition at line 136 of file qlogging.cpp.

Referenced by isFatal().

+ Here is the caller graph for this function:

◆ copyInternalContext()

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

Definition at line 2283 of file qlogging.cpp.

◆ formatLogMessage()

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

Definition at line 1558 of file qlogging.cpp.

◆ grabMessageHandler()

static bool grabMessageHandler ( )
static

Definition at line 1992 of file qlogging.cpp.

◆ is_fatal_count_down()

static bool is_fatal_count_down ( QAtomicInt & n)
static

Definition at line 150 of file qlogging.cpp.

◆ isDefaultCategory()

static bool isDefaultCategory ( const char * category)
static

Definition at line 879 of file qlogging.cpp.

◆ isFatal()

static bool isFatal ( QtMsgType msgType)
static

Definition at line 161 of file qlogging.cpp.

References checked_var_value(), QtCriticalMsg, and QtFatalMsg.

+ Here is the call graph for this function:

◆ preformattedMessageHandler()

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

Definition at line 1960 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 887 of file qlogging.cpp.

◆ qDefaultMessageHandler()

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

Definition at line 1975 of file qlogging.cpp.

◆ qErrnoWarning() [1/2]

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

Definition at line 2072 of file qlogging.cpp.

◆ qErrnoWarning() [2/2]

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

Definition at line 2088 of file qlogging.cpp.

◆ qInstallMessageHandler()

QtMessageHandler qInstallMessageHandler ( QtMessageHandler h)

Definition at line 2266 of file qlogging.cpp.

◆ qSetMessagePattern()

void qSetMessagePattern ( const QString & pattern)

Definition at line 2275 of file qlogging.cpp.

◆ qt_maybe_message_fatal()

template<typename String>
static 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 2030 of file qlogging.cpp.

◆ qt_message()

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

Definition at line 333 of file qlogging.cpp.

◆ qt_message_output()

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

Definition at line 2065 of file qlogging.cpp.

◆ qt_message_print()

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

Definition at line 2006 of file qlogging.cpp.

◆ stderr_message_handler()

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

Definition at line 1917 of file qlogging.cpp.

◆ stderrHasConsoleAttached()

static 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 212 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()

static bool systemHasStderr ( )
static

Returns true if writing to stderr is supported.

See also
stderrHasConsoleAttached()

Definition at line 187 of file qlogging.cpp.

Referenced by stderrHasConsoleAttached().

+ Here is the caller graph for this function:

◆ ungrabMessageHandler()

static void ungrabMessageHandler ( )
static

Definition at line 2001 of file qlogging.cpp.

Variable Documentation

◆ appnameTokenC

static const char appnameTokenC = "%{appname}"
static

Definition at line 1060 of file qlogging.cpp.

Referenced by QMessagePattern::setPattern().

◆ backtraceTokenC

static const char backtraceTokenC = "%{backtrace"
static

Definition at line 1064 of file qlogging.cpp.

Referenced by QMessagePattern::setPattern().

◆ categoryTokenC

static const char categoryTokenC = "%{category}"
static

◆ emptyTokenC

static const char emptyTokenC = ""
static

Definition at line 1072 of file qlogging.cpp.

◆ endifTokenC

static const char endifTokenC = "%{endif}"
static

◆ fileTokenC

static const char fileTokenC = "%{file}"
static

Definition at line 1056 of file qlogging.cpp.

Referenced by QMessagePattern::setPattern().

◆ functionTokenC

static const char functionTokenC = "%{function}"
static

Definition at line 1058 of file qlogging.cpp.

Referenced by QMessagePattern::setPattern().

◆ ifCategoryTokenC

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

Definition at line 1065 of file qlogging.cpp.

Referenced by QMessagePattern::setDefaultPattern().

◆ ifCriticalTokenC

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

Definition at line 1069 of file qlogging.cpp.

◆ ifDebugTokenC

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

Definition at line 1066 of file qlogging.cpp.

◆ ifFatalTokenC

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

Definition at line 1070 of file qlogging.cpp.

◆ ifInfoTokenC

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

Definition at line 1067 of file qlogging.cpp.

◆ ifWarningTokenC

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

Definition at line 1068 of file qlogging.cpp.

◆ lineTokenC

static const char lineTokenC = "%{line}"
static

Definition at line 1057 of file qlogging.cpp.

Referenced by QMessagePattern::setPattern().

◆ messageHandler

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

Definition at line 1678 of file qlogging.cpp.

◆ messageTokenC

static const char messageTokenC = "%{message}"
static

◆ msgHandlerGrabbed

static Q_CONSTINIT thread_local bool msgHandlerGrabbed = false
static

Definition at line 1990 of file qlogging.cpp.

◆ pidTokenC

static const char pidTokenC = "%{pid}"
static

Definition at line 1059 of file qlogging.cpp.

Referenced by QMessagePattern::setPattern().

◆ qthreadptrTokenC

static const char qthreadptrTokenC = "%{qthreadptr}"
static

Definition at line 1062 of file qlogging.cpp.

Referenced by QMessagePattern::setPattern().

◆ systemMessageSink

static constexpr SystemMessageSink systemMessageSink
staticconstexpr
Initial value:
= {
nullptr
}

Definition at line 1940 of file qlogging.cpp.

◆ threadidTokenC

static const char threadidTokenC = "%{threadid}"
static

Definition at line 1061 of file qlogging.cpp.

Referenced by QMessagePattern::setPattern().

◆ timeTokenC

static const char timeTokenC = "%{time"
static

Definition at line 1063 of file qlogging.cpp.

Referenced by QMessagePattern::setPattern().

◆ typeTokenC

static const char typeTokenC = "%{type}"
static

Definition at line 1054 of file qlogging.cpp.

Referenced by QMessagePattern::setPattern().