|
VLink 2.0.0
A high-performance communication middleware
|
A low-overhead, thread-safe, formatted timestamp generator for log output. 更多...
#include <atomic>#include <chrono>#include <cstdint>#include <mutex>#include <string_view>#include "./macros.h"类 | |
| class | vlink::CachedTimestamp |
| Cached, thread-safe formatted timestamp generator. 更多... | |
命名空间 | |
| namespace | vlink |
A low-overhead, thread-safe, formatted timestamp generator for log output.
CachedTimestamp generates a human-readable timestamp string (e.g. "03-18 14:30:01.042") without formatting the full string every call. Instead it caches the date-and-second part and only updates the millisecond field on sub-second increments, significantly reducing the number of expensive strftime / localtime_r calls under high log throughput.
The caching strategy:
std::atomic<int64_t> storing the last formatted Unix second. Access is serialized so only one thread updates the shared cache at a time.update_milliseconds() without reformatting the entire string.std::mutex protects the shared buffer while formatting or patching the millisecond field."%02d-%02d %02d:%02d:%02d.%03d" produces "MM-DD HH:MM:SS.mmm" (18 characters). Do not include year if character count matters.