1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-03-31 02:12:40 +08:00
spdlog/src/formatters.cpp
2014-01-27 19:35:18 +02:00

41 lines
1.2 KiB
C++

#include "stdafx.h"
#include "c11log/formatters/formatters.h"
#include "c11log/level.h"
void c11log::formatters::format_time(const c11log::formatters::timepoint& tp, std::ostream &dest)
{
std::tm tm = details::os::localtime(std::chrono::system_clock::to_time_t(tp));
//get ms
//auto duration = tp.time_since_epoch();
//int millis = static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000);
char buf[64];
auto size = sprintf(buf, "[%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);
dest.write(buf, size);
}
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();
}