1 Star 0 Fork 0

aven/tlib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
TLogger.cc 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
qq66921494 提交于 2015-07-31 00:20 . .
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include "TLogger.h"
Logger::Logger(char* filePath)
{
logFileTime_ = 0;
logFile_ = NULL;
strcpy(filePath_, filePath);
}
Logger::~Logger()
{
if (logFile_)
{
fclose(logFile_);
logFile_ = NULL;
}
}
void Logger::OpenFile(char* file)
{
if (logFile_)
{
fclose(logFile_);
logFile_ = NULL;
}
logFile_ = fopen(file,"a + ");
fprintf(logFile_,"\n\n");
fprintf(logFile_,"====================================================\n");
fprintf(logFile_,"====================STARTRUN========================\n");
fprintf(logFile_,"====================================================\n");
fprintf(logFile_,"\n\n");
}
void Logger::WriteLog(const char* level, const char* fmt, ...)
{
std::lock_guard<std::mutex> lock(mutex_);
va_list args;
va_start(args, fmt);
vsprintf(writeBuf_, fmt, args);
va_end(args);
time_t atimer = time(NULL);
struct tm* area = localtime(&atimer);
int time = ((area->tm_year + 1900)*100)
+ ((area->tm_mon + 1)*10000)
+ (area->tm_mday*100)
+ area->tm_hour;
if (logFileTime_ < time)
{
logFileTime_ = time;
char file[256];
sprintf(file, "%s_%04d%02d%02d%02d.log", filePath_, area->tm_year + 1900, area->tm_mon + 1, area->tm_mday,area->tm_hour);
OpenFile(file);
}
fprintf(logFile_, "%04d-%02d-%02d %02d:%02d:%02d TID:%05d %5s - %s\n", area->tm_year + 1900, area->tm_mon + 1, area->tm_mday, area->tm_hour, area->tm_min, area->tm_sec, 0, level, writeBuf_);
fflush(logFile_);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xubingyue/tlib.git
git@gitee.com:xubingyue/tlib.git
xubingyue
tlib
tlib
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385