diff --git a/bench/formatter-bench.cpp b/bench/formatter-bench.cpp index 1454c6bb..8302acf9 100644 --- a/bench/formatter-bench.cpp +++ b/bench/formatter-bench.cpp @@ -10,7 +10,15 @@ void bench_formatter(benchmark::State &state, std::string pattern) { - auto formatter = spdlog::details::make_unique(pattern); + std::unique_ptr formatter; + if(pattern == "%+") + { + formatter = spdlog::details::make_unique(); + } + else { + formatter = spdlog::details::make_unique(pattern); + } + spdlog::memory_buf_t dest; std::string logger_name = "logger-name"; const char *text = "Hello. This is some message with length of 80 "; diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index c55b5eea..1994ed76 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -31,7 +31,7 @@ namespace spdlog { namespace details { SPDLOG_INLINE registry::registry() - : formatter_(new pattern_formatter()) + : formatter_(new default_formatter()) { #ifndef SPDLOG_DISABLE_DEFAULT_LOGGER diff --git a/include/spdlog/pattern_formatter.h b/include/spdlog/pattern_formatter.h index e9ccfaa7..ec060d0a 100644 --- a/include/spdlog/pattern_formatter.h +++ b/include/spdlog/pattern_formatter.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -119,6 +120,110 @@ private: void compile_pattern_(const std::string &pattern); }; + +// Full info formatter +// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v +class default_formatter final : public formatter +{ +public: + default_formatter() = default; + ~default_formatter() = default; + + std::unique_ptr clone() const override + { + return details::make_unique(); + } + + void format(const details::log_msg &msg, memory_buf_t &dest) override + { + // 30ns + using std::chrono::duration_cast; + using std::chrono::milliseconds; + using std::chrono::seconds; + // cache the date/time part for the next second. + auto duration = msg.time.time_since_epoch(); + auto secs = duration_cast(duration); + //[2021-08-23 00:57:12.310] [info] Welcome to spdlog version 1.9.2 ! + if (cache_timestamp_ != secs || cached_datetime_.size() == 0) + { + auto tm_time = details::os::localtime(log_clock::to_time_t(msg.time)); + cached_datetime_.clear(); + cached_datetime_.push_back('['); + details::fmt_helper::append_int(tm_time.tm_year + 1900, cached_datetime_); + cached_datetime_.push_back('-'); + + details::fmt_helper::pad2(tm_time.tm_mon + 1, cached_datetime_); + cached_datetime_.push_back('-'); + + details::fmt_helper::pad2(tm_time.tm_mday, cached_datetime_); + cached_datetime_.push_back(' '); + + details::fmt_helper::pad2(tm_time.tm_hour, cached_datetime_); + cached_datetime_.push_back(':'); + + details::fmt_helper::pad2(tm_time.tm_min, cached_datetime_); + cached_datetime_.push_back(':'); + + details::fmt_helper::pad2(tm_time.tm_sec, cached_datetime_); + cached_datetime_.push_back('.'); + + cache_timestamp_ = secs; + } + // 32ns + + dest.append(cached_datetime_.begin(), cached_datetime_.end()); + // 36ns + auto millis = details::fmt_helper::time_fraction(msg.time); + // 40ns + + details::fmt_helper::pad3(static_cast(millis.count()), dest); + + // 45ns + + dest.push_back(']'); + dest.push_back(' '); + // 49ns + + // append logger name if exists + if (msg.logger_name.size() > 0) + { + dest.push_back('['); + details::fmt_helper::append_string_view(msg.logger_name, dest); + dest.push_back(']'); + dest.push_back(' '); + } + // 56ns + + dest.push_back('['); + // wrap the level name with color + msg.color_range_start = dest.size(); + // fmt_helper::append_string_view(level::to_c_str(msg.level), dest); + details::fmt_helper::append_string_view(level::to_string_view(msg.level), dest); + msg.color_range_end = dest.size(); + dest.push_back(']'); + dest.push_back(' '); + + // add source location if present + + if (!msg.source.empty()) + { + dest.push_back('['); + // const char *filename = details::short_filename_formatter::basename(msg.source.filename); + // details::fmt_helper::append_string_view(filename, dest); + dest.push_back(':'); + details::fmt_helper::append_int(msg.source.line, dest); + dest.push_back(']'); + dest.push_back(' '); + } + details::fmt_helper::append_string_view(msg.payload, dest); + details::fmt_helper::append_string_view(details::os::default_eol, dest); + } + +private: + std::chrono::seconds cache_timestamp_{0}; + memory_buf_t cached_datetime_; +}; + } // namespace spdlog #ifdef SPDLOG_HEADER_ONLY diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h index d8db423c..3b19abd1 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -17,7 +17,7 @@ template SPDLOG_INLINE ansicolor_sink::ansicolor_sink(FILE *target_file, color_mode mode) : target_file_(target_file) , mutex_(ConsoleMutex::mutex()) - , formatter_(details::make_unique()) + , formatter_(details::make_unique()) { set_color_mode(mode); diff --git a/include/spdlog/sinks/base_sink-inl.h b/include/spdlog/sinks/base_sink-inl.h index 421fdf9d..63380c20 100644 --- a/include/spdlog/sinks/base_sink-inl.h +++ b/include/spdlog/sinks/base_sink-inl.h @@ -14,7 +14,7 @@ template SPDLOG_INLINE spdlog::sinks::base_sink::base_sink() - : formatter_{details::make_unique()} + : formatter_{details::make_unique()} {} template