qdir 类似工具_Qt5之 简单的日志记录工具类

qt5 自己做的一个读写 sqlite3 的数据工具,能打开sqlite3格式的数据库文件

地址:https://blog..net/HK_5788/article/details/80963547

完善前面的文章中提到的工具,最近在实战中遇到了不少问题,故增加日志记录功能,记录软件的一些信息,方便查找问题

Qt 5 , mingw

------------- 上代码-------------

类名 :

HLogHelper

头文件

#ifndef HLOGHELPER_H

#define HLOGHELPER_H

#include

#include

#include

#include

#include

// 日志记录类

class HLogHelper : public QObject

{

Q_OBJECT

public:

HLogHelper();

//---------------------------------------------------------------

// 设置文件名前缀,ABC_20180909_131415.log, 这里,参数就传递 ABC

// 若不传递参数,默认以日期的方式命名:20180909_131415.log

// 函数返回值: 0 - 创建文件成功,

// 1 - 打开文件失败

int HInit(QString strFilePre);

// 对象销毁时,关闭文件

// 返回值: 1 - 关闭失败,文件已经关闭

// 0 - 关闭成功

int HUnInit();

//---------------------------------------------------------------

// 时间 + 记录内容

int HLogTime(QString str ...);

// 记录内容

int HLog(QString str...);

private:

enum

{

// 日志文件大小

he_log_file_size_1024kb = 1024,

};

private:

QString m_FileLogName;

QFile m_File;

QTextStream m_LogTextStream;

QMutex m_FileLogMutex;

QDateTime m_DateTime;

};

#endif // HLOGHELPER_H

.cpp文件

#include "hloghelper.h"

#include

#include

#include

#include

#include

HLogHelper::HLogHelper()

{

m_FileLogName = QString("");

}

// 初始化创建文件并打开文件

int HLogHelper::HInit(QString strFilePre)

{

int len = strFilePre.length();

QString fileName("");

// 设置文件名

// 1、若strFilePre不为空

if (0 < len)

{

// 获取当前日期

QString date = m_DateTime.currentDateTime().toString("yyyy_MM_dd_hh_mm_ss_zzz");

fileName = strFilePre + QString("_") + date;

}

else

{

//

QString date = m_DateTime.currentDateTime().toString("yyyy_MM_dd_hh_mm_ss_zzz");

fileName = date;

}

m_FileLogName = fileName + QString("_.log");

// 2、打开文件

// 若当前exe所在目录下不存在 HLog文件夹,则创建

QString logPath = QApplication::applicationDirPath() + QString("/HLog/");

QDir dir(logPath);

if (false == dir.exists())

{

dir.mkpath(logPath);

}

// 构造文件

m_FileLogName = logPath + m_FileLogName;

m_File.setFileName(m_FileLogName);

bool openFlag = m_File.open(QIODevice::Text | QIODevice::Truncate | QIODevice::WriteOnly | QIODevice::Append);

if (false == openFlag)

{

return 1;

}

m_LogTextStream.setDevice(&m_File);

return 0;

}

// 关闭文件

int HLogHelper::HUnInit()

{

bool isExist = m_File.exists(m_FileLogName);

// 若不存在

if (false == isExist)

{

return 1;

}

// 文件存在,检查文件是否已经打开

bool hasOepned = m_File.isOpen();

// 文件打开了

if (true == hasOepned)

{

m_File.flush();

m_File.close();

}

return 0;

}

// 日志记录前带日期

int HLogHelper::HLogTime(QString str...)

{

// 获取当前日期

QString date = m_DateTime.currentDateTime().toString("yyyy_MM_dd hh_mm_ss_zzz:");

QByteArray ba = (date + str).toLocal8Bit();

char *pArr = ba.data();

va_list al;

va_start(al, pArr);

QString strResult = QString::vasprintf(pArr, al);

va_end(al);

m_LogTextStream << strResult << endl;

m_LogTextStream.flush();

return 0;

}

// 日志前不带日期

int HLogHelper::HLog(QString str...)

{

QByteArray ba = str.toLocal8Bit();

char *pArr = ba.data();

va_list al;

va_start(al, pArr);

QString strResult = QString::vasprintf(pArr, al);

va_end(al);

m_LogTextStream << strResult << endl;

m_LogTextStream.flush();

return 0;

}

说明:

1、使用方法,首先需要调用 HInit设置日志文件名先关信息,并打开文件

2、记录日志有2种方法,一种是每一行记录前面支持日期, 另一种则不支持记录日期。

3、该类会在 exe所在目录创建一个 HLog 文件夹,用于存放日志文件

不足:

1、没有指定日志文件的大小,可以一直向文件写入,很不友好,

2、没有增加删除日志,比如,软件自动删除前1个月的日志

针对这些不足,后期维护补上..........................................

Ex:

设置日志名

#ifdef he_use_log

m_Log.HInit(QString("ABC"));

#endif//he_use_log

记录日志

#ifdef he_use_log

m_Log.HLogTime(QString("void Dialog::TopMenuStartSlotOpen() start"));

#endif // he_use_log

--- 完-----

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值