1 Star 1 Fork 0

俞寅达/nicenet

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Log.cpp 1.57 KB
一键复制 编辑 原始数据 按行查看 历史
俞寅达 提交于 2021-10-05 16:02 . complete
#include "Log.h"
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <chrono>
#include "Clock.h"
MODULE_IMPL(nicehero::Log)
namespace nicehero
{
const int TIMEBUF_LENGTH = 12;
const int LOGBUF_MAXLEN = 1024 * 1024;
char LOGBUF[LOGBUF_MAXLEN + 1] = { 0 };
void Log::log(const char *msg, ...)
{
const int reservedLen = 1;//for \n
ui64 now = Clock::getInstance()->getTime();
#ifdef WIN32
tm tm_now;
localtime_s(&tm_now, (time_t*)&now);
strftime(m_timebuf, sizeof(m_timebuf), "[%H:%M:%S]: ", &tm_now);
strncpy(LOGBUF, m_timebuf, TIMEBUF_LENGTH);
#else
tm* tm_ = localtime((time_t*)&now);
strftime(m_timebuf, sizeof(m_timebuf), "[%H:%M:%S]: ", tm_);
strncpy(LOGBUF, m_timebuf, TIMEBUF_LENGTH);
#endif
const int size = LOGBUF_MAXLEN - TIMEBUF_LENGTH - reservedLen;
va_list args;
va_start(args, msg);
vsnprintf(LOGBUF + TIMEBUF_LENGTH, size, msg, args);
va_end(args);
puts(LOGBUF);
}
void Log::logerr(const char *msg, ...)
{
const int reservedLen = 1;//for \n
ui64 now = Clock::getInstance()->getTime();
#ifdef WIN32
tm tm_now;
localtime_s(&tm_now, (time_t*)&now);
strftime(m_timebuf, sizeof(m_timebuf), "[%H:%M:%S]: ", &tm_now);
strncpy(LOGBUF, m_timebuf, TIMEBUF_LENGTH);
#else
tm* tm_ = localtime((time_t*)&now);
strftime(m_timebuf, sizeof(m_timebuf), "[%H:%M:%S]: ", tm_);
strncpy(LOGBUF, m_timebuf, TIMEBUF_LENGTH);
#endif
const int size = LOGBUF_MAXLEN - TIMEBUF_LENGTH - reservedLen;
va_list args;
va_start(args, msg);
vsnprintf(LOGBUF + TIMEBUF_LENGTH, size, msg, args);
va_end(args);
puts(LOGBUF);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/nicehero/nicenet.git
[email protected]:nicehero/nicenet.git
nicehero
nicenet
nicenet
master

搜索帮助