From eab522e743b4f41caed1a7426cf37c77531d1382 Mon Sep 17 00:00:00 2001 From: doug1234 Date: Thu, 13 Jan 2022 20:57:14 -0500 Subject: [PATCH 1/5] Now only getting the date if formater needs to display date related information. --- include/spdlog/pattern_formatter-inl.h | 43 +++++++++++++++++++++++--- include/spdlog/pattern_formatter.h | 1 + 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index ec727032b..caf7c62ca 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -1021,6 +1021,7 @@ SPDLOG_INLINE pattern_formatter::pattern_formatter( , pattern_time_type_(time_type) , last_log_secs_(0) , custom_handlers_(std::move(custom_user_flags)) + , needs_time_(false) { std::memset(&cached_tm_, 0, sizeof(cached_tm_)); compile_pattern_(pattern_); @@ -1032,6 +1033,7 @@ SPDLOG_INLINE pattern_formatter::pattern_formatter(pattern_time_type time_type, , eol_(std::move(eol)) , pattern_time_type_(time_type) , last_log_secs_(0) + , needs_time_(false) { std::memset(&cached_tm_, 0, sizeof(cached_tm_)); formatters_.push_back(details::make_unique(details::padding_info{})); @@ -1049,11 +1051,13 @@ SPDLOG_INLINE std::unique_ptr pattern_formatter::clone() const SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest) { - auto secs = std::chrono::duration_cast(msg.time.time_since_epoch()); - if (secs != last_log_secs_) - { - cached_tm_ = get_time_(msg); - last_log_secs_ = secs; + if (needs_time_) { + const auto secs = std::chrono::duration_cast(msg.time.time_since_epoch()); + if (secs != last_log_secs_) + { + cached_tm_ = get_time_(msg); + last_log_secs_ = secs; + } } for (auto &f : formatters_) @@ -1097,6 +1101,7 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i { case ('+'): // default formatter formatters_.push_back(details::make_unique(padding)); + needs_time_ = true; break; case 'n': // logger name @@ -1121,101 +1126,125 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i case ('a'): // weekday formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('A'): // short weekday formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('b'): case ('h'): // month formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('B'): // short month formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('c'): // datetime formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('C'): // year 2 digits formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('Y'): // year 4 digits formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('D'): case ('x'): // datetime MM/DD/YY formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('m'): // month 1-12 formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('d'): // day of month 1-31 formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('H'): // hours 24 formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('I'): // hours 12 formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('M'): // minutes formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('S'): // seconds formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('e'): // milliseconds formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('f'): // microseconds formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('F'): // nanoseconds formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('E'): // seconds since epoch formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('p'): // am/pm formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('r'): // 12 hour clock 02:55:02 pm formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('R'): // 24-hour HH:MM time formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('T'): case ('X'): // ISO 8601 time format (HH:MM:SS) formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('z'): // timezone formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('P'): // pid formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('^'): // color range start @@ -1252,18 +1281,22 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i case ('u'): // elapsed time since last log message in nanos formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('i'): // elapsed time since last log message in micros formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('o'): // elapsed time since last log message in millis formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; case ('O'): // elapsed time since last log message in seconds formatters_.push_back(details::make_unique>(padding)); + needs_time_ = true; break; default: // Unknown flag appears as is diff --git a/include/spdlog/pattern_formatter.h b/include/spdlog/pattern_formatter.h index 6810f03b6..57156cb7f 100644 --- a/include/spdlog/pattern_formatter.h +++ b/include/spdlog/pattern_formatter.h @@ -103,6 +103,7 @@ class SPDLOG_API pattern_formatter final : public formatter std::string pattern_; std::string eol_; pattern_time_type pattern_time_type_; + bool needs_time_; std::tm cached_tm_; std::chrono::seconds last_log_secs_; std::vector> formatters_; From 5568b16ed5e3a7dffe3e67d30e0ca721da0a489f Mon Sep 17 00:00:00 2001 From: doug1234 Date: Thu, 13 Jan 2022 21:35:02 -0500 Subject: [PATCH 2/5] Resetting the needs time flag when setting a pattern. --- include/spdlog/pattern_formatter-inl.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index caf7c62ca..4342685d3 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -1071,6 +1071,7 @@ SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory SPDLOG_INLINE void pattern_formatter::set_pattern(std::string pattern) { pattern_ = std::move(pattern); + needs_time_ = false; compile_pattern_(pattern_); } From d9ec02d40024984e074fd357f352f41f421aedb0 Mon Sep 17 00:00:00 2001 From: doug1234 Date: Fri, 14 Jan 2022 20:06:26 -0500 Subject: [PATCH 3/5] Fix mistake in previous checkin. --- include/spdlog/pattern_formatter-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index 4342685d3..c98c9fb2d 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -1033,7 +1033,7 @@ SPDLOG_INLINE pattern_formatter::pattern_formatter(pattern_time_type time_type, , eol_(std::move(eol)) , pattern_time_type_(time_type) , last_log_secs_(0) - , needs_time_(false) + , needs_time_(true) { std::memset(&cached_tm_, 0, sizeof(cached_tm_)); formatters_.push_back(details::make_unique(details::padding_info{})); From 584d77237ea732e924cb5b1473667ce51321a371 Mon Sep 17 00:00:00 2001 From: doug1234 Date: Sat, 15 Jan 2022 13:35:27 -0500 Subject: [PATCH 4/5] Several minor improvements based on code review suggestions. --- include/spdlog/pattern_formatter-inl.h | 62 ++++++++++++-------------- include/spdlog/pattern_formatter.h | 2 +- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index c98c9fb2d..587e50620 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -1021,7 +1021,7 @@ SPDLOG_INLINE pattern_formatter::pattern_formatter( , pattern_time_type_(time_type) , last_log_secs_(0) , custom_handlers_(std::move(custom_user_flags)) - , needs_time_(false) + , need_localtime_(false) { std::memset(&cached_tm_, 0, sizeof(cached_tm_)); compile_pattern_(pattern_); @@ -1033,7 +1033,7 @@ SPDLOG_INLINE pattern_formatter::pattern_formatter(pattern_time_type time_type, , eol_(std::move(eol)) , pattern_time_type_(time_type) , last_log_secs_(0) - , needs_time_(true) + , need_localtime_(true) { std::memset(&cached_tm_, 0, sizeof(cached_tm_)); formatters_.push_back(details::make_unique(details::padding_info{})); @@ -1051,7 +1051,8 @@ SPDLOG_INLINE std::unique_ptr pattern_formatter::clone() const SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest) { - if (needs_time_) { + if (need_localtime_) + { const auto secs = std::chrono::duration_cast(msg.time.time_since_epoch()); if (secs != last_log_secs_) { @@ -1071,7 +1072,7 @@ SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory SPDLOG_INLINE void pattern_formatter::set_pattern(std::string pattern) { pattern_ = std::move(pattern); - needs_time_ = false; + need_localtime_ = false; compile_pattern_(pattern_); } @@ -1102,7 +1103,7 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i { case ('+'): // default formatter formatters_.push_back(details::make_unique(padding)); - needs_time_ = true; + need_localtime_ = true; break; case 'n': // logger name @@ -1127,125 +1128,120 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i case ('a'): // weekday formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('A'): // short weekday formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('b'): case ('h'): // month formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('B'): // short month formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('c'): // datetime formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('C'): // year 2 digits formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('Y'): // year 4 digits formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('D'): case ('x'): // datetime MM/DD/YY formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('m'): // month 1-12 formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('d'): // day of month 1-31 formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('H'): // hours 24 formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('I'): // hours 12 formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('M'): // minutes formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('S'): // seconds formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('e'): // milliseconds formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; break; case ('f'): // microseconds formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; break; case ('F'): // nanoseconds formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; break; case ('E'): // seconds since epoch formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; break; case ('p'): // am/pm formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('r'): // 12 hour clock 02:55:02 pm formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('R'): // 24-hour HH:MM time formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('T'): case ('X'): // ISO 8601 time format (HH:MM:SS) formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('z'): // timezone formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('P'): // pid formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; break; case ('^'): // color range start @@ -1282,22 +1278,22 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i case ('u'): // elapsed time since last log message in nanos formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('i'): // elapsed time since last log message in micros formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('o'): // elapsed time since last log message in millis formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; case ('O'): // elapsed time since last log message in seconds formatters_.push_back(details::make_unique>(padding)); - needs_time_ = true; + need_localtime_ = true; break; default: // Unknown flag appears as is diff --git a/include/spdlog/pattern_formatter.h b/include/spdlog/pattern_formatter.h index 57156cb7f..a1a64807f 100644 --- a/include/spdlog/pattern_formatter.h +++ b/include/spdlog/pattern_formatter.h @@ -103,7 +103,7 @@ class SPDLOG_API pattern_formatter final : public formatter std::string pattern_; std::string eol_; pattern_time_type pattern_time_type_; - bool needs_time_; + bool need_localtime_; std::tm cached_tm_; std::chrono::seconds last_log_secs_; std::vector> formatters_; From 28b9adf794ac9affe13402a54f7e9ea58ec4dfc6 Mon Sep 17 00:00:00 2001 From: doug1234 Date: Sat, 15 Jan 2022 16:41:06 -0500 Subject: [PATCH 5/5] Added the last few suggested changes. --- include/spdlog/pattern_formatter-inl.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index 587e50620..19d19e291 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -1278,22 +1278,18 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i case ('u'): // elapsed time since last log message in nanos formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; break; case ('i'): // elapsed time since last log message in micros formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; break; case ('o'): // elapsed time since last log message in millis formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; break; case ('O'): // elapsed time since last log message in seconds formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; break; default: // Unknown flag appears as is