您的位置:首页 > 其它

qInstallMsgHandler实现日志输出

2012-10-09 14:38 197 查看
#include <QtDebug>
#include <QFile>
#include <QTextStream>

#define _TIME_ qPrintable (QTime::currentTime ().toString ("hh:mm:ss:zzz"))

void Log(QtMsgType type, const char* msg)
{
QString qstrText;
switch (type)
{
case QtDebugMsg:
qstrText = QString("%1: %2").arg(_TIME_, msg);
break;
case QtWarningMsg:
qstrText = QString("%1: %2").arg(_TIME_, msg);
break;
case QtCriticalMsg:
qstrText = QString("%1: %2").arg(_TIME_, msg);
break;
case QtFatalMsg:
qstrText = QString("%1: %2").arg(_TIME_, msg);
exit(0);
}
QFile out("log.txt");
out.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream ts(&out);
ts<<qstrText<<endl;
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

qInstallMsgHandler(Log);

qDebug("this is a debug message");
qWarning("this is a warning message");
qCritical("this is a critical message");
qFatal("this is a fatal message");

return a.exec();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: