Template Function gkfs::log::detail::format_timestamp_to

Function Documentation

template<typename Buffer>
static inline void gkfs::log::detail::format_timestamp_to(Buffer &&buffer, const date::time_zone *const timezone = nullptr)

format_timestamp_to - safely format a timestamp for logging messages

This function produes a timestamp that can be used to prefix logging messages. Since we are actively intercepting system calls, the formatting MUST NOT rely on internal system calls, otherwise we risk recursively calling ourselves for each syscall generated. Also, we cannot rely on the C formatting functions asctime, ctime, gmtime, localtime, mktime, asctime_r, ctime_r, gmtime_r, localtime_r, since they acquire a non-reentrant lock to determine the caller’s timezone (yes, the assumedly reentrant *_r versions of the functions exhibit this problem as well, see https://sourceware.org/bugzilla/show_bug.cgi?id=16145). To solve this issue and still get readable timestamps, we determine and cache the timezone when the logger is created so that the lock is only held once, by one thread exactly, and we pass it as an argument whenever we need to format a timestamp. If no timezone is provided, we just format the epoch.

NOTE: we use the date C++ library to query the timezone database and to format the timestamps.