diff --git a/include/spdlog/details/pattern_formatter-inl.h b/include/spdlog/details/pattern_formatter-inl.h index 14d93f08..b0cc2669 100644 --- a/include/spdlog/details/pattern_formatter-inl.h +++ b/include/spdlog/details/pattern_formatter-inl.h @@ -888,6 +888,40 @@ public: } }; +// print elapsed time since last message +template +class elapsed_formatter final : public flag_formatter +{ + +public: + using DurationUnits = Units; + + explicit elapsed_formatter(padding_info padinfo) + : flag_formatter(padinfo), + last_message_time_(log_clock::now()) + {} + + void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + { + auto delta = msg.time - last_message_time_; + auto delta_units = std::chrono::duration_cast(delta); + last_message_time_ = msg.time; + if (padinfo_.enabled()) + { + scoped_pad p(6, padinfo_, dest); + fmt_helper::pad6(static_cast(delta_units.count()), dest); + } + else + { + fmt_helper::pad6(static_cast(delta_units.count()), dest); + } + } + +protected: + log_clock::time_point last_message_time_; + +}; + // Full info formatter // pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v class full_formatter final : public flag_formatter @@ -1199,6 +1233,22 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i formatters_.push_back(details::make_unique('%')); break; + case ('u'): // elapsed time since last log message in nanos + formatters_.push_back(details::make_unique>(padding)); + break; + + case ('i'): // elapsed time since last log message in micros + formatters_.push_back(details::make_unique>(padding)); + break; + + case ('o'): // elapsed time since last log message in millis + formatters_.push_back(details::make_unique>(padding)); + break; + + case ('O'): // elapsed time since last log message in seconds + formatters_.push_back(details::make_unique>(padding)); + break; + default: // Unknown flag appears as is auto unknown_flag = details::make_unique(); unknown_flag->add_ch('%');