零、效果
[INFO] 2020-02-25 14:47:57.473 threadId 11144, File:..\SX\EasyLog.h ,Line:37 ,Func: >> 初始化
一、目录结构如下
二、pri文件
SOURCES += $$PWD/EasyLog.cpp
HEADERS += $$PWD/EasyLog.h
三、h 文件
#ifndef EASYLOG_H
#define EASYLOG_H
#include <QFile>
#include <QMutex>
#include <QDateTime>
#include <QTextStream>
#include <stdio.h>
#include <QMap>
#define LOG_INFO 0
#define LOG_WARNING 1
#define LOG_ERROR 2
#define DETAIL_INFO QString::fromLocal8Bit(" File:%1 ,Line:%2 ,Func:%3 >> ").arg(__FILE__).arg(__LINE__).arg(__FUNCTION__)
#define LOG_IDENTIFIER ".log"
#define LOGERROR(_log) EasyLOG->writeLog(_log, DETAIL_INFO, LOG_ERROR)
#define LOGINFO(_log) EasyLOG->writeLog(_log, DETAIL_INFO, LOG_INFO)
#define LOGWRANING(_log) EasyLOG->writeLog(_log, DETAIL_INFO, LOG_WARNING)
//==============================================================================
#define EasyLOG EasyLog::getInstance("AppTrace")
#define SSTLOG EasyLog::getInstance("SSTrace")
//==============================================================================
class EasyLog
{
public:
static EasyLog *getInstance(QString logPrefix);
void writeLog(QString _log, QString _details = DETAIL_INFO, int _flag = LOG_INFO);
void InitLog();
void DelLogFile(QDate begin,QDate end,QString path);
private:
EasyLog(QString prefix);
~EasyLog();
void openLog();
private:
static EasyLog instance;
QString m_fileDate;
QFile m_log;
QMutex m_mutex;
QTextStream m_stream;
QMap<QString,EasyLog*> m_mapLog;
QString m_logPrefix;
QString m_logPath;
};
#endif // EASYLOG_H
四、 cpp 文件夹
#include "EasyLog.h"
#include <QDir>
#include <QDirIterator>
#include <QSettings>
#include <QApplication>
#include <QDebug>
#include <QThread>
EasyLog EasyLog::instance("");
EasyLog::EasyLog(QString prefix)
{
this->m_logPrefix = prefix;
if (prefix != "") {
InitLog();
}
}
EasyLog::~EasyLog()
{
if (!m_mapLog.empty()) {
QMapIterator<QString, EasyLog*> iter(m_mapLog);
while (iter.hasNext()) {
iter.next();
EasyLog* elog = iter.value();
if (elog != NULL) {
delete elog;
elog = NULL;
}
}
this->m_mapLog.clear();
}
}
EasyLog *EasyLog::getInstance(QString logPrefix)
{
if (instance.m_mapLog[logPrefix] == NULL) {
EasyLog* log = new EasyLog(logPrefix);
instance.m_mapLog[logPrefix] = log;
}
return instance.m_mapLog[logPrefix];
}
void EasyLog::InitLog()
{
//[1] 进入日志路径
QDir _applicationDir(QApplication::applicationDirPath());
if (!_applicationDir.exists(_applicationDir.path() + "/log"))
{
_applicationDir.mkpath(_applicationDir.path() + "/log");
}
_applicationDir.cd("log");
this->m_logPath = _applicationDir.path();
qDebug()<<"logpath:"<<this->m_logPath << this->m_logPrefix;
//[2] 删除前30天的日志
QDate delDate = QDate::currentDate().addDays(-30);
DelLogFile(delDate,QDate::currentDate(),m_logPath +"/");
//[3] 打开文件
this->openLog();
}
void EasyLog::DelLogFile(QDate begintime, QDate endtime, QString path)
{
QDir dir(path);
QStringList nameFilters;
nameFilters << "*.log";
QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
if(dir.count() == 0)
{
return;
}
for (int var = 0; var < files.count(); var++)
{
QString fname = files[var];
QStringList tmpFnamelst = fname.split("_");
QString aname = tmpFnamelst.at(1);
QDate tmpDate = QDate::fromString(aname.left(10), "yyyy-MM-dd");
if ( tmpDate.isValid() && tmpDate > begintime && tmpDate <= endtime) {
qDebug()<<fname<<" save.";
}else{
dir.remove(files[var]);
qDebug()<<"@del file name:"<<fname<<" "<<",del succ";
}
}
}
void EasyLog::openLog()
{
if (m_logPrefix == "") {
return;
}
QString date = QDate::currentDate().toString("yyyy-MM-dd");
QString fileNamelog;
fileNamelog += this->m_logPrefix;
fileNamelog += "_";
fileNamelog += date;
fileNamelog += LOG_IDENTIFIER;
m_log.setFileName(m_logPath + "/" + fileNamelog);
// 进行日期对比;查看是否要新建一个日志
QString sDate = QDateTime::currentDateTime().toString("yyyy_MM_dd");
if (m_fileDate != sDate)
{
this->m_fileDate = sDate;
if (m_log.open(QIODevice::ReadWrite | QIODevice::Text | QFile::Append))
{
m_stream.setDevice(&m_log);
QString _logTitle = QString::asprintf("//initial log success\n\n");
m_stream << _logTitle;
}
else
{
QString _err = m_log.errorString();
}
}
}
void EasyLog::writeLog(QString _log, QString _details, int _flag)
{
if (this->m_logPrefix == "") {
return;
}
QDateTime dtime = QDateTime::currentDateTime();
QLocale locale = QLocale::English;//指定显示
QString stemp;
stemp.sprintf("%s.%03d threadId %d,",
(locale.toString(dtime,"yyyy-MM-dd hh:mm:ss").toStdString().c_str()),
dtime.time().msec(),
(int)QThread::currentThreadId());
m_mutex.lock();
if (_flag == LOG_INFO)
{
m_stream <<"[INFO] " << stemp << " " << _details<< _log<< "\n";
}
else if (_flag == LOG_WARNING)
{
m_stream << "[WARN] " << stemp << " " << _details << _log<< "\n";
}
else if (_flag == LOG_ERROR)
{
m_stream << "[ERR] " << stemp << " " << _details << _log<< "\n";
}
m_stream.flush();
m_log.flush();
m_mutex.unlock();
}