本页内容

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

这是一个对具有以下签名的函数指针的 typedef:

    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)

category 在function 中的line 处,为file 构建一个 QMessageLogger 以记录消息。

另请参阅 QLoggingCategory 。

QDebug QMessageLogger::critical() const

使用QDebug 流记录一条关键消息

另请参阅 qCritical() 和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() 和QDebug 。

QDebug QMessageLogger::debug(QMessageLogger::CategoryFunction catFunc) const

使用QDebug 流,将一条调试消息记录到catFunc 返回的类别中。

另请参阅 qCDebug() 和QDebug 。

QDebug QMessageLogger::debug(const QLoggingCategory &cat) const

使用QDebug 流将一条调试消息记录到cat 类别中。

另请参阅 qCDebug() 和QDebug 。

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 中引入。

另请参阅 qFatal() 和QDebug 。

[since 6.5] QDebug QMessageLogger::fatal(QMessageLogger::CategoryFunction catFunc) const

使用QDebug 流,将一条致命消息记录到catFunc 返回的类别中。

该函数在 Qt 6.5 中引入。

另请参阅 qCFatal() 和QDebug 。

[since 6.5] QDebug QMessageLogger::fatal(const QLoggingCategory &cat) const

使用QDebug 流将一条致命消息记录到cat 类别中。

该函数自 Qt 6.5 起引入。

另请参阅 qCFatal() 和QDebug 。

[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

为上下文cat 记录格式为msg 的致命错误消息。可使用由msg 指定的附加参数。

该函数于 Qt 6.5 中引入。

另请参阅 qCFatal()。

QDebug QMessageLogger::info() const

使用QDebug 流记录一条信息性消息。

另请参阅 qInfo() 和QDebug 。

QDebug QMessageLogger::info(QMessageLogger::CategoryFunction catFunc) const

使用QDebug 流,将一条信息性消息记录到由catFunc 返回的类别中。

另请参阅 qCInfo() 和QDebug 。

QDebug QMessageLogger::info(const QLoggingCategory &cat) const

使用QDebug 流将一条信息级消息记录到cat 类别中。

另请参阅 qCInfo() 和QDebug 。

void QMessageLogger::info(const char *msg, ...) const

记录格式为msg 的信息消息。还可以使用由msg 指定的附加参数。

另请参阅 qInfo()。

void QMessageLogger::info(QMessageLogger::CategoryFunction catFunc, const char *msg, ...) const

针对catFunc 返回的上下文,记录格式为msg 的 informational 消息。可以使用由msg 指定的其他参数。

另请参阅 qCInfo()。

void QMessageLogger::info(const QLoggingCategory &cat, const char *msg, ...) const

为上下文cat 记录格式为msg 的信息性消息。可以使用由msg 指定的附加参数。

另请参阅 qCInfo()。

QDebug QMessageLogger::warning() const

使用QDebug 流记录一条警告消息

另请参阅 qWarning() 和QDebug 。

QDebug QMessageLogger::warning(QMessageLogger::CategoryFunction catFunc) const

使用QDebug 流,将一条警告消息记录到catFunc 返回的类别中。

另请参阅 qCWarning() 和QDebug 。

QDebug QMessageLogger::warning(const QLoggingCategory &cat) const

使用QDebug 流将一条警告消息记录到cat 类别中。

另请参阅 qCWarning() 和QDebug 。

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

为上下文cat 记录格式为msg 的警告消息。可使用由msg 指定的附加参数。

另请参阅 qCWarning()。

© 2026 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.