- #include <QtDebug>
- #include <QFile>
- #include <QTextStream >
- void customMessageHandler(QtMsgType type, const char *msg)
- {
- QString txt;
- switch (type)
- {
- case QtDebugMsg: //调试信息提示
- txt = QString("Debug: %1").arg(msg);
- break;
- case QtWarningMsg: //一般的warning提示
- 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();
- default:
- break;
- }
- QFile outFile("Log.txt");
- outFile.open(QIODevice::WriteOnly | QIODevice::Append);
- QTextStream ts(&outFile);
- ts << txt << endl;
- }
- int main( int argc, char * argv[] )
- {
- QApplication app( argc, argv );
- //先注册自己的MsgHandler
- qInstallMsgHandler(customMessageHandler);
- //以后就可以像下面这样直接打日志到文件中,而且日志也会包含时间信息
- 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");
- return app.exec();
- }
实现Qt日志输出到文件
最新推荐文章于 2024-09-21 00:00:00 发布