File indexing completed on 2026-07-21 08:36:48
0001
0002
0003
0004
0005 #ifndef QLOGGING_H
0006 #define QLOGGING_H
0007
0008 #include <QtCore/qtclasshelpermacros.h>
0009 #include <QtCore/qtconfigmacros.h>
0010 #include <QtCore/qtcoreexports.h>
0011 #include <QtCore/qcontainerfwd.h>
0012
0013 #if 0
0014
0015 #pragma qt_no_master_include
0016 #pragma qt_class(QtLogging)
0017 #endif
0018
0019 QT_BEGIN_NAMESPACE
0020
0021
0022
0023
0024
0025
0026 class QDebug;
0027 class QNoDebug;
0028
0029
0030 enum QtMsgType {
0031 QtDebugMsg,
0032 QT7_ONLY(QtInfoMsg,)
0033 QtWarningMsg,
0034 QtCriticalMsg,
0035 QtFatalMsg,
0036 QT6_ONLY(QtInfoMsg,)
0037 #if QT_DEPRECATED_SINCE(6, 7)
0038 QtSystemMsg Q_DECL_ENUMERATOR_DEPRECATED_X("Use QtCriticalMsg instead.") = QtCriticalMsg
0039 #endif
0040 };
0041
0042 class QInternalMessageLogContext;
0043 class QMessageLogContext
0044 {
0045 Q_DISABLE_COPY(QMessageLogContext)
0046 public:
0047 static constexpr int CurrentVersion = 2;
0048 constexpr QMessageLogContext() noexcept = default;
0049 constexpr QMessageLogContext(const char *fileName, int lineNumber, const char *functionName, const char *categoryName) noexcept
0050 : line(lineNumber), file(fileName), function(functionName), category(categoryName) {}
0051
0052 int version = CurrentVersion;
0053 int line = 0;
0054 const char *file = nullptr;
0055 const char *function = nullptr;
0056 const char *category = nullptr;
0057
0058 private:
0059 QMessageLogContext ©ContextFrom(const QMessageLogContext &logContext) noexcept;
0060
0061 friend class QInternalMessageLogContext;
0062 friend class QMessageLogger;
0063 };
0064
0065 class QLoggingCategory;
0066
0067 #if defined(Q_CC_MSVC_ONLY)
0068 # define QT_MESSAGE_LOGGER_NORETURN
0069 #else
0070 # define QT_MESSAGE_LOGGER_NORETURN Q_NORETURN
0071 #endif
0072
0073 class Q_CORE_EXPORT QMessageLogger
0074 {
0075 Q_DISABLE_COPY(QMessageLogger)
0076 public:
0077 constexpr QMessageLogger() : context() {}
0078 constexpr QMessageLogger(const char *file, int line, const char *function)
0079 : context(file, line, function, "default") {}
0080 constexpr QMessageLogger(const char *file, int line, const char *function, const char *category)
0081 : context(file, line, function, category) {}
0082
0083 void debug(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
0084 void noDebug(const char *, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3)
0085 {}
0086 void info(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
0087 Q_DECL_COLD_FUNCTION
0088 void warning(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
0089 Q_DECL_COLD_FUNCTION
0090 void critical(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
0091 QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION
0092 void fatal(const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
0093
0094 typedef const QLoggingCategory &(*CategoryFunction)();
0095
0096 void debug(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
0097 void debug(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
0098 void info(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
0099 void info(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
0100 Q_DECL_COLD_FUNCTION
0101 void warning(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
0102 Q_DECL_COLD_FUNCTION
0103 void warning(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
0104 Q_DECL_COLD_FUNCTION
0105 void critical(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
0106 Q_DECL_COLD_FUNCTION
0107 void critical(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
0108 QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION
0109 void fatal(const QLoggingCategory &cat, const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
0110 QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION
0111 void fatal(CategoryFunction catFunc, const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
0112
0113 #ifndef QT_NO_DEBUG_STREAM
0114 QDebug debug() const;
0115 QDebug debug(const QLoggingCategory &cat) const;
0116 QDebug debug(CategoryFunction catFunc) const;
0117 QDebug info() const;
0118 QDebug info(const QLoggingCategory &cat) const;
0119 QDebug info(CategoryFunction catFunc) const;
0120 Q_DECL_COLD_FUNCTION
0121 QDebug warning() const;
0122 Q_DECL_COLD_FUNCTION
0123 QDebug warning(const QLoggingCategory &cat) const;
0124 Q_DECL_COLD_FUNCTION
0125 QDebug warning(CategoryFunction catFunc) const;
0126 Q_DECL_COLD_FUNCTION
0127 QDebug critical() const;
0128 Q_DECL_COLD_FUNCTION
0129 QDebug critical(const QLoggingCategory &cat) const;
0130 Q_DECL_COLD_FUNCTION
0131 QDebug critical(CategoryFunction catFunc) const;
0132 Q_DECL_COLD_FUNCTION
0133 QDebug fatal() const;
0134 Q_DECL_COLD_FUNCTION
0135 QDebug fatal(const QLoggingCategory &cat) const;
0136 Q_DECL_COLD_FUNCTION
0137 QDebug fatal(CategoryFunction catFunc) const;
0138 #endif
0139
0140 # if QT_CORE_REMOVED_SINCE(6, 10)
0141 QNoDebug noDebug() const noexcept;
0142 # endif
0143 inline QNoDebug noDebug(...) const noexcept;
0144
0145 private:
0146 QMessageLogContext context;
0147 };
0148
0149 #undef QT_MESSAGE_LOGGER_NORETURN
0150
0151 #if !defined(QT_MESSAGELOGCONTEXT) && !defined(QT_NO_MESSAGELOGCONTEXT)
0152 # if defined(QT_NO_DEBUG)
0153 # define QT_NO_MESSAGELOGCONTEXT
0154 # else
0155 # define QT_MESSAGELOGCONTEXT
0156 # endif
0157 #endif
0158
0159 #ifdef QT_MESSAGELOGCONTEXT
0160 #define QT_MESSAGELOG_FILE static_cast<const char *>(__FILE__)
0161 #define QT_MESSAGELOG_LINE __LINE__
0162 #define QT_MESSAGELOG_FUNC static_cast<const char *>(Q_FUNC_INFO)
0163 #else
0164 #define QT_MESSAGELOG_FILE nullptr
0165 #define QT_MESSAGELOG_LINE 0
0166 #define QT_MESSAGELOG_FUNC nullptr
0167 #endif
0168
0169 #define qDebug QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).debug
0170 #define qInfo QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).info
0171 #define qWarning QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).warning
0172 #define qCritical QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).critical
0173 #define qFatal QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).fatal
0174
0175 Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void qErrnoWarning(int code, const char *msg, ...);
0176 Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void qErrnoWarning(const char *msg, ...);
0177
0178 #define QT_NO_QDEBUG_MACRO while (false) QMessageLogger().noDebug
0179
0180 #if defined(QT_NO_DEBUG_OUTPUT)
0181 # undef qDebug
0182 # define qDebug QT_NO_QDEBUG_MACRO
0183 #endif
0184 #if defined(QT_NO_INFO_OUTPUT)
0185 # undef qInfo
0186 # define qInfo QT_NO_QDEBUG_MACRO
0187 #endif
0188 #if defined(QT_NO_WARNING_OUTPUT)
0189 # undef qWarning
0190 # define qWarning QT_NO_QDEBUG_MACRO
0191 # define qErrnoWarning QT_NO_QDEBUG_MACRO
0192 #endif
0193
0194 Q_CORE_EXPORT void qt_message_output(QtMsgType, const QMessageLogContext &context,
0195 const QString &message);
0196
0197 typedef void (*QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QString &);
0198 Q_CORE_EXPORT QtMessageHandler qInstallMessageHandler(QtMessageHandler);
0199
0200 Q_CORE_EXPORT void qSetMessagePattern(const QString &messagePattern);
0201 Q_CORE_EXPORT QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context,
0202 const QString &buf);
0203
0204 Q_DECL_COLD_FUNCTION
0205 Q_CORE_EXPORT QString qt_error_string(int errorCode = -1);
0206
0207 QT_END_NAMESPACE
0208 #endif