diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index ec727032..19d19e29 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)) + , need_localtime_(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) + , need_localtime_(true) { std::memset(&cached_tm_, 0, sizeof(cached_tm_)); formatters_.push_back(details::make_unique(details::padding_info{})); @@ -1049,11 +1051,14 @@ 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_) + if (need_localtime_) { - cached_tm_ = get_time_(msg); - last_log_secs_ = secs; + 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_) @@ -1067,6 +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); + need_localtime_ = false; compile_pattern_(pattern_); } @@ -1097,6 +1103,7 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i { case ('+'): // default formatter formatters_.push_back(details::make_unique(padding)); + need_localtime_ = true; break; case 'n': // logger name @@ -1121,60 +1128,74 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i case ('a'): // weekday formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('A'): // short weekday formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('b'): case ('h'): // month formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('B'): // short month formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('c'): // datetime formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('C'): // year 2 digits formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('Y'): // year 4 digits formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('D'): case ('x'): // datetime MM/DD/YY formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('m'): // month 1-12 formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('d'): // day of month 1-31 formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('H'): // hours 24 formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('I'): // hours 12 formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('M'): // minutes formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('S'): // seconds formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('e'): // milliseconds @@ -1195,23 +1216,28 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i case ('p'): // am/pm formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('r'): // 12 hour clock 02:55:02 pm formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('R'): // 24-hour HH:MM time formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('T'): case ('X'): // ISO 8601 time format (HH:MM:SS) formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('z'): // timezone formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; break; case ('P'): // pid diff --git a/include/spdlog/pattern_formatter.h b/include/spdlog/pattern_formatter.h index 6810f03b..a1a64807 100644 --- a/include/spdlog/pattern_formatter.h +++ b/include/spdlog/pattern_formatter.h @@ -103,6 +103,7 @@ private: std::string pattern_; std::string eol_; pattern_time_type pattern_time_type_; + bool need_localtime_; std::tm cached_tm_; std::chrono::seconds last_log_secs_; std::vector> formatters_;