QT日志系统

今天给大家介绍下QT里面的日志系统,直接上代码

 1 #include <QApplication>
 2 #include <iostream>
 3 #include "ThorHardwareUtil/PLC/plc.h"
 4 #include "ThorHardwareUtil/crc.h"
 5 #include "ThorHardwareUtil/Warehouse/baseshelf.h"
 6 #include <QDebug>
 7 #include "opendoorthread.h"
 8 #include "TestProject/testform.h"
 9 #include <QtMessageHandler>
10 #include <QFile>
11 #include <QDir>
12 #include <ThorUtil/confighelper.h>
13 #include <thread>
14 using namespace std;
15 //写入信息到文件
16 void WriteLog(QString str, QString LogType)
17 {
18 
19     QString fileFolder= qApp->applicationDirPath()+"/log/"+QDateTime::currentDateTime().toString("yyyy-MM-dd");
20     QDir dir(fileFolder);
21     if(!dir.exists())
22     {
23         dir.mkpath(fileFolder);
24     }
25     QString filePath=QString("%1/%2.log").arg(fileFolder).arg(LogType);
26     QString strToWrite="日志产生时间:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
27     strToWrite.append(QString("\r\n日志消息:%1").arg(str));
28     strToWrite.append("\r\n---------------------------------------------------------------------");
29     QFile file(filePath);
30     file.open(QIODevice::WriteOnly | QIODevice::Append);
31     QTextStream text_stream(&file);
32     text_stream <<strToWrite<<"\r\n";
33     file.flush();
34     file.close();
35     //打印到控制台
36     std::cout << strToWrite.toLocal8Bit().constData() << std::endl;
37 }
38 //注册函数
39 void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
40 {
41     QString txtMessage = "";
42     QString messageType = "";
43     switch (type)    {
44     case QtDebugMsg:    //调试信息提示
45         messageType = "Debug";
46         txtMessage = QString("Debug: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(QString::number(context.line)).arg(context.function);
47         break;
48     case QtInfoMsg:
49         messageType = "Info";
50         txtMessage = QString("Warning: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(QString::number(context.line)).arg(context.function);
51         break;
52     case QtWarningMsg:    //一般的warning提示
53         messageType = "Waring";
54         txtMessage = QString("Warning: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(QString::number(context.line)).arg(context.function);
55         break;
56     case QtCriticalMsg:    //严重错误提示
57         messageType = "Critical";
58         txtMessage = QString("Critical: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(QString::number(context.line)).arg(context.function);
59         PostErrorMessage(txtMessage,messageType);
60         break;
61     case QtFatalMsg:    //致命错误提示
62         messageType = "Fatal";
63         txtMessage = QString("Fatal: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(QString::number(context.line)).arg(context.function);
64         abort();
65     }
66     WriteLog(txtMessage, messageType);
67 }
68 int main(int argc, char *argv[])
69 {
70     QApplication a(argc, argv);
71     //注册监听级别函数
72     qInstallMessageHandler(myMessageOutput);
73     
74      qDebug() << "debug";
75 
76 
77     return a.exec();
78 }

这样大家使用qDebug,qInfo,qCritical等打印信息就会分类输出到日志文件里面,方便查看

注意:release模式下无法打印QMessageLogContext的具体内容,因为qt默认把release给取消了,需要在

pro文件里面加上DEFINES += QT_MESSAGELOGCONTEXT

CMAKELISTS中添加add_definitions("-DQT_MESSAGELOGCONTEXT")

posted @ 2018-08-21 11:54  徐沛东  阅读(5613)  评论(5编辑  收藏  举报