提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
QT日志使用
原文链接:https://blog.csdn.net/kissgoodbye2012/article/details/117304529
在程序编写过程中,调试功能是非常重要的,决定着编程的效率。
常用的调试方法是单步调试,但如果程序编译计算机和程序运行计算机不同且两个计算机地理距离较远时,单步调试就不可能了。
同时单步调试的方法的调试效率相对较低,且难以用于多线程的调试。
如果程序中存在BUG,在运行时报错,往往有些BUG很难复现,这样通过对日志文件的查看,就提供了一种找出程序错误的可能。
因此,掌握日志的用法非常必要,有助于我们提高编程的效率,提高代码的质量。
1 在main.cpp中定义
1.1 在main.cpp中包含头文件
#include <QDir>
#include <QFile>
#include <QMutex>
#include <QTextStream>
#include <QTime>
1.2 在main.cpp的main函数中使用如下函数注册
//注册MessageHandler
qInstallMessageHandler(outputMessage);
1.3 在main.cpp中定义函数
void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
static QMutex mutex;
mutex.lock();
QString text;
switch(type)
{
case QtDebugMsg:
text = QString("Debug:");
break;
case QtWarningMsg:
text = QString("Warning:");
break;
case QtCriticalMsg:
text = QString("Critical:");
break;
case QtFatalMsg:
text = QString("Fatal:");
}
QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
QString current_date = QString("(%1)").arg(current_date_time);
QString message = QString("%1 \r\n%2 %3 \r\n%4").arg(current_date).arg(text).arg(context_info).arg(msg);
//判断文件夹是否存在,不存在新建
QString aFile = QDir::currentPath() + "/LogFile";
QDir dir(aFile);
if(!dir.exists())
{
dir.mkdir(aFile);//只创建一级子目录,即必须保证上级目录存在
}
QString current_time = QDateTime::currentDateTime().toString("yyyyMMdd");
QFile file(aFile+"/log"+current_time+".txt");
file.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream text_stream(&file);
text_stream << message << "\r\n \r\n";
file.flush();
file.close();
mutex.unlock();
}
2 在任意位置使用
使用如下4种函数,可以打印不同的程序信息。
//方式一:
//打印日志到文件中
qDebug("This is a debug message");
qWarning("This is a warning message");
qCritical("This is a critical message");
qFatal("This is a fatal message");
//方式二
//首先,包含头文件
#include <QDebug>
//然后,像正常打印信息一样使用
QString LogInfo="这是一个测试消息!";
qDebug()<<LogInfo<<endl;
3 结果
在程序所在目录的LogFIle文件夹下,就是日志所在的路径。
日志的内容如下所示:包含日志信息的类型,发生日期和时间,错误发生的代码位置。
在QT中使用日志系统
当写好的软件发布出去后,用户遇到死机或一些其他的bug,我们该怎么追踪这些问题呢,这时候日志系统很好的帮助了我们。最近也是参照网络大牛的博客代码实现了log。下面看看代码吧。
void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
static QMutex mutex;
mutex.lock();
QString text;
switch(type)
{
case QtDebugMsg:
text = QString("Debug:");
break;
case QtWarningMsg:
text = QString("Warning:");
break;
case QtCriticalMsg:
text = QString("Critical:");
break;
case QtFatalMsg:
text = QString("Fatal:");
}
QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
QString current_date = QString("(%1)").arg(current_date_time);
QString message = QString("%1 %2 %3 %4").arg(text).arg(context_info).arg(msg).arg(current_date);
QFile file("log.txt");
file.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream text_stream(&file);
text_stream << message << "\r\n";
file.flush();
file.close();``
mutex.unlock();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qInstallMessageHandler(outputMessage);
MainWindow w;
w.show();
return a.exec();
}
//不用添加其他头文件,直接复制即可使用。