1 Star 1 Fork 0

xiongqiumin/LockServer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LogManager.cpp 1.66 KB
一键复制 编辑 原始数据 按行查看 历史
xiongqiumin 提交于 2021-01-25 23:23 . init
#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);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xiongqiumin/lock-server.git
[email protected]:xiongqiumin/lock-server.git
xiongqiumin
lock-server
LockServer
master

搜索帮助