Files
spdlog/src/formatters.cpp

49 lines
1.2 KiB
C++
Raw Normal View History

2014-01-25 11:09:04 +02:00
#include "stdafx.h"
2014-02-06 00:52:58 +02:00
#include <memory.h>
2014-01-25 11:09:04 +02:00
#include "c11log/formatters/formatters.h"
#include "c11log/level.h"
2014-02-06 00:52:58 +02:00
static thread_local std::tm last_tm;
static thread_local char timestamp_cache[64];
2014-01-25 11:09:04 +02:00
void c11log::formatters::format_time(const c11log::formatters::timepoint& tp, std::ostream &dest)
{
2014-01-27 19:35:18 +02:00
2014-02-06 00:52:58 +02:00
auto tm = details::os::localtime(std::chrono::system_clock::to_time_t(tp));
// Cache timestamp string of last second
if(memcmp(&tm, &last_tm, sizeof(tm)))
{
sprintf(timestamp_cache, "[%d-%02d-%02d %02d:%02d:%02d]", tm.tm_year + 1900,
tm.tm_mon + 1,
tm.tm_mday,
tm.tm_hour,
tm.tm_min,
tm.tm_sec);
last_tm = tm;
}
dest << timestamp_cache;
2014-01-25 11:09:04 +02:00
}
void c11log::formatters::format_time(std::ostream& dest)
{
return format_time(std::chrono::system_clock::now(), dest);
}
static const char _hex_chars[17] = "0123456789ABCDEF";
std::string c11log::formatters::to_hex(const unsigned char* buf, std::size_t size)
{
std::ostringstream oss;
for (std::size_t i = 0; i < size; i++) {
oss << _hex_chars[buf[i] >> 4];
oss << _hex_chars[buf[i] & 0x0F];
}
return oss.str();
2014-01-27 11:44:10 +02:00
}