#pragma once
#include <QObject>
#include <QMutex>
#define gSysLog LkSystemLog::instance()
class LkSystemLog : public QObject
{
Q_OBJECT
private:
LkSystemLog(QObject *parent = 0);
~LkSystemLog();
public:
static LkSystemLog* instance();
void info(QString info, bool cullRepeat = false);
void warn(QString warn, bool cullRepeat = false);
void error(QString error, bool cullRepeat = false);
void shutdown();
void clearRepeatList();
void init();
public slots:
void onFileTimer();
private:
QStringList mLogList;
QMutex mMutex;
int mCurLogHour;
};
#include "LkSystemLog.h"
#include <QDateTime>
#include <QMutexLocker>
#include "log4cpp/Category.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/DailyRollingFileAppender.hh"
#include "log4cpp/SimpleLayout.hh"
#include "log4cpp/PropertyConfigurator.hh"
#include "log4cpp/PatternLayout.hh"
#include "log4cpp/OstreamAppender.hh"
#include "LkHourlyRollingFileAppender.h"
#include <QCoreApplication>
#include <QFile>
#include <QDir>
#include <QTimer>
#define LogDirName "log"
#define DataDirName "data"
#define WRITE_LOG 1
static log4cpp::Category *logger;
LkSystemLog::LkSystemLog(QObject *parent)
: QObject(parent)
{
init();
}
LkSystemLog::~LkSystemLog()
{
logger->shutdown();
}
void LkSystemLog::init()
{
{
log4cpp::PatternLayout* pLayout1 = new log4cpp::PatternLayout();
pLayout1->setConversionPattern("%d: [%p] %m%n");
QString dirPath = QCoreApplication::applicationDirPath() + "/" + LogDirName;
QDir dir(dirPath);
if (!dir.exists())
{
dir.mkdir(dirPath);
}
QString logName = dirPath + "/" + QDateTime::currentDateTime().toString("yyyy-MM-dd-HH") + "H.log";
log4cpp::LkHourlyRollingFileAppender* fileAppender = new log4cpp::LkHourlyRollingFileAppender("fileAppender", logName.toStdString(), dirPath.toStdString());
fileAppender->setLayout(pLayout1);
logger = &(log4cpp::Category::getRoot().getInstance("Log"));
logger->addAppender(fileAppender);
logger->setPriority(log4cpp::Priority::INFO);
mCurLogHour = QDateTime::currentDateTime().time().hour();
QTimer* fileTimer = new QTimer(this);
connect(fileTimer, &QTimer::timeout, this, &LkSystemLog::onFileTimer);
fileTimer->start(1000);
}
}
LkSystemLog* LkSystemLog::instance()
{
static LkSystemLog sysLog;
return &sysLog;
}
void LkSystemLog::onFileTimer()
{
int curHour = QDateTime::currentDateTime().time().hour();
if (curHour != mCurLogHour)
{
QMutexLocker locker(&mMutex);
mCurLogHour = curHour;
log4cpp::LkHourlyRollingFileAppender* appender =
dynamic_cast<log4cpp::LkHourlyRollingFileAppender*>(logger->getAppender("fileAppender"));
struct tm now;
time_t t = time(NULL);
bool timeok = localtime_s(&now, &t) == 0;
if (timeok) {
appender->setLogsTime(now);
appender->rollOver();
}
}
}
void LkSystemLog::info(QString info, bool cullRepeat)
{
if (cullRepeat && mLogList.contains(info)){
return;
}
mLogList.append(info);
QMutexLocker locker(&mMutex);
logger->info(info.toStdString());
}
void LkSystemLog::warn(QString warn, bool cullRepeat )
{
return;
if (cullRepeat && mLogList.contains(warn)) {
return;
}
mLogList.append(warn);
QMutexLocker locker(&mMutex);
logger->warn(warn.toStdString());
}
void LkSystemLog::error(QString error, bool cullRepeat)
{
return;
if (cullRepeat && mLogList.contains(error)) {
return;
}
mLogList.append(error);
QMutexLocker locker(&mMutex);
logger->error(error.toStdString());
}
void LkSystemLog::shutdown()
{
}
void LkSystemLog::clearRepeatList()
{
mLogList.clear();
}