代码拉取完成,页面将自动刷新
#include "LogManager.h"
#include "stdarg.h"
#include <QTime>
#include <QDir>
#include <QApplication>
#include <QMetaType>
//LogManager
LogManager *LogManager::instance()
{
static LogManager inst;
return &inst;
}
LogManager::LogManager()
{
qRegisterMetaType<LogObjectPtr>("LogObjectPtr");
m_init = false;
}
LogManager::~LogManager()
{
}
void LogManager::init()
{
connect(&timer, SIGNAL(timeout()), this, SLOT(onTimeOut()));
createFile();
timer.start(60 * 1000);
m_init = true;
}
void LogManager::deinit()
{
QMutexLocker locker(&mutex);
m_init = false;
this->disconnect();
timer.stop();
generalFile.close();
luaFile.close();
}
void LogManager::createFile()
{
QString logDir = QApplication::applicationDirPath() + "/log";
QDir dir;
if (!QDir(logDir).exists())
dir.mkdir(logDir);
curDate = QDate::currentDate();
QString generalPath = logDir + "/" + curDate.toString("yyyy-MM-dd") + ".log";
generalFile.setFileName(generalPath);
generalFile.open(QIODevice::WriteOnly | QIODevice::Append);
}
void LogManager::onTimeOut()
{
QMutexLocker locker(&mutex);
if (curDate.daysTo(QDate::currentDate()) >= 1)
createFile();
}
void LogManager::log(const char * module, int type, const char *format, ...)
{
QMutexLocker locker(&mutex);
if (!m_init)
return;
char buffer[1024] = { 0 };
va_list var;
va_start(var, format);
vsnprintf(buffer, 1024, format, var);
va_end(var);
//
LogObject *obj = new LogObject();
obj->time = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
obj->module = module;
obj->level = type;
obj->message = buffer;
emit sigLog(LogObjectPtr(obj));
//дļ
strcat(buffer, "\r\n");
generalFile.write(buffer);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。