本文使用重定向到文本文件,重定向到其他控件或者输出可自行修改txt显示或者输出的方式
#include <QCoreApplication>
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QTime>
void customMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
QString txt;
switch (type)
{
case QtDebugMsg:
txt = QString("Debug: %1").arg(msg);
break;
case QtWarningMsg:
txt = QString("Warning: %1").arg(msg);
break;
case QtInfoMsg:
txt = QString("Info: %1").arg(msg);
break;
case QtCriticalMsg:
txt = QString("Critical: %1").arg(msg);
break;
case QtFatalMsg:
txt = QString("Fatal: %1").arg(msg);
break;
}
QFile outFile("./log.txt");
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream ts(&outFile);
ts << QTime::currentTime().toString("hh:mm:ss.zzz ") << txt << endl;
}
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
qInstallMessageHandler((QtMessageHandler)customMessageHandler);
qDebug() << "test qdebug";
qInfo() << "test qinfo";
return a.exec();
}
调用完成后,查看文件夹文件