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.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QLOGGING_H
6#define QLOGGING_H
7
8#include <QtCore/qtclasshelpermacros.h>
9#include <QtCore/qtconfigmacros.h>
10#include <QtCore/qtcoreexports.h>
11#include <QtCore/qcontainerfwd.h>
12
13#if 0
14// header is automatically included in qglobal.h
15#pragma qt_no_master_include
16#pragma qt_class(QtLogging)
17#endif
18
19QT_BEGIN_NAMESPACE
20
21/*
22 Forward declarations only.
23
24 In order to use the qDebug() stream, you must #include<QDebug>
25*/
26class QDebug;
27class QNoDebug;
28
29
32 QT7_ONLY(QtInfoMsg,)
36 QT6_ONLY(QtInfoMsg,)
37#if QT_DEPRECATED_SINCE(6, 7)
38 QtSystemMsg Q_DECL_ENUMERATOR_DEPRECATED_X("Use QtCriticalMsg instead.") = QtCriticalMsg
39#endif
40};
41
44{
46public:
47 static constexpr int CurrentVersion = 2;
48 constexpr QMessageLogContext() noexcept = default;
49 constexpr QMessageLogContext(const char *fileName, int lineNumber, const char *functionName, const char *categoryName) noexcept
50 : line(lineNumber), file(fileName), function(functionName), category(categoryName) {}
51
53 int line = 0;
54 const char *file = nullptr;
55 const char *function = nullptr;
56 const char *category = nullptr;
57
58private:
59 QMessageLogContext &copyContextFrom(const QMessageLogContext &logContext) noexcept;
60
62 friend class QMessageLogger;
63};
64
65class QLoggingCategory;
66
67#if defined(Q_CC_MSVC_ONLY)
68# define QT_MESSAGE_LOGGER_NORETURN
69#else
70# define QT_MESSAGE_LOGGER_NORETURN Q_NORETURN
71#endif
72
73class Q_CORE_EXPORT QMessageLogger
74{
75 Q_DISABLE_COPY(QMessageLogger)
76public:
77 constexpr QMessageLogger() : context() {}
78 constexpr QMessageLogger(const char *file, int line, const char *function)
79 : context(file, line, function, "default") {}
80 constexpr QMessageLogger(const char *file, int line, const char *function, const char *category)
81 : context(file, line, function, category) {}
82
83 void debug(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
84 void noDebug(const char *, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3)
85 {}
86 void info(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
87 Q_DECL_COLD_FUNCTION
88 void warning(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
89 Q_DECL_COLD_FUNCTION
90 void critical(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
91 QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION
92 void fatal(const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
93
94 typedef const QLoggingCategory &(*CategoryFunction)();
95
96 void debug(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
97 void debug(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
98 void info(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
99 void info(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
100 Q_DECL_COLD_FUNCTION
101 void warning(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
102 Q_DECL_COLD_FUNCTION
103 void warning(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
104 Q_DECL_COLD_FUNCTION
105 void critical(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
106 Q_DECL_COLD_FUNCTION
107 void critical(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
108 QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION
109 void fatal(const QLoggingCategory &cat, const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
110 QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION
111 void fatal(CategoryFunction catFunc, const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
112
113#ifndef QT_NO_DEBUG_STREAM
114 QDebug debug() const;
115 QDebug debug(const QLoggingCategory &cat) const;
116 QDebug debug(CategoryFunction catFunc) const;
117 QDebug info() const;
118 QDebug info(const QLoggingCategory &cat) const;
119 QDebug info(CategoryFunction catFunc) const;
121 QDebug warning() const;
123 QDebug warning(const QLoggingCategory &cat) const;
125 QDebug warning(CategoryFunction catFunc) const;
127 QDebug critical() const;
129 QDebug critical(const QLoggingCategory &cat) const;
131 QDebug critical(CategoryFunction catFunc) const;
133 QDebug fatal() const;
135 QDebug fatal(const QLoggingCategory &cat) const;
137 QDebug fatal(CategoryFunction catFunc) const;
138#endif // QT_NO_DEBUG_STREAM
139
140# if QT_CORE_REMOVED_SINCE(6, 10)
141 QNoDebug noDebug() const noexcept;
142# endif
143 inline QNoDebug noDebug(...) const noexcept; // in qdebug.h
144
145private:
146 QMessageLogContext context;
147};
148
149#undef QT_MESSAGE_LOGGER_NORETURN
150
151#if !defined(QT_MESSAGELOGCONTEXT) && !defined(QT_NO_MESSAGELOGCONTEXT)
152# if defined(QT_NO_DEBUG)
153# define QT_NO_MESSAGELOGCONTEXT
154# else
155# define QT_MESSAGELOGCONTEXT
156# endif
157#endif
158
160 #define QT_MESSAGELOG_FILE static_cast<const char *>(__FILE__)
161 #define QT_MESSAGELOG_LINE __LINE__
162 #define QT_MESSAGELOG_FUNC static_cast<const char *>(Q_FUNC_INFO)
163#else
164 #define QT_MESSAGELOG_FILE nullptr
165 #define QT_MESSAGELOG_LINE 0
166 #define QT_MESSAGELOG_FUNC nullptr
167#endif
168
169#define qDebug QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).debug
170#define qInfo QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).info
171#define qWarning QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).warning
172#define qCritical QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).critical
173#define qFatal QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).fatal
174
175Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void qErrnoWarning(int code, const char *msg, ...);
176Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void qErrnoWarning(const char *msg, ...);
177
178#define QT_NO_QDEBUG_MACRO while (false) QMessageLogger().noDebug
179
180#if defined(QT_NO_DEBUG_OUTPUT)
181# undef qDebug
182# define qDebug QT_NO_QDEBUG_MACRO
183#endif
184#if defined(QT_NO_INFO_OUTPUT)
185# undef qInfo
186# define qInfo QT_NO_QDEBUG_MACRO
187#endif
188#if defined(QT_NO_WARNING_OUTPUT)
189# undef qWarning
190# define qWarning QT_NO_QDEBUG_MACRO
191# define qErrnoWarning QT_NO_QDEBUG_MACRO
192#endif
193
194Q_CORE_EXPORT void qt_message_output(QtMsgType, const QMessageLogContext &context,
195 const QString &message);
196
197typedef void (*QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QString &);
198Q_CORE_EXPORT QtMessageHandler qInstallMessageHandler(QtMessageHandler);
199
200Q_CORE_EXPORT void qSetMessagePattern(const QString &messagePattern);
201Q_CORE_EXPORT QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context,
202 const QString &buf);
203
204Q_DECL_COLD_FUNCTION
205Q_CORE_EXPORT QString qt_error_string(int errorCode = -1);
206
207QT_END_NAMESPACE
208#endif // QLOGGING_H
\inmodule QtCore
Definition qbytearray.h:58
int initFrom(const QMessageLogContext &logContext)
void populateBacktrace(int frameCount)
Definition qlist.h:81
\inmodule QtCore
Definition qlogging.h:44
constexpr QMessageLogContext(const char *fileName, int lineNumber, const char *functionName, const char *categoryName) noexcept
Definition qlogging.h:49
const char * category
Definition qlogging.h:56
constexpr QMessageLogContext() noexcept=default
const char * function
Definition qlogging.h:55
const char * file
Definition qlogging.h:54
\inmodule QtCore
Definition qlogging.h:74
QDebug debug(CategoryFunction catFunc) const
QDebug debug(const QLoggingCategory &cat) const
Logs a debug message into category cat using a QDebug stream.
Definition qlogging.cpp:526
void void void void Q_DECL_COLD_FUNCTION void Q_DECL_COLD_FUNCTION void Q_DECL_COLD_FUNCTION void Q_DECL_COLD_FUNCTION void QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION void QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION void QDebug debug() const
Logs a debug message using a QDebug stream.
Definition qlogging.cpp:512
QDebug info(const QLoggingCategory &cat) const
Logs an informational message into the category cat using a QDebug stream.
Definition qlogging.cpp:615
QDebug info() const
Logs an informational message using a QDebug stream.
Definition qlogging.cpp:601
QNoDebug noDebug(...) const noexcept
QDebug info(CategoryFunction catFunc) const
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:177
static const char ifCriticalTokenC[]
static bool grabMessageHandler()
void qt_message_output(QtMsgType msgType, const QMessageLogContext &context, const QString &message)
static const char emptyTokenC[]
static Q_NEVER_INLINE void qt_message(QtMsgType msgType, const QMessageLogContext &context, const char *msg, va_list ap)
Definition qlogging.cpp:409
static void preformattedMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &formattedMessage)
static bool systemHasStderr()
Returns true if writing to stderr is supported.
Definition qlogging.cpp:263
static const char endifTokenC[]
static bool isDefaultCategory(const char *category)
Definition qlogging.cpp:955
static const char messageTokenC[]
static bool qt_append_thread_name_to(QString &message)
Definition qlogging.cpp:248
static constexpr SystemMessageSink systemMessageSink
static void qt_maybe_message_fatal(QtMsgType, const QMessageLogContext &context, String &&message)
\inmodule QtCore \title Qt Logging Types
#define HANDLE_IF_TOKEN(LEVEL)
Q_DECLARE_TYPEINFO(QMessagePattern::BacktraceParams, Q_RELOCATABLE_TYPE)
static void qDefaultMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &buf)
static const char timeTokenC[]
static bool isFatalCountDown(const char *varname, QBasicAtomicInt &n)
Definition qlogging.cpp:153
void qErrnoWarning(int code, const char *msg,...)
static const char qthreadptrTokenC[]
static const char fileTokenC[]
static const char ifDebugTokenC[]
static const char ifFatalTokenC[]
static const char categoryTokenC[]
static void stderr_message_handler(QtMsgType type, const QMessageLogContext &context, const QString &formattedMessage)
static const char lineTokenC[]
static const char typeTokenC[]
static void ungrabMessageHandler()
static void copyInternalContext(QInternalMessageLogContext *self, const QMessageLogContext &logContext) noexcept
static const char ifCategoryTokenC[]
static int checked_var_value(const char *varname)
Definition qlogging.cpp:139
static const char threadnameTokenC[]
static const char pidTokenC[]
Q_TRACE_POINT(qtcore, qt_message_print, int type, const char *category, const char *function, const char *file, int line, const QString &message)
static const char threadidTokenC[]
static QString formatLogMessage(QtMsgType type, const QMessageLogContext &context, const QString &str)
static Q_CONSTINIT bool msgHandlerGrabbed
static const char backtraceTokenC[]
void qErrnoWarning(const char *msg,...)
static const char functionTokenC[]
#define IF_TOKEN(LEVEL)
static const char ifWarningTokenC[]
static const char appnameTokenC[]
static bool isFatal(QtMsgType msgType)
Definition qlogging.cpp:187
static const char ifInfoTokenC[]
QtMessageHandler qInstallMessageHandler(QtMessageHandler h)
static void qt_message_print(QtMsgType, const QMessageLogContext &context, const QString &message)
static bool stderrHasConsoleAttached()
Returns true if writing to stderr will end up in a console/terminal visible to the user.
Definition qlogging.cpp:288
void qSetMessagePattern(const QString &pattern)
Combined button and popup list for selecting options.
bool shouldLogToStderr()
Returns true if logging stderr should be ensured.
Definition qlogging.cpp:341
#define __has_include(x)
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2582
#define QT_MESSAGELOG_FUNC
Definition qlogging.h:162
#define QT_MESSAGELOG_FILE
Definition qlogging.h:160
#define QT_MESSAGE_LOGGER_NORETURN
Definition qlogging.h:70
#define QT_MESSAGELOG_LINE
Definition qlogging.h:161
Q_CORE_EXPORT void qSetMessagePattern(const QString &messagePattern)
#define QT_MESSAGELOGCONTEXT
Definition qlogging.h:155
QtMsgType
Definition qlogging.h:30
@ QtCriticalMsg
Definition qlogging.h:34
@ QtFatalMsg
Definition qlogging.h:35
@ QtDebugMsg
Definition qlogging.h:31
Q_CORE_EXPORT void qt_message_output(QtMsgType, const QMessageLogContext &context, const QString &message)
void(* QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QString &)
Definition qlogging.h:197
QMutex QBasicMutex
Definition qmutex.h:360
void setPattern(const QString &pattern)
std::unique_ptr< std::unique_ptr< const char[]>[]> literals
std::chrono::steady_clock::time_point appStartTime
std::unique_ptr< const char *[]> tokens
QList< QString > timeArgs
static QBasicMutex mutex
void setDefaultPattern()