Qt log日志保存
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash
·
程序调试中需要保存qDebug的打印信息,首先想到的是Linux重定向输出到一个文件中,但是qdebug不行。google发现Qt已经自带了保存log的方法。
#include <QtGui/QApplication>
#include <QTextCodec>
#include <QDebug>
void customMessageHandler(QtMsgType type, const char *msg)
{
QString txt;
switch (type) {
//调试信息提示
case QtDebugMsg:
txt = QString("Debug: %1").arg(msg);
break;
//一般的warning提示
case QtWarningMsg:
txt = QString("Warning: %1").arg(msg);
break;
//严重错误提示
case QtCriticalMsg:
txt = QString("Critical: %1").arg(msg);
break;
//致命错误提示
case QtFatalMsg:
txt = QString("Fatal: %1").arg(msg);
abort();
}
QFile outFile1("debuglog1.txt");
QFile outFile2("debuglog2.txt");
outFile1.open(QIODevice::WriteOnly | QIODevice::Append);
if(outFile1.size() >= 1024*10 )
{
outFile1.close();
outFile2.remove();
QFile::copy("debuglog1.txt","debuglog2.txt");
outFile1.remove();
QFile outFile3("debuglog1.txt");
outFile3.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream ts(&outFile3);
ts << txt << endl;
}
else
{
QTextStream ts(&outFile1);
ts << txt << endl;
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qInstallMsgHandler(customMessageHandler);
MainServer mainWin;
mainWin.show();
return a.exec();
}
debuglog1.txt 如果大于10k了,就复制到debuglog2.txt,同时删除debuglog1.txt,重新开始写
PS:bool QFile::copy ( const QString & fileName, const QString & newName ) [static]
This is an overloaded function.
Copies the file fileName to newName. Returns true if successful; otherwise returns false.
If a file with the name newName already exists, copy() returns false (i.e., QFile will not overwrite it).
A beautiful web dashboard for Linux
最近提交(Master分支:4 个月前 )
186a802e
added ecosystem file for PM2 5 年前
5def40a3
Add host customization support for the NodeJS version 5 年前
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)