qt的日志有四个级别:
qDebug: 调试信息
qWarning: 警告信息
qCritical: 严重错误
qFatal: 致命错误
可以通过<QtGlobal>下的
void qCritical ( const char * msg, ... );
void qDebug ( const char * msg, ... );
void qFatal ( const char * msg, ... );
void qWarning ( const char * msg, ... );
或include <QtDebug>时,采用更便捷输出
qDebug()<<"debug";
qWarning()<<"warning";
qCritical()<<"critical";
在程序调试中这样的line printf输出没问题,如果app被部署后需要将这些输出的日志输出到日志文件中,需通过
QtMsgHandler qInstallMsgHandler ( QtMsgHandler handler )设置输出,如果想取消输出可通过qInstallMsgHandler(0)设置
qt日志输出示例:
void logMsgHandler(QtMsgType type, const char *msg)
{
QString strLog;
switch (type) {
case QtDebugMsg:
strLog = QString(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ")+"[Debug] %1").arg(QObject::tr(msg));
break;
case QtWarningMsg:
strLog = QString(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ")+"[Warn] %1").arg(QObject::tr(msg));
break;
case QtCriticalMsg:
strLog = QString(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ")+"[Critical] %1").arg(QObject::tr(msg));
break;
case QtFatalMsg:
strLog = QString(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ")+"[Fatal] %1").arg(QObject::tr(msg));
// abort();
default:
break;
}
//更复杂的写入文件按实际需求设置
QFile outFile("debug.log");
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
/**< the max size of log.txt.*/
if(outFile.size()>10000)
{
outFile.close();
//清空旧日志
outFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Truncate);
outFile.close();
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
}
QTextStream ts(&outFile);
ts << strLog << endl;
};
#include <QDebug>
int main(int argc, char **argv)
{
qInstallMsgHandler(myMessageOutput);
QApplication app(argc, argv);
//打印日志到文件中
qDebug("debug");
qWarning("warning");
qCritical("critical");
qFatal("fatal");
qDebug()<<"debug_";
qWarning()<<"warning_";
qCritical()<<"critical_";
return app.exec();
}
qDebug: 调试信息
qWarning: 警告信息
qCritical: 严重错误
qFatal: 致命错误
可以通过<QtGlobal>下的
void qCritical ( const char * msg, ... );
void qDebug ( const char * msg, ... );
void qFatal ( const char * msg, ... );
void qWarning ( const char * msg, ... );
或include <QtDebug>时,采用更便捷输出
qDebug()<<"debug";
qWarning()<<"warning";
qCritical()<<"critical";
在程序调试中这样的line printf输出没问题,如果app被部署后需要将这些输出的日志输出到日志文件中,需通过
QtMsgHandler qInstallMsgHandler ( QtMsgHandler handler )设置输出,如果想取消输出可通过qInstallMsgHandler(0)设置
qt日志输出示例:
void logMsgHandler(QtMsgType type, const char *msg)
{
QString strLog;
switch (type) {
case QtDebugMsg:
strLog = QString(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ")+"[Debug] %1").arg(QObject::tr(msg));
break;
case QtWarningMsg:
strLog = QString(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ")+"[Warn] %1").arg(QObject::tr(msg));
break;
case QtCriticalMsg:
strLog = QString(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ")+"[Critical] %1").arg(QObject::tr(msg));
break;
case QtFatalMsg:
strLog = QString(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ")+"[Fatal] %1").arg(QObject::tr(msg));
// abort();
default:
break;
}
//更复杂的写入文件按实际需求设置
QFile outFile("debug.log");
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
/**< the max size of log.txt.*/
if(outFile.size()>10000)
{
outFile.close();
//清空旧日志
outFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Truncate);
outFile.close();
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
}
QTextStream ts(&outFile);
ts << strLog << endl;
};
#include <QDebug>
int main(int argc, char **argv)
{
qInstallMsgHandler(myMessageOutput);
QApplication app(argc, argv);
//打印日志到文件中
qDebug("debug");
qWarning("warning");
qCritical("critical");
qFatal("fatal");
qDebug()<<"debug_";
qWarning()<<"warning_";
qCritical()<<"critical_";
return app.exec();
}