1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include <QtCore/qglobal.h> |
41 | |
42 | #ifndef QLOGGING_H |
43 | #define QLOGGING_H |
44 | |
45 | #if 0 |
46 | // header is automatically included in qglobal.h |
47 | #pragma qt_no_master_include |
48 | #endif |
49 | |
50 | QT_BEGIN_NAMESPACE |
51 | |
52 | /* |
53 | Forward declarations only. |
54 | |
55 | In order to use the qDebug() stream, you must #include<QDebug> |
56 | */ |
57 | class QDebug; |
58 | class QNoDebug; |
59 | |
60 | enum QtMsgType { QtDebugMsg, QtWarningMsg, QtCriticalMsg, QtFatalMsg, QtInfoMsg, QtSystemMsg = QtCriticalMsg }; |
61 | |
62 | class QMessageLogContext |
63 | { |
64 | Q_DISABLE_COPY(QMessageLogContext) |
65 | public: |
66 | Q_DECL_CONSTEXPR QMessageLogContext() |
67 | : version(2), line(0), file(nullptr), function(nullptr), category(nullptr) {} |
68 | Q_DECL_CONSTEXPR QMessageLogContext(const char *fileName, int lineNumber, const char *functionName, const char *categoryName) |
69 | : version(2), line(lineNumber), file(fileName), function(functionName), category(categoryName) {} |
70 | |
71 | void copy(const QMessageLogContext &logContext); |
72 | |
73 | int version; |
74 | int line; |
75 | const char *file; |
76 | const char *function; |
77 | const char *category; |
78 | |
79 | private: |
80 | friend class QMessageLogger; |
81 | friend class QDebug; |
82 | }; |
83 | |
84 | class QLoggingCategory; |
85 | |
86 | class Q_CORE_EXPORT QMessageLogger |
87 | { |
88 | Q_DISABLE_COPY(QMessageLogger) |
89 | public: |
90 | Q_DECL_CONSTEXPR QMessageLogger() : context() {} |
91 | Q_DECL_CONSTEXPR QMessageLogger(const char *file, int line, const char *function) |
92 | : context(file, line, function, "default" ) {} |
93 | Q_DECL_CONSTEXPR QMessageLogger(const char *file, int line, const char *function, const char *category) |
94 | : context(file, line, function, category) {} |
95 | |
96 | void debug(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3); |
97 | void noDebug(const char *, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3) |
98 | {} |
99 | void info(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3); |
100 | Q_DECL_COLD_FUNCTION |
101 | void warning(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3); |
102 | Q_DECL_COLD_FUNCTION |
103 | void critical(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3); |
104 | |
105 | typedef const QLoggingCategory &(*CategoryFunction)(); |
106 | |
107 | void debug(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
108 | void debug(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
109 | void info(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
110 | void info(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
111 | Q_DECL_COLD_FUNCTION |
112 | void warning(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
113 | Q_DECL_COLD_FUNCTION |
114 | void warning(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
115 | Q_DECL_COLD_FUNCTION |
116 | void critical(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
117 | Q_DECL_COLD_FUNCTION |
118 | void critical(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); |
119 | |
120 | #ifndef Q_CC_MSVC |
121 | Q_NORETURN |
122 | #endif |
123 | Q_DECL_COLD_FUNCTION |
124 | void fatal(const char *msg, ...) const Q_DECL_NOTHROW Q_ATTRIBUTE_FORMAT_PRINTF(2, 3); |
125 | |
126 | #ifndef QT_NO_DEBUG_STREAM |
127 | QDebug debug() const; |
128 | QDebug debug(const QLoggingCategory &cat) const; |
129 | QDebug debug(CategoryFunction catFunc) const; |
130 | QDebug info() const; |
131 | QDebug info(const QLoggingCategory &cat) const; |
132 | QDebug info(CategoryFunction catFunc) const; |
133 | QDebug warning() const; |
134 | QDebug warning(const QLoggingCategory &cat) const; |
135 | QDebug warning(CategoryFunction catFunc) const; |
136 | QDebug critical() const; |
137 | QDebug critical(const QLoggingCategory &cat) const; |
138 | QDebug critical(CategoryFunction catFunc) const; |
139 | |
140 | QNoDebug noDebug() const Q_DECL_NOTHROW; |
141 | #endif // QT_NO_DEBUG_STREAM |
142 | |
143 | private: |
144 | QMessageLogContext context; |
145 | }; |
146 | |
147 | #if !defined(QT_MESSAGELOGCONTEXT) && !defined(QT_NO_MESSAGELOGCONTEXT) |
148 | # if defined(QT_NO_DEBUG) |
149 | # define QT_NO_MESSAGELOGCONTEXT |
150 | # else |
151 | # define QT_MESSAGELOGCONTEXT |
152 | # endif |
153 | #endif |
154 | |
155 | #ifdef QT_MESSAGELOGCONTEXT |
156 | #define QT_MESSAGELOG_FILE __FILE__ |
157 | #define QT_MESSAGELOG_LINE __LINE__ |
158 | #define QT_MESSAGELOG_FUNC Q_FUNC_INFO |
159 | #else |
160 | #define QT_MESSAGELOG_FILE nullptr |
161 | #define QT_MESSAGELOG_LINE 0 |
162 | #define QT_MESSAGELOG_FUNC nullptr |
163 | #endif |
164 | |
165 | #define qDebug QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).debug |
166 | #define qInfo QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).info |
167 | #define qWarning QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).warning |
168 | #define qCritical QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).critical |
169 | #define qFatal QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).fatal |
170 | |
171 | #define QT_NO_QDEBUG_MACRO while (false) QMessageLogger().noDebug |
172 | |
173 | #if defined(QT_NO_DEBUG_OUTPUT) |
174 | # undef qDebug |
175 | # define qDebug QT_NO_QDEBUG_MACRO |
176 | #endif |
177 | #if defined(QT_NO_INFO_OUTPUT) |
178 | # undef qInfo |
179 | # define qInfo QT_NO_QDEBUG_MACRO |
180 | #endif |
181 | #if defined(QT_NO_WARNING_OUTPUT) |
182 | # undef qWarning |
183 | # define qWarning QT_NO_QDEBUG_MACRO |
184 | #endif |
185 | |
186 | Q_CORE_EXPORT void qt_message_output(QtMsgType, const QMessageLogContext &context, |
187 | const QString &message); |
188 | |
189 | Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void qErrnoWarning(int code, const char *msg, ...); |
190 | Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void qErrnoWarning(const char *msg, ...); |
191 | |
192 | #if QT_DEPRECATED_SINCE(5, 0)// deprecated. Use qInstallMessageHandler instead! |
193 | typedef void (*QtMsgHandler)(QtMsgType, const char *); |
194 | Q_CORE_EXPORT QT_DEPRECATED QtMsgHandler qInstallMsgHandler(QtMsgHandler); |
195 | #endif |
196 | |
197 | typedef void (*QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QString &); |
198 | Q_CORE_EXPORT QtMessageHandler qInstallMessageHandler(QtMessageHandler); |
199 | |
200 | Q_CORE_EXPORT void qSetMessagePattern(const QString &messagePattern); |
201 | Q_CORE_EXPORT QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, |
202 | const QString &buf); |
203 | |
204 | QT_END_NAMESPACE |
205 | #endif // QLOGGING_H |
206 | |