mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 16:35:45 +08:00
timestamp caching of last second
This commit is contained in:
parent
52dc317cfb
commit
6400b1887e
@ -1,23 +1,31 @@
|
|||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
|
#include <memory.h>
|
||||||
|
|
||||||
#include "c11log/formatters/formatters.h"
|
#include "c11log/formatters/formatters.h"
|
||||||
#include "c11log/level.h"
|
#include "c11log/level.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static thread_local std::tm last_tm;
|
||||||
|
static thread_local char timestamp_cache[64];
|
||||||
|
|
||||||
void c11log::formatters::format_time(const c11log::formatters::timepoint& tp, std::ostream &dest)
|
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));
|
auto tm = details::os::localtime(std::chrono::system_clock::to_time_t(tp));
|
||||||
//get ms
|
// Cache timestamp string of last second
|
||||||
//auto duration = tp.time_since_epoch();
|
if(memcmp(&tm, &last_tm, sizeof(tm)))
|
||||||
//int millis = static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000);
|
{
|
||||||
|
sprintf(timestamp_cache, "[%d-%02d-%02d %02d:%02d:%02d]", tm.tm_year + 1900,
|
||||||
|
tm.tm_mon + 1,
|
||||||
char buf[64];
|
tm.tm_mday,
|
||||||
auto size = sprintf(buf, "[%d-%02d-%02d %02d:%02d:%02d]",
|
tm.tm_hour,
|
||||||
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
tm.tm_min,
|
||||||
tm.tm_hour, tm.tm_min, tm.tm_sec);
|
tm.tm_sec);
|
||||||
|
last_tm = tm;
|
||||||
dest.write(buf, size);
|
}
|
||||||
|
|
||||||
|
dest << timestamp_cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
void c11log::formatters::format_time(std::ostream& dest)
|
void c11log::formatters::format_time(std::ostream& dest)
|
||||||
|
Loading…
Reference in New Issue
Block a user