firemail
标题:
封装了一个QT的日志记录类
[打印本页]
作者:
hechengjin
时间:
2016-9-23 15:54
标题:
封装了一个QT的日志记录类
本帖最后由 hechengjin 于 2016-10-27 09:24 编辑
main.cpp
#include <QApplication>
#include <QTranslator>
#include <QMessageBox>
#include "mainwindow.h"
/*下面一部分可以作为独立的头文件*/
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QDateTime>
#define qDebug() (qDebug() << " " << QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz") << " " << __FILE__ << "(" << __LINE__ << ")" << endl << "\t")
#define qWarning() (qWarning() << " " << QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz") << " " << __FILE__ << "(" << __LINE__ << ")" << endl << "\t")
#define qCritical() (qCritical() << " " << QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz") << " " << __FILE__ << "(" << __LINE__ << ")" << endl << "\t")
#define qFatal() (qFatal() << " " << QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz") << " " << __FILE__ << "(" << __LINE__ << ")" << endl << "\t")
// 将qDebug的信息记入日志的函数
void C61850IED::log(QtMsgType type, const char *msg)
{
if (!s_logFile.isOpen())
{
QDir logDir;
if (!logDir.exists("d:/sl330/log"))
{
logDir.mkpath("d:/sl330/log");
}
if (!s_logFile.open(QIODevice::ReadWrite | QIODevice::Text))
return;
}
QTextStream out(&s_logFile);
if (!s_logPosFile.isOpen())
{
if (s_logPosFile.open(QIODevice::ReadWrite))
{
// 第一次打开的时候读取上次写到的位置
qint64 pos = 0;
s_logPosFile.seek(0);
qint64 iReadLen = s_logPosFile.read((char *)&pos, sizeof(pos));
if (iReadLen != sizeof(pos))
{
// 读取失败,就转到文件开头
}
// 转到上次写入的位置
out.seek(pos);
}
else
{
return;
}
}
out << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz") << " ";
switch (type)
{
case QtDebugMsg:
out << "qDebug" << endl << " " << msg << endl << endl;
break;
case QtWarningMsg:
out << "qWarning" << endl << " " << msg << endl << endl;
break;
case QtCriticalMsg:
out << "qCritical" << endl << " " << msg << endl << endl;
break;
case QtFatalMsg:
out << "qFatal" << endl << " " << msg << endl << endl;
abort();
}
// 日志文件大小暂限制为1M
if (out.pos() > 1024 * 1024)
{
s_logFile.resize(out.pos());
out.seek(0);
out << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz") << " ";
out << endl << "Log file wrapped >>>>>>>>>" << endl << endl;
}
// 将当前记录到的位置写到61850to103.log.pos文件中
qint64 pos = out.pos();
s_logPosFile.seek(0);
s_logPosFile.write((char *)&pos, sizeof(pos));
}
int main(int argc, char **argv)
{
qInstallMsgHandler(logMessage);
QApplication a(argc, argv);
QTranslator translator;
qWarning() << "Hello!\n";
if (!translator.load("chinese_s"))
{
QMessageBox::information(NULL, "Error", "Failed to load translation file.");
}
a.installTranslator(&translator);
MainWindow *mainwindow = new MainWindow();
mainwindow->show();
return a.exec();
}
在release版本下也能起作用,输出的日志test.log:
Warning: "2008.06.09 03:40:41.750" main.cpp ( 46 )
Hello!
Warning: "2008.06.09 03:41:47.437" main.cpp ( 46 )
Hello!
Warning: "2008.06.09 03:41:49.718" main.cpp ( 46 )
Hello!
程序运行了三次就记录了上面的三句。把错误信息重定向到日志文件中如果程序出现问题以后查找起来就方便多了。
http://blog.csdn.net/wang9834664/article/details/9167795
欢迎光临 firemail (http://firemail.wang:8088/)
Powered by Discuz! X3