封装了一个QT的日志记录类
来源:互联网 发布:淘宝卖家感谢信怎么写 编辑:程序博客网 时间:2024/05/18 03:27
来源 :http://tomlau-2000.blog.sohu.com/115297793.html
main.cpp
#include <QApplication>
#include <QTranslator>
#include <QMessageBox>
#include "mainwindow.h"
#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")
#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");
}
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;
}
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))
{
// 读取失败,就转到文件开头
}
{
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.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;
}
{
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));
}
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();
}
{
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!
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!
程序运行了三次就记录了上面的三句。把错误信息重定向到日志文件中如果程序出现问题以后查找起来就方便多了。
- 封装了一个QT的日志记录类
- 封装了一个QT的日志记录类
- 一个简单的Qt日志类 使用方便
- 一个简易的C++日志记录类
- Qt类的封装
- qt 很好的封装了 webkit内核
- 一个日志记录帮助类
- Qt记录应用日志
- 封装了一个 Windows 的 命名管道的类
- 【Log日志】一个封装的日志打印库(显示类,方法,行数)--破晓觉醒
- 封装了一个简单的注册表操作类
- 一个封装了MD5,SHA1等加密算法的类
- 一个封装了的浏览文件夹类 CFolderDialog
- 封装了一个TP发送邮件的类
- 自己封装了一个socket下载文件的类
- 用swift封装了一个关于 AFNetworking的类
- 分享一个简单的Android日志文件记录类
- 一个可能是线程安全的日志记录类
- 做人做事,做架构师架构师能力模型解析
- 字符编码理解
- 动态规划-数塔路径之和最大值及路径输出问题
- 回去看看正则表达式的书
- android 基础知识一
- 封装了一个QT的日志记录类
- at91sam9260ek开发板上jffs2根文件系统制作步骤(转)
- 机房收费系统=三层+设计模式
- windows客户端连接到samba服务器(如何使用samba)
- 建立视图层次结构树,学习用
- 收录几篇JQueryMobile的文章备用
- AI 的会议总结(by南大周志华)
- Block教程系列
- 正则表达式,匹配,切割,替换。获取。网页爬虫