日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

QT4保存调试日志

發(fā)布時(shí)間:2024/9/5 c/c++ 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QT4保存调试日志 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

windows下Qt程序發(fā)布后,所有調(diào)試日志都沒有了,有時(shí)候出錯(cuò)不容易找出,所以做了個(gè)根據(jù)命令行傳入debug后,把調(diào)試日志寫到文件中的功能:

#include <QtGui/QApplication> #include "mainwindow.h" #include <QFile> #include <QTextStream> #include <QDebug> #include <QDateTime>static QString logfilepath = "debuglog.txt";void customMessageHandler(QtMsgType type, const char *msg) {QString txt;switch (type) {//調(diào)試信息提示case QtDebugMsg:txt = QString("Debug: %1").arg(msg);break;//一般的warning提示case QtWarningMsg:txt = QString("Warning: %1").arg(msg);break;//嚴(yán)重錯(cuò)誤提示case QtCriticalMsg:txt = QString("Critical: %1").arg(msg);break;//致命錯(cuò)誤提示case QtFatalMsg:txt = QString("Fatal: %1").arg(msg);abort();}QDateTime now = QDateTime::currentDateTime();QString filepath = "debuglog_" + now.toString("yyyyMMddhhmmss") + ".txt";QFile outFile(logfilepath);outFile.open(QIODevice::WriteOnly | QIODevice::Append);QTextStream ts(&outFile);ts << txt << endl; }int main(int argc, char *argv[]) {QApplication a(argc, argv);// 獲取命令行參數(shù)QStringList ql = QCoreApplication::arguments ();// 遍歷命令行參數(shù),如果傳入debug 則把qDebug等輸出重定向到文件中foreach(QString tmp, ql){qDebug() << "parm: " << tmp << endl;if (tmp == "debug"){QDateTime now = QDateTime::currentDateTime();logfilepath = "debuglog_" + now.toString("yyyyMMddhhmmss") + ".txt";//先注冊(cè)自己的MsgHandleIrqInstallMsgHandler(customMessageHandler);}}//以后就可以像下面這樣直接打日志到文件中,而且日志也會(huì)包含時(shí)間信息qDebug("This is a debug message at thisisqt.com");qWarning("This is a warning message at thisisqt.com");qCritical("This is a critical message at thisisqt.com");//qFatal("This is a fatal message at thisisqt.com");MainWindow w;w.show();return a.exec(); } 參考:

http://www.cppblog.com/lauer3912/archive/2011/04/10/143870.html

總結(jié)

以上是生活随笔為你收集整理的QT4保存调试日志的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。