From 6d91e680c57661245d93a99b42dc9d7c14a0dce6 Mon Sep 17 00:00:00 2001 From: ns6089 <61738816+ns6089@users.noreply.github.com> Date: Thu, 8 Aug 2024 17:52:08 +0300 Subject: [PATCH] feat(logging): include milliseconds timestamps (#2963) --- src/logging.cpp | 15 ++++++++++----- src_assets/common/assets/web/index.html | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/logging.cpp b/src/logging.cpp index 5cc226ea..a78b5b5d 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -4,10 +4,12 @@ */ // standard includes #include +#include #include // lib includes #include +#include #include #include #include @@ -67,7 +69,6 @@ namespace logging { sink->set_formatter([](const bl::record_view &view, bl::formatting_ostream &os) { constexpr const char *message = "Message"; constexpr const char *severity = "Severity"; - constexpr int DATE_BUFFER_SIZE = 21 + 2 + 1; // Full string plus ": \0" auto log_level = view.attribute_values()[severity].extract().get(); @@ -93,11 +94,15 @@ namespace logging { break; }; - char _date[DATE_BUFFER_SIZE]; - std::time_t t = std::time(nullptr); - strftime(_date, DATE_BUFFER_SIZE, "[%Y:%m:%d:%H:%M:%S]: ", std::localtime(&t)); + auto now = std::chrono::system_clock::now(); + auto ms = std::chrono::duration_cast( + now - std::chrono::time_point_cast(now)); - os << _date << log_type << view.attribute_values()[message].extract(); + auto t = std::chrono::system_clock::to_time_t(now); + auto lt = *std::localtime(&t); + + os << "["sv << std::put_time(<, "%Y-%m-%d %H:%M:%S.") << boost::format("%03u") % ms.count() << "]: "sv + << log_type << view.attribute_values()[message].extract(); }); // Flush after each log record to ensure log file contents on disk isn't stale. diff --git a/src_assets/common/assets/web/index.html b/src_assets/common/assets/web/index.html index d279f16d..9eccf9e1 100644 --- a/src_assets/common/assets/web/index.html +++ b/src_assets/common/assets/web/index.html @@ -142,7 +142,7 @@ /** Parse the text errors, calculating the text, the timestamp and the level */ fancyLogs() { if (!this.logs) return []; - let regex = /(\[\d{4}:\d{2}:\d{2}:\d{2}:\d{2}:\d{2}\]):\s/g; + let regex = /(\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}]):\s/g; let rawLogLines = (this.logs.split(regex)).splice(1); let logLines = [] for (let i = 0; i < rawLogLines.length; i += 2) {