Qt 日志之Qdebug 封装类

简单的日志库可以用Qdebug做一个注册InstallMessageHandle,还有一种是使用开源的日志库,如Boost 库,Log4Qt 等。开源这些日志功能比较强大,Qdebug的话做些本地运行日志跟踪状态,还是足够的。

根据日期生成文件夹
文件夹存放日志,按时间命名,大于1024 * 1024 * 4 ,(4M)自动新建文件。大小可以根据具体项目更改.
文件自动新增
时间,文件名+行号+level +信息 :
内容
只需包含如下头文件:
使用时:
CLog::InstallLog(); 初始化
qDebug()<<QStringLiteral"中文消息";
qError()<<…;

#pragma once
//by karlchan cgs
#include <QDateTime> 
#include <QFile>
#include <QTextStream>
#include <QApplication>
#include <QDebug>
#include <QDir>

static int gIndex = 0;
class CLog
{
public:
	CLog();
	~CLog();
	
public:
	static void InstallLog() 
	{
		makesureDir();
		makesureIndex();
		qInstallMessageHandler(CLog::outputMessage);
	};
protected:
	
private:
	static void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
	{
		static QMutex mutex;
		QMutexLocker locker(&mutex);  
		QString	strDir = QApplication::applicationDirPath() + "/../Log/" + QDateTime::currentDateTime().toString("yyyyMMdd");

		QString text;
		switch (type)
		{
		case QtWarningMsg:
			text = QString("[Warning]:");
			break;
		case QtInfoMsg:
		case QtDebugMsg:
			text = QString("[Info]:");
			break;
		case QtFatalMsg:
		case QtCriticalMsg:
			text = QString("[Error]:");
		}

		QString context_info = QString("[%1-%2]").arg(QString(context.file)).arg(context.line);
		QString current_date_time = QDateTime::currentDateTime().toString("[hh:mm:ss.z]");//yyyy-MM-dd
		QString current_date = QDateTime::currentDateTime().toString("yyyyMMdd");//z
		current_date += QString("_%1").arg(gIndex, 3, 10, QChar('0'));
		QString message = QString("%0%1%2 %3").arg(current_date_time).arg(context_info).arg(text).arg(msg);
		
		QString strPath = strDir + "/" + current_date + ".log";

		QFileInfo fi(strPath);
		if (fi.size() >= (1024 * 1024 * 10))
		{
			gIndex++;
			current_date = QDateTime::currentDateTime().toString("yyyyMMdd");//z
			current_date += QString("_%1").arg(gIndex, 3, 10, QChar('0'));
		}
		
		strPath = strDir + "/" + current_date + ".log";
		QFile file(strPath);
		if (file.open(QIODevice::WriteOnly | QIODevice::Append))
		{
			QTextStream text_stream(&file);
			text_stream << message << "\r\n";
			file.flush();
			file.close();  
		}
	}
	static void makesureDir() 
	{
		QString strDir;
		strDir = QApplication::applicationDirPath() + "/../Log/";
		QDir dir(strDir);
		if (!dir.exists())
		{
			dir.mkdir(strDir);
		}
		strDir = QApplication::applicationDirPath() + "/../Log/" + QDateTime::currentDateTime().toString("yyyyMMdd");
		dir.setPath(strDir);
		if (!dir.exists())
		{
			dir.mkdir(strDir);
		}
	};
	static void makesureIndex() 
	{
		QString	strDir = QApplication::applicationDirPath() + "/../Log/" + QDateTime::currentDateTime().toString("yyyyMMdd");
		QDir dir(strDir);
		dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
		QFileInfoList list = dir.entryInfoList();
		for each (auto var in list)
		{
			QFileInfo fi(var.absoluteFilePath());
			if (fi.size() >= (1024 * 1024 * 10))
			{
				gIndex++;
			}
		}
	};
};


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值