#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QTextStream>
void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
static QFile logFile("log.txt");
if (!logFile.isOpen())
logFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream ts(&logFile);
switch (type) {
case QtDebugMsg:
ts << "Debug: ";
break;
case QtInfoMsg:
ts << "Info: ";
break;
case QtWarningMsg:
ts << "Warning: ";
break;
case QtCriticalMsg:
ts << "Critical: ";
break;
case QtFatalMsg:
ts << "Fatal: ";
abort();
}
ts << context.file << "(" << context.line << "): " << msg << endl;
}
int main(int argc, char *argv[])
{
qInstallMessageHandler(myMessageHandler);
QCoreApplication a(argc, argv);
qDebug() << "This is a debug message.";
qInfo() << "This is an info message.";
qWarning() << "This is a warning message.";
qCritical() << "This is a critical message.";
return a.exec();
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » QT log日志
发表评论 取消回复