QT 5.0 - 内置日志? [英] QT 5.0 - Built in Logging?

查看:820
本文介绍了QT 5.0 - 内置日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些关于Qt 5.0 Logging的研究,它似乎内置了日志类。我无法找到一个例子。我找到了我认为相关的类。

I was doing some research on Qt 5.0 Logging and it appears to have built in classes for logging. I'm having trouble finding an example. I have located the classes i believe are relevant here.

QMessageLogger

QMessageLogContext

我可以从文档中大致看到如何创建QMessageLogger对象,

I can see roughly how to create the QMessageLogger Object from the documentation, but how can I create a log file and append to it?

推荐答案

默认情况下使用qDebug(),qWarning()等将允许创建一个日志文件,您可以将信息记录到控制台。

By default using qDebug(), qWarning(), etc will allow you to log information out to the console.

#include <QtDebug>
qDebug() << "Hello world!";

QMessageLogger旨在利用特殊的C ++宏(例如函数,行,文件)

QMessageLogger is designed to leverage special C++ macros (e.g. function, line, file)

QMessageLogger(__FILE__, __LINE__, 0).debug() << "Hello world!";

在Qt5中,消息记录器在幕后使用,因为qDebug()是一个宏, QMessageLogger的一个实例。所以我只是使用常规的qDebug()。

In Qt5 the message logger is used behind the scenes since qDebug() is a macro that will eventually instantiate an instance of QMessageLogger. So I'd just go with using the regular qDebug().

QMessageLogContext包含我认为元数据,即文件,行号,等等qDebug()语句它被从中调用。通常,如果你定义自己的QtMessageHandler(参见qInstallMessageHandler()),你会关心日志上下文。

The QMessageLogContext contains what I'd consider as "meta-data", i.e. the file, line number, etc that the qDebug() statement it was called from. Normally you'd concern yourself with the log-context if you're defining your own QtMessageHandler (see qInstallMessageHandler()).

消息处理程序允许更多地控制日志记录机制 - 例如将日志记录信息发送到自定义日志记录服务器或甚至文件。

The message handler allows for more control of the logging mechanism - like sending logging information to a custom logging server or even to a file.

如Qt文档中所述,创建自定义消息处理程序很简单:

As provided in the Qt Documentation, creating a custom message handler is simple:

void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
  std::cout << msg.toStdString();
}

查看更好的示例和这里的展示

这篇关于QT 5.0 - 内置日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆