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 | |
18 | QT_BEGIN_NAMESPACE |
19 | |
20 | /* |
21 | Forward declarations only. |
22 | |
23 | In order to use the qDebug() stream, you must #include<QDebug> |
24 | */ |
25 | class QDebug; |
26 | class QNoDebug; |
27 | |
28 | |
29 | enum QtMsgType { |
30 | QtDebugMsg, |
31 | QtWarningMsg, |
32 | QtCriticalMsg, |
33 | QtFatalMsg, |
34 | QtInfoMsg, |
35 | QtSystemMsg = QtCriticalMsg |
36 | }; |
37 | |
38 | class QMessageLogContext |
39 | { |
40 | Q_DISABLE_COPY(QMessageLogContext) |
41 | public: |
42 | constexpr QMessageLogContext() noexcept = default; |
43 | constexpr QMessageLogContext(const char *fileName, int lineNumber, const char *functionName, const char *categoryName) noexcept |
44 | : line(lineNumber), file(fileName), function(functionName), category(categoryName) {} |
45 | |
46 | int version = 2; |
47 | int line = 0; |
48 | const char *file = nullptr; |
49 | const char *function = nullptr; |
50 | const char *category = nullptr; |
51 | |
52 | private: |
53 | QMessageLogContext ©ContextFrom(const QMessageLogContext &logContext) noexcept; |
54 | |
55 | friend class QMessageLogger; |
56 | friend class QDebug; |
57 | }; |
58 | |
59 | class QLoggingCategory; |
60 | |
61 | #if defined(Q_CC_MSVC_ONLY) |
62 | # define QT_MESSAGE_LOGGER_NORETURN |
63 | #else |
64 | # define QT_MESSAGE_LOGGER_NORETURN Q_NORETURN |
65 | #endif |
66 | |
67 | class Q_CORE_EXPORT QMessageLogger |
68 | { |
69 | Q_DISABLE_COPY(QMessageLogger) |
70 | public: |
71 | constexpr QMessageLogger() : context() {} |
72 | constexpr QMessageLogger(const char *file, int line, const char *function) |
73 | : context(file, line, function, "default" ) {} |
74 | constexpr QMessageLogger(const char *file, int line, const char *function, const char *category) |
75 | : context(file, line, function, category) {} |
76 | |
77 | void debug(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3); |
78 | void noDebug(const char *, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3) |
79 | {} |
80 | void info(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3); |
81 | Q_DECL_COLD_FUNCTION |
82 | void warning(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3); |
83 | Q_DECL_COLD_FUNCTION |
84 | void critical(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3); |
85 | QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION |
86 | void fatal(const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(2, 3); |
87 | |
88 | typedef const QLoggingCategory &(*CategoryFunction)(); |
89 | |
90 | void debug(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
91 | void debug(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
92 | void info(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
93 | void info(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
94 | Q_DECL_COLD_FUNCTION |
95 | void warning(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
96 | Q_DECL_COLD_FUNCTION |
97 | void warning(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
98 | Q_DECL_COLD_FUNCTION |
99 | void critical(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
100 | Q_DECL_COLD_FUNCTION |
101 | void critical(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
102 | QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION |
103 | void fatal(const QLoggingCategory &cat, const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
104 | QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION |
105 | void fatal(CategoryFunction catFunc, const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
106 | |
107 | #ifndef QT_NO_DEBUG_STREAM |
108 | QDebug debug() const; |
109 | QDebug debug(const QLoggingCategory &cat) const; |
110 | QDebug debug(CategoryFunction catFunc) const; |
111 | QDebug info() const; |
112 | QDebug info(const QLoggingCategory &cat) const; |
113 | QDebug info(CategoryFunction catFunc) const; |
114 | Q_DECL_COLD_FUNCTION |
115 | QDebug warning() const; |
116 | Q_DECL_COLD_FUNCTION |
117 | QDebug warning(const QLoggingCategory &cat) const; |
118 | Q_DECL_COLD_FUNCTION |
119 | QDebug warning(CategoryFunction catFunc) const; |
120 | Q_DECL_COLD_FUNCTION |
121 | QDebug critical() const; |
122 | Q_DECL_COLD_FUNCTION |
123 | QDebug critical(const QLoggingCategory &cat) const; |
124 | Q_DECL_COLD_FUNCTION |
125 | QDebug critical(CategoryFunction catFunc) const; |
126 | Q_DECL_COLD_FUNCTION |
127 | QDebug fatal() const; |
128 | Q_DECL_COLD_FUNCTION |
129 | QDebug fatal(const QLoggingCategory &cat) const; |
130 | Q_DECL_COLD_FUNCTION |
131 | QDebug fatal(CategoryFunction catFunc) const; |
132 | |
133 | QNoDebug noDebug() const noexcept; |
134 | #endif // QT_NO_DEBUG_STREAM |
135 | |
136 | private: |
137 | QMessageLogContext context; |
138 | }; |
139 | |
140 | #undef QT_MESSAGE_LOGGER_NORETURN |
141 | |
142 | #if !defined(QT_MESSAGELOGCONTEXT) && !defined(QT_NO_MESSAGELOGCONTEXT) |
143 | # if defined(QT_NO_DEBUG) |
144 | # define QT_NO_MESSAGELOGCONTEXT |
145 | # else |
146 | # define QT_MESSAGELOGCONTEXT |
147 | # endif |
148 | #endif |
149 | |
150 | #ifdef QT_MESSAGELOGCONTEXT |
151 | #define QT_MESSAGELOG_FILE static_cast<const char *>(__FILE__) |
152 | #define QT_MESSAGELOG_LINE __LINE__ |
153 | #define QT_MESSAGELOG_FUNC static_cast<const char *>(Q_FUNC_INFO) |
154 | #else |
155 | #define QT_MESSAGELOG_FILE nullptr |
156 | #define QT_MESSAGELOG_LINE 0 |
157 | #define QT_MESSAGELOG_FUNC nullptr |
158 | #endif |
159 | |
160 | #define qDebug QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).debug |
161 | #define qInfo QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).info |
162 | #define qWarning QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).warning |
163 | #define qCritical QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).critical |
164 | #define qFatal QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).fatal |
165 | |
166 | #define QT_NO_QDEBUG_MACRO while (false) QMessageLogger().noDebug |
167 | |
168 | #if defined(QT_NO_DEBUG_OUTPUT) |
169 | # undef qDebug |
170 | # define qDebug QT_NO_QDEBUG_MACRO |
171 | #endif |
172 | #if defined(QT_NO_INFO_OUTPUT) |
173 | # undef qInfo |
174 | # define qInfo QT_NO_QDEBUG_MACRO |
175 | #endif |
176 | #if defined(QT_NO_WARNING_OUTPUT) |
177 | # undef qWarning |
178 | # define qWarning QT_NO_QDEBUG_MACRO |
179 | #endif |
180 | |
181 | Q_CORE_EXPORT void qt_message_output(QtMsgType, const QMessageLogContext &context, |
182 | const QString &message); |
183 | |
184 | Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void qErrnoWarning(int code, const char *msg, ...); |
185 | Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void qErrnoWarning(const char *msg, ...); |
186 | |
187 | typedef void (*QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QString &); |
188 | Q_CORE_EXPORT QtMessageHandler qInstallMessageHandler(QtMessageHandler); |
189 | |
190 | Q_CORE_EXPORT void qSetMessagePattern(const QString &messagePattern); |
191 | Q_CORE_EXPORT QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, |
192 | const QString &buf); |
193 | |
194 | Q_DECL_COLD_FUNCTION |
195 | Q_CORE_EXPORT QString qt_error_string(int errorCode = -1); |
196 | |
197 | QT_END_NAMESPACE |
198 | #endif // QLOGGING_H |
199 | |