本例主要介绍把QT调试信息,通过日志文件的形式输出,多用于Release版本。
在main.cpp修改如下代码:
//输出调试信息
void outputMsg(QtMsgType type, const char* m_cpMesg)
{
QString sLog;
QDateTime curDateTime = QDateTime::currentDateTime();
QString sSurDateTime = curDateTime.toString("yyyy-MM-dd hh:mm:ss.zzz");
switch(type)
{
case QtDebugMsg:
sLog = QString("%1 Debug: %2").arg(sSurDateTime).arg(m_cpMesg);
break;
case QtWarningMsg:
sLog = QString("%1 Warning: %2").arg(sSurDateTime).arg(m_cpMesg);
break;
case QtCriticalMsg:
sLog = QString("%1 Critical: %2").arg(sSurDateTime).arg(m_cpMesg);
break;
case QtFatalMsg:
sLog = QString("%1 Fatal: %2").arg(sSurDateTime).arg(m_cpMesg);
break;
default:
break;
}
QFile file("./debuglog.txt");
file.open(QIODevice::WriteOnly|QIODevice::Append);
QTextStream textstream(&file);
textstream << endl << sLog << endl;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qInstallMsgHandler(outputMsg);
MainWindow w;
w.show();
return a.exec();
}
当.exe程序运行时,会在把调试信息,存在文件debuglog.txt中