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

source code of qtbase/src/corelib/global/qlogging.h