From 26b1b1ee2255632f16a762dfcb488ef3eff1660c Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Mon, 18 Nov 2024 20:25:01 +0100 Subject: some fixes to timestamp caching in logging (#219) * some fixes to timestamp caching in logging --- src/zenutil/include/zenutil/logging/fullformatter.h | 4 ++-- src/zenutil/include/zenutil/logging/testformatter.h | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) (limited to 'src/zenutil') diff --git a/src/zenutil/include/zenutil/logging/fullformatter.h b/src/zenutil/include/zenutil/logging/fullformatter.h index d4a04d3e5..64010ac1e 100644 --- a/src/zenutil/include/zenutil/logging/fullformatter.h +++ b/src/zenutil/include/zenutil/logging/fullformatter.h @@ -183,8 +183,8 @@ public: private: std::chrono::time_point m_Epoch; std::tm m_CachedLocalTm; - std::chrono::seconds m_LastLogSecs; - std::atomic m_CacheTimestamp; + std::chrono::seconds m_LastLogSecs{std::chrono::seconds(87654321)}; + std::atomic m_CacheTimestamp{std::chrono::seconds(87654321)}; spdlog::memory_buf_t m_CachedDatetime; std::string m_LogId; std::string m_LinePrefix; diff --git a/src/zenutil/include/zenutil/logging/testformatter.h b/src/zenutil/include/zenutil/logging/testformatter.h index 39fc44e75..bdeb287be 100644 --- a/src/zenutil/include/zenutil/logging/testformatter.h +++ b/src/zenutil/include/zenutil/logging/testformatter.h @@ -37,8 +37,10 @@ public: auto duration = msg.time - m_Epoch; auto secs = std::chrono::duration_cast(duration); - if (m_CacheTimestamp != secs || m_CachedDatetime.size() == 0) + if (m_CacheTimestamp != secs) { + RwLock::ExclusiveLockScope _(m_TimestampLock); + m_CachedDatetime.clear(); m_CachedDatetime.push_back('['); @@ -85,7 +87,10 @@ public: m_CacheTimestamp = secs; } - dest.append(m_CachedDatetime.begin(), m_CachedDatetime.end()); + { + RwLock::SharedLockScope _(m_TimestampLock); + dest.append(m_CachedDatetime.begin(), m_CachedDatetime.end()); + } auto millis = spdlog::details::fmt_helper::time_fraction(msg.time); spdlog::details::fmt_helper::pad3(static_cast(millis.count()), dest); @@ -137,10 +142,11 @@ public: private: std::chrono::time_point m_Epoch; std::tm m_CachedTm; - std::chrono::seconds m_LastLogSecs; - std::chrono::seconds m_CacheTimestamp{0}; + std::chrono::seconds m_LastLogSecs{std::chrono::seconds(87654321)}; + std::chrono::seconds m_CacheTimestamp{std::chrono::seconds(87654321)}; spdlog::memory_buf_t m_CachedDatetime; std::string m_LogId; + RwLock m_TimestampLock; }; -} // namespace zen::logging \ No newline at end of file +} // namespace zen::logging -- cgit v1.2.3