VLink  2.0.0
A high-performance communication middleware
cached_timestamp.h 文件参考

Sub-second-cached timestamp string generator used by the VLink logger hot path. 更多...

#include <atomic>
#include <chrono>
#include <cstdint>
#include <mutex>
#include <string_view>
#include "./macros.h"
cached_timestamp.h 的引用(Include)关系图:

浏览源代码.

class  vlink::CachedTimestamp
 Mutable cached generator for short formatted timestamps. 更多...
 

命名空间

 

详细描述

Sub-second-cached timestamp string generator used by the VLink logger hot path.

CachedTimestamp renders a wall-clock or UTC timestamp into a stable internal buffer and patches only the millisecond suffix when the seconds field has not advanced. This avoids the repeated snprintf / localtime_r round trip that dominates naive per-line logger output.

Resolution and field layout
Field Width Source
Month 2 %02d
Day 2 %02d
Hour 2 %02d
Minute 2 %02d
Second 2 %02d
Millisecond (in-place patch) 3 %03d, last three chars of buffer

The default format "%02d-%02d @c %02d:%02d:%02d.%03d" produces 18 characters such as "03-18 14:30:01.042". The internal buffer is 32 bytes; longer formats are dropped.

Example
for (int i = 0; i < 1000; ++i) {
// Wall clock (local time) - patched in place on the same second.
std::string_view local = ts.get();
// UTC variant on demand:
std::string_view utc = ts.get("%02d-%02d %02d:%02d:%02d.%03d", true);
write_to_log(local);
}
注解
Used internally by vlink::Logger. Application code rarely needs to construct one directly; doing so is harmless and inexpensive.