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
4#ifndef QLOGGING_H
5#define QLOGGING_H
6
7#include <QtCore/qtclasshelpermacros.h>
8#include <QtCore/qtconfigmacros.h>
9#include <QtCore/qtcoreexports.h>
10#include <QtCore/qcontainerfwd.h>
11
12#if 0
13// header is automatically included in qglobal.h
14#pragma qt_no_master_include
15#pragma qt_class(QtLogging)
16#endif
17
18QT_BEGIN_NAMESPACE
19
20/*
21 Forward declarations only.
22
23 In order to use the qDebug() stream, you must #include<QDebug>
24*/
25class QDebug;
26class QNoDebug;
27
28
31 QT7_ONLY(QtInfoMsg,)
35 QT6_ONLY(QtInfoMsg,)
36#if QT_DEPRECATED_SINCE(6, 7)
37 QtSystemMsg Q_DECL_ENUMERATOR_DEPRECATED_X("Use QtCriticalMsg instead.") = QtCriticalMsg
38#endif
39};
40
43{
45public:
46 static constexpr int CurrentVersion = 2;
47 constexpr QMessageLogContext() noexcept = default;
48 constexpr QMessageLogContext(const char *fileName, int lineNumber, const char *functionName, const char *categoryName) noexcept
49 : line(lineNumber), file(fileName), function(functionName), category(categoryName) {}
50
52 int line = 0;
53 const char *file = nullptr;
54 const char *function = nullptr;
55 const char *category = nullptr;
56
57private:
58 QMessageLogContext &copyContextFrom(const QMessageLogContext &logContext) noexcept;
59
61 friend class QMessageLogger;
62};
63
64class QLoggingCategory;
65
66#if defined(Q_CC_MSVC_ONLY)
67# define QT_MESSAGE_LOGGER_NORETURN
68#else
69# define QT_MESSAGE_LOGGER_NORETURN Q_NORETURN
70#endif
71
72class Q_CORE_EXPORT QMessageLogger
73{
74 Q_DISABLE_COPY(QMessageLogger)
75public:
76 constexpr QMessageLogger() : context() {}
77 constexpr QMessageLogger(const char *file, int line, const char *function)
78 : context(file, line, function, "default") {}
79 constexpr QMessageLogger(const char *file, int line, const char *function, const char *category)
80 : context(file, line, function, category) {}
81
82 void debug(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
83 void noDebug(const char *, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3)
84 {}
85 void info(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
86 Q_DECL_COLD_FUNCTION
87 void warning(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
88 Q_DECL_COLD_FUNCTION
89 void critical(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
90 QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION
91 void fatal(const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
92
93 typedef const QLoggingCategory &(*CategoryFunction)();
94
95 void debug(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
96 void debug(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
97 void info(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
98 void info(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
99 Q_DECL_COLD_FUNCTION
100 void warning(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
101 Q_DECL_COLD_FUNCTION
102 void warning(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
103 Q_DECL_COLD_FUNCTION
104 void critical(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
105 Q_DECL_COLD_FUNCTION
106 void critical(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
107 QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION
108 void fatal(const QLoggingCategory &cat, const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
109 QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION
110 void fatal(CategoryFunction catFunc, const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
111
112#ifndef QT_NO_DEBUG_STREAM
113 QDebug debug() const;
114 QDebug debug(const QLoggingCategory &cat) const;
115 QDebug debug(CategoryFunction catFunc) const;
116 QDebug info() const;
117 QDebug info(const QLoggingCategory &cat) const;
118 QDebug info(CategoryFunction catFunc) const;
120 QDebug warning() const;
122 QDebug warning(const QLoggingCategory &cat) const;
124 QDebug warning(CategoryFunction catFunc) const;
126 QDebug critical() const;
128 QDebug critical(const QLoggingCategory &cat) const;
130 QDebug critical(CategoryFunction catFunc) const;
132 QDebug fatal() const;
134 QDebug fatal(const QLoggingCategory &cat) const;
136 QDebug fatal(CategoryFunction catFunc) const;
137#endif // QT_NO_DEBUG_STREAM
138
139# if QT_CORE_REMOVED_SINCE(6, 10)
140 QNoDebug noDebug() const noexcept;
141# endif
142 inline QNoDebug noDebug(...) const noexcept; // in qdebug.h
143
144private:
145 QMessageLogContext context;
146};
147
148#undef QT_MESSAGE_LOGGER_NORETURN
149
150#if !defined(QT_MESSAGELOGCONTEXT) && !defined(QT_NO_MESSAGELOGCONTEXT)
151# if defined(QT_NO_DEBUG)
152# define QT_NO_MESSAGELOGCONTEXT
153# else
154# define QT_MESSAGELOGCONTEXT
155# endif
156#endif
157
159 #define QT_MESSAGELOG_FILE static_cast<const char *>(__FILE__)
160 #define QT_MESSAGELOG_LINE __LINE__
161 #define QT_MESSAGELOG_FUNC static_cast<const char *>(Q_FUNC_INFO)
162#else
163 #define QT_MESSAGELOG_FILE nullptr
164 #define QT_MESSAGELOG_LINE 0
165 #define QT_MESSAGELOG_FUNC nullptr
166#endif
167
168#define qDebug QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).debug
169#define qInfo QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).info
170#define qWarning QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).warning
171#define qCritical QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).critical
172#define qFatal QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).fatal
173
174Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void qErrnoWarning(int code, const char *msg, ...);
175Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void qErrnoWarning(const char *msg, ...);
176
177#define QT_NO_QDEBUG_MACRO while (false) QMessageLogger().noDebug
178
179#if defined(QT_NO_DEBUG_OUTPUT)
180# undef qDebug
181# define qDebug QT_NO_QDEBUG_MACRO
182#endif
183#if defined(QT_NO_INFO_OUTPUT)
184# undef qInfo
185# define qInfo QT_NO_QDEBUG_MACRO
186#endif
187#if defined(QT_NO_WARNING_OUTPUT)
188# undef qWarning
189# define qWarning QT_NO_QDEBUG_MACRO
190# define qErrnoWarning QT_NO_QDEBUG_MACRO
191#endif
192
193Q_CORE_EXPORT void qt_message_output(QtMsgType, const QMessageLogContext &context,
194 const QString &message);
195
196typedef void (*QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QString &);
197Q_CORE_EXPORT QtMessageHandler qInstallMessageHandler(QtMessageHandler);
198
199Q_CORE_EXPORT void qSetMessagePattern(const QString &messagePattern);
200Q_CORE_EXPORT QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context,
201 const QString &buf);
202
203Q_DECL_COLD_FUNCTION
204Q_CORE_EXPORT QString qt_error_string(int errorCode = -1);
205
206QT_END_NAMESPACE
207#endif // QLOGGING_H
\inmodule QtCore
Definition qbytearray.h:57
Definition qlist.h:80
\inmodule QtCore
Definition qlogging.h:43
constexpr QMessageLogContext(const char *fileName, int lineNumber, const char *functionName, const char *categoryName) noexcept
Definition qlogging.h:48
const char * category
Definition qlogging.h:55
constexpr QMessageLogContext() noexcept=default
const char * function
Definition qlogging.h:54
const char * file
Definition qlogging.h:53
\inmodule QtCore
Definition qlogging.h:73
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:450
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:436
QDebug info(const QLoggingCategory &cat) const
Logs an informational message into the category cat using a QDebug stream.
Definition qlogging.cpp:539
QDebug info() const
Logs an informational message using a QDebug stream.
Definition qlogging.cpp:525
QNoDebug noDebug(...) const noexcept
QDebug info(CategoryFunction catFunc) const
static Q_CONSTINIT thread_local bool msgHandlerGrabbed
static const char ifCriticalTokenC[]
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:333
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:187
static const char endifTokenC[]
static bool isDefaultCategory(const char *category)
Definition qlogging.cpp:879
static const char messageTokenC[]
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 const char timeTokenC[]
static bool is_fatal_count_down(QAtomicInt &n)
Definition qlogging.cpp:150
static const char qthreadptrTokenC[]
static const char fileTokenC[]
static const char ifDebugTokenC[]
static const char ifFatalTokenC[]
static const char categoryTokenC[]
static const char lineTokenC[]
static const char typeTokenC[]
static const char ifCategoryTokenC[]
static int checked_var_value(const char *varname)
Definition qlogging.cpp:136
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 const char backtraceTokenC[]
static const char functionTokenC[]
#define IF_TOKEN(LEVEL)
static const char ifWarningTokenC[]
static const char appnameTokenC[]
static bool isFatal(QtMsgType msgType)
Definition qlogging.cpp:161
static const char ifInfoTokenC[]
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:212
Combined button and popup list for selecting options.
bool shouldLogToStderr()
Returns true if logging stderr should be ensured.
Definition qlogging.cpp:265
#define __has_include(x)
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2462
#define QT_MESSAGELOG_FUNC
Definition qlogging.h:161
Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void qErrnoWarning(int code, const char *msg,...)
Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void qErrnoWarning(const char *msg,...)
#define QT_MESSAGELOG_FILE
Definition qlogging.h:159
#define QT_MESSAGE_LOGGER_NORETURN
Definition qlogging.h:69
#define QT_MESSAGELOG_LINE
Definition qlogging.h:160
Q_CORE_EXPORT void qSetMessagePattern(const QString &messagePattern)
#define QT_MESSAGELOGCONTEXT
Definition qlogging.h:154
QtMsgType
Definition qlogging.h:29
@ QtCriticalMsg
Definition qlogging.h:33
@ QtFatalMsg
Definition qlogging.h:34
@ QtDebugMsg
Definition qlogging.h:30
Q_CORE_EXPORT void qt_message_output(QtMsgType, const QMessageLogContext &context, const QString &message)
void(* QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QString &)
Definition qlogging.h:196
QMutex QBasicMutex
Definition qmutex.h:346
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()