一、qInstallMessageHandler
1.1 说明
在软件开发维护过程中,日志是不可缺少的一部分,优秀的日志可以帮我们迅速的定位问题的所在
这篇文章我们来谈谈QT中日志的使用
QT中可以直接使用qInstallMessageHandler来重定义qdebug、qinfo这些消息类型,这样子就很方便于我们的代码的编写了,debug和release时,我们还可以做不同的区分等
首先来看一下官方说明
嗯,很长的一段英文,我们来把它翻译一下(有道出马一波)
1.2 官方样例
#include <qapplication.h>
#include <stdio.h>
#include <stdlib.h>
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
switch (type) {
case QtDebugMsg:
fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtInfoMsg:
fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtWarningMsg:
fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtCriticalMsg:
fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtFatalMsg:
fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
}
}
int main(int argc, char **argv)
{
qInstallMessageHandler(myMessageOutput);
QApplication app(argc, argv);
...
return app.exec();
}
二、DEMO使用
2.1 需求分析
- 日志需要能够写到 文件里+界面显示
- 日志需要包含但不限于 时间、级别、行数、函数、类、内容
2.2 新建Qlogger类
简单的实现以下函数
.h文件
#ifndef QLOGGER_H
#define QLOGGER_H
#include <qstring.h>
class QLogger
{
private:
QLogger();
QString m_msg;
public:
static QLogger *GetInstance();
static QLogger *m_Instance;
QString GetMsg();
void setMsg(const QString &msg);
};
#endif // QLOGGER_H
.cpp文件
#include "qlogger.h"
#include <QApplication>
#include <qdatetime.h>
#include <QTextStream>
#include <QFile>
#include <QDate>
QLogger* QLogger::m_Instance = NULL;
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
QDateTime current_date_time =QDateTime::currentDateTime();
QString currentTime =current_date_time.toString("yyyy.MM.dd hh:mm:ss.zzz ddd");
QString temp;
switch (type) {
case QtDebugMsg:
temp = "[" + currentTime +"]" + " [Debug]" + msg;
break;
case QtInfoMsg:
temp = "[" + currentTime +"]" + " [Info]" + msg;
break;
case QtWarningMsg:
temp = "[" + currentTime +"]" + " [Warning]" + msg;
break;
case QtCriticalMsg:
temp = "[" + currentTime +"]" + " [Critical]" + msg;
break;
case QtFatalMsg:
temp = "[" + currentTime +"]" + " [Fatal]" + msg;
break;
}
temp += " [Function]" + QString(context.function) + " :" + QString::number(context.line);
fprintf(stderr,temp.toLocal8Bit());
QLogger::GetInstance()->setMsg(temp);
QFile file(QDate::currentDate().toString()+"log.txt");
file.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream text_stream(&file);
text_stream << temp << "\r\n";
file.flush();
file.close();
}
QLogger::QLogger()
{
qInstallMessageHandler(myMessageOutput);
}
void QLogger::setMsg(const QString &msg)
{
m_msg = msg;
}
QLogger *QLogger::GetInstance()
{
if(m_Instance == NULL){
m_Instance = new QLogger();
}
return m_Instance;
}
QString QLogger::GetMsg()
{
return m_msg;
}
2.3 代码说明
-
最简单的单例模式
-
消息输出格式
简单的修改消息输出的格式,使其包涵 时间、级别、内容、函数、行数等
-
文本输出
按照时间输出日志txt文件,建议加锁
三、运行
首先在程序开始前,调用一次单例,使消息重映射
然后就可以使用qDebug()、qInfo()等Qt的函数来打日志了
运行后会生成此文件
日志格式效果如下;