| 6383 | } |
| 6384 | |
| 6385 | struct log_printf_t { |
| 6386 | std::size_t operator()( // |
| 6387 | char *buffer, std::size_t buffer_size, // |
| 6388 | std::source_location const &location, int code, std::string_view message) const noexcept { |
| 6389 | // On MSVC, the `high_resolution_clock` is the `steady_clock`, which won't work with `to_time_t`. |
| 6390 | // `std::chrono::high_resolution_clock` is usually just an alias to either `system_clock` or |
| 6391 | // `steady_clock`. There is debate on whether using it is a good idea at all. |
| 6392 | // https://en.cppreference.com/w/cpp/chrono/high_resolution_clock |
| 6393 | #if defined(_MSC_VER) || defined(__APPLE__) |
| 6394 | auto now = std::chrono::system_clock::now(); |
| 6395 | #else |
| 6396 | auto now = std::chrono::high_resolution_clock::now(); |
| 6397 | #endif |
| 6398 | auto time_since_epoch = now.time_since_epoch(); |
| 6399 | |
| 6400 | // Extract seconds and milliseconds |
| 6401 | auto seconds = std::chrono::duration_cast<std::chrono::seconds>(time_since_epoch); |
| 6402 | auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(time_since_epoch) - seconds; |
| 6403 | |
| 6404 | // Format as ISO 8601: YYYY-MM-DDTHH:MM:SS.mmm |
| 6405 | auto time_t_now = std::chrono::system_clock::to_time_t(now); |
| 6406 | auto tm = std::gmtime(&time_t_now); // UTC only, no local timezone dependency |
| 6407 | |
| 6408 | int count_bytes = std::snprintf( // |
| 6409 | buffer, buffer_size, |
| 6410 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ | " // time format |
| 6411 | "%s:%d <%03d> " // location and code format |
| 6412 | "\"%.*s\"\n", // message format |
| 6413 | tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, // date |
| 6414 | tm->tm_hour, tm->tm_min, tm->tm_sec, static_cast<int>(milliseconds.count()), // time |
| 6415 | location.file_name(), location.line(), code, // location and code |
| 6416 | static_cast<int>(message.size()), message.data() // message of known length |
| 6417 | ); |
| 6418 | return static_cast<std::size_t>(count_bytes); |
| 6419 | } |
| 6420 | }; |
| 6421 | |
| 6422 | BENCHMARK(logging<log_printf_t>)->Name("log_printf")->MinTime(2); |
| 6423 |
nothing calls this directly
no outgoing calls
no test coverage detected