QMessageLogger Class
QMessageLogger 类生成日志信息。更多
头文件: | #include <QMessageLogger> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
公共类型
公共函数
QMessageLogger() | |
QMessageLogger(const char *file, int line, const char *function) | |
QMessageLogger(const char *file, int line, const char *function, const char *category) | |
QDebug | critical() const |
QDebug | critical(QMessageLogger::CategoryFunction catFunc) const |
QDebug | critical(const QLoggingCategory &cat) const |
void | critical(const char *msg, ...) const |
void | critical(QMessageLogger::CategoryFunction catFunc, const char *msg, ...) const |
void | critical(const QLoggingCategory &cat, const char *msg, ...) const |
QDebug | debug() const |
QDebug | debug(QMessageLogger::CategoryFunction catFunc) const |
QDebug | debug(const QLoggingCategory &cat) const |
void | debug(const char *msg, ...) const |
void | debug(QMessageLogger::CategoryFunction catFunc, const char *msg, ...) const |
void | debug(const QLoggingCategory &cat, const char *msg, ...) const |
(since 6.5) QDebug | fatal() const |
(since 6.5) QDebug | fatal(QMessageLogger::CategoryFunction catFunc) const |
(since 6.5) QDebug | fatal(const QLoggingCategory &cat) const |
void | fatal(const char *msg, ...) const |
(since 6.5) void | fatal(QMessageLogger::CategoryFunction catFunc, const char *msg, ...) const |
(since 6.5) void | fatal(const QLoggingCategory &cat, const char *msg, ...) const |
QDebug | info() const |
QDebug | info(QMessageLogger::CategoryFunction catFunc) const |
QDebug | info(const QLoggingCategory &cat) const |
void | info(const char *msg, ...) const |
void | info(QMessageLogger::CategoryFunction catFunc, const char *msg, ...) const |
void | info(const QLoggingCategory &cat, const char *msg, ...) const |
QDebug | warning() const |
QDebug | warning(QMessageLogger::CategoryFunction catFunc) const |
QDebug | warning(const QLoggingCategory &cat) const |
void | warning(const char *msg, ...) const |
void | warning(QMessageLogger::CategoryFunction catFunc, const char *msg, ...) const |
void | warning(const QLoggingCategory &cat, const char *msg, ...) const |
详细说明
QMessageLogger 用于为 Qt 日志框架生成消息。通常通过qDebug(),qInfo(),qWarning(),qCritical, 或qFatal() 函数使用,这些函数实际上是宏:例如,qDebug() 扩展为 QMessageLogger(__FILE__, __LINE__,Q_FUNC_INFO).debug() 用于调试构建,而 QMessageLogger(0, 0, 0).debug() 用于发布构建。
直接使用的一个例子是转发来自脚本语言(如 QML)的错误:
void statusChanged(QQmlComponent::Status status) { if (status == QQmlComponent::Error) { for (const QQmlError &error: std::as_const(component->errors())) { const QByteArray file = error.url().toEncoded(); QMessageLogger(file.constData(), error.line(), 0).debug() << error.description(); } } }
另请参阅 QMessageLogContext,qDebug(),qInfo(),qWarning(),qCritical() 和qFatal().
成员类型文档
QMessageLogger::CategoryFunction
这是一个指向函数指针的类型定义,其签名如下:
const QLoggingCategory &category();
Q_DECLARE_LOGGING_CATEGORY
宏生成具有此签名的函数声明,Q_LOGGING_CATEGORY
生成其定义。
另请参见 QLoggingCategory 。
成员函数文档
[constexpr]
QMessageLogger::QMessageLogger()
构造默认 QMessageLogger。请参阅其他构造函数以指定上下文信息。
[constexpr]
QMessageLogger::QMessageLogger(const char *file, int line, const char *function)
构造一个 QMessageLogger,记录function 中line file 的日志信息。等价于 QMessageLogger(file, line, function, "default")
[constexpr]
QMessageLogger::QMessageLogger(const char *file, int line, const char *function, const char *category)
构造一个 QMessageLogger,记录function 中line file 的category 消息。
另请参阅 QLoggingCategory 。
QDebug QMessageLogger::critical() const
使用QDebug 流记录关键信息
QDebug QMessageLogger::critical(QMessageLogger::CategoryFunction catFunc) const
使用QDebug 流将关键信息记录到catFunc 返回的类别中。
另请参阅 qCCritical() 和QDebug 。
QDebug QMessageLogger::critical(const QLoggingCategory &cat) const
使用QDebug 流将关键信息记录到cat 类别中。
另请参阅 qCCritical() 和QDebug 。
void QMessageLogger::critical(const char *msg, ...) const
记录格式为msg 的关键信息。可使用msg 指定的附加参数。
另请参阅 qCritical() 。
void QMessageLogger::critical(QMessageLogger::CategoryFunction catFunc, const char *msg, ...) const
为catFunc 返回的上下文记录格式为msg 的关键信息。可使用msg 指定的附加参数。
另请参阅 qCCritical() 。
void QMessageLogger::critical(const QLoggingCategory &cat, const char *msg, ...) const
为上下文cat 记录格式为msg 的关键信息。可使用msg 指定的附加参数。
另请参阅 qCCritical() 。
QDebug QMessageLogger::debug() const
使用QDebug 流记录调试信息
QDebug QMessageLogger::debug(QMessageLogger::CategoryFunction catFunc) const
使用QDebug 流将调试信息记录到catFunc 返回的类别中。
QDebug QMessageLogger::debug(const QLoggingCategory &cat) const
使用QDebug 流将调试信息记录到cat 类别中。
void QMessageLogger::debug(const char *msg, ...) const
记录格式为msg 的调试信息。可使用msg 指定的其他参数。
另请参阅 qDebug() 。
void QMessageLogger::debug(QMessageLogger::CategoryFunction catFunc, const char *msg, ...) const
记录由catFunc 返回的上下文的调试信息,格式为msg 。可使用msg 指定的其他参数。
另请参阅 qCDebug() 。
void QMessageLogger::debug(const QLoggingCategory &cat, const char *msg, ...) const
记录在上下文cat 中以格式msg 指定的调试信息。可使用msg 指定的附加参数。
另请参阅 qCDebug() 。
[since 6.5]
QDebug QMessageLogger::fatal() const
使用QDebug 流记录一条致命消息。
该函数在 Qt 6.5 中引入。
[since 6.5]
QDebug QMessageLogger::fatal(QMessageLogger::CategoryFunction catFunc) const
使用QDebug 流将致命消息记录到catFunc 返回的类别中。
该函数在 Qt 6.5 中引入。
[since 6.5]
QDebug QMessageLogger::fatal(const QLoggingCategory &cat) const
使用QDebug 流将致命消息记录到类别cat 中。
该函数在 Qt 6.5 中引入。
[noexcept]
void QMessageLogger::fatal(const char *msg, ...) const
记录格式为msg 的致命信息。可使用msg 指定的其他参数。
另请参阅 qFatal() 。
[noexcept, since 6.5]
void QMessageLogger::fatal(QMessageLogger::CategoryFunction catFunc, const char *msg, ...) const
为catFunc 返回的上下文记录格式为msg 的致命信息。可使用msg 指定的附加参数。
此函数在 Qt 6.5 中引入。
另请参阅 qCFatal() 。
[noexcept, since 6.5]
void QMessageLogger::fatal(const QLoggingCategory &cat, const char *msg, ...) const
记录格式为msg 的致命信息,上下文cat 。可使用msg 指定的附加参数。
此函数在 Qt 6.5 中引入。
另请参阅 qCFatal() 。
QDebug QMessageLogger::info() const
使用QDebug 流记录一条信息消息。
QDebug QMessageLogger::info(QMessageLogger::CategoryFunction catFunc) const
使用QDebug 流将信息记录到catFunc 返回的类别中。
QDebug QMessageLogger::info(const QLoggingCategory &cat) const
使用QDebug 流将信息记录到类别cat 中。
void QMessageLogger::info(const char *msg, ...) const
记录格式为msg 的信息。可使用msg 指定的附加参数。
另请参阅 qInfo() 。
void QMessageLogger::info(QMessageLogger::CategoryFunction catFunc, const char *msg, ...) const
为catFunc 返回的上下文记录格式为msg 的信息。可使用msg 指定的附加参数。
另请参阅 qCInfo() 。
void QMessageLogger::info(const QLoggingCategory &cat, const char *msg, ...) const
记录一条信息消息,格式为msg ,上下文cat 。可使用msg 指定的附加参数。
另请参阅 qCInfo() 。
QDebug QMessageLogger::warning() const
使用QDebug 流记录警告信息
QDebug QMessageLogger::warning(QMessageLogger::CategoryFunction catFunc) const
使用QDebug 流将警告信息记录到catFunc 返回的类别中。
QDebug QMessageLogger::warning(const QLoggingCategory &cat) const
使用QDebug 流将警告信息记录到cat 类别中。
void QMessageLogger::warning(const char *msg, ...) const
记录格式为msg 的警告信息。可使用msg 指定的其他参数。
另请参阅 qWarning() 。
void QMessageLogger::warning(QMessageLogger::CategoryFunction catFunc, const char *msg, ...) const
针对catFunc 返回的上下文,记录格式为msg 的警告信息。可使用msg 指定的附加参数。
另请参阅 qCWarning() 。
void QMessageLogger::warning(const QLoggingCategory &cat, const char *msg, ...) const
记录格式为msg 、上下文为cat 的警告信息。可使用msg 指定的其他参数。
另请参阅 qCWarning() 。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.