diff --git a/example/Makefile.clang b/example/Makefile.clang index 78488446..475855db 100644 --- a/example/Makefile.clang +++ b/example/Makefile.clang @@ -4,26 +4,20 @@ CXX_RELEASE_FLAGS = -O2 CXX_DEBUG_FLAGS= -g -all: example bench -debug: example-debug bench-debug +all: example +debug: example-debug example: example.cpp $(CXX) example.cpp -o example-clang $(CXXFLAGS) $(CXX_RELEASE_FLAGS) -bench: bench.cpp - $(CXX) bench.cpp -o bench-clang $(CXXFLAGS) $(CXX_RELEASE_FLAGS) example-debug: example.cpp $(CXX) example.cpp -o example-clang-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) - -bench-debug: bench.cpp - $(CXX) bench.cpp -o bench-clang-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) - clean: - rm -f *.o logs/*.txt example-clang example-clang-debug bench-clang bench-clang-debug + rm -f *.o logs/*.txt example-clang example-clang-debug rebuild: clean all diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index 2ff58ca1..5052cc22 100644 --- a/include/spdlog/details/fmt_helper.h +++ b/include/spdlog/details/fmt_helper.h @@ -33,25 +33,7 @@ inline void append_int(T n, fmt::memory_buffer &dest) dest.append(i.data(), i.data() + i.size()); } - -inline void append_and_pad2(int n, fmt::memory_buffer &dest) -{ - if (n > 9) - { - append_int(n, dest); - return; - } - if (n >= 0) // 0-9 - { - dest.push_back('0'); - append_int(n, dest); - return; - } - // negatives (unlikely but just in case, let fmt deal with it) - fmt::format_to(dest, "{:02}", n); -} - -inline void append_and_pad3(int n, fmt::memory_buffer &dest) +inline void pad2(int n, fmt::memory_buffer &dest) { if (n > 99) { @@ -60,70 +42,43 @@ inline void append_and_pad3(int n, fmt::memory_buffer &dest) } if (n > 9) // 10-99 { - dest.push_back('0'); - dest.push_back('0' + n / 10); - dest.push_back('0' + n % 10); - return; - } - else if (n >= 0) - { - dest.push_back('0'); - dest.push_back('0'); - dest.push_back('0' + n); - return; - } - // negatives (unlikely, but just in case let fmt deal with it) - else - { - fmt::format_to(dest, "{:03}", n); + dest.push_back('0' + (n / 10)); + dest.push_back('0' + (n % 10)); return; } - + if (n >= 0) // 0-9 + { + dest.push_back('0'); + dest.push_back('0' + n); + return; + } + // negatives (unlikely, but just in case, let fmt deal with it) + fmt::format_to(dest, "{:02}", n); } -inline void append_and_pad6(int n, fmt::memory_buffer &dest) +inline void pad3(int n, fmt::memory_buffer &dest) { - if (n > 99999) + if (n > 99) { append_int(n, dest); return; } - if (n > 9999) + if (n > 9) // 10-99 { dest.push_back('0'); - } - else if (n > 999) - { - dest.push_back('0'); - dest.push_back('0'); - } - else if (n > 99) - { - dest.push_back('0'); - dest.push_back('0'); - dest.push_back('0'); - } - else if (n > 9) - { - dest.push_back('0'); - dest.push_back('0'); - dest.push_back('0'); - dest.push_back('0'); - } - else if (n >= 0) - { - dest.push_back('0'); - dest.push_back('0'); - dest.push_back('0'); - dest.push_back('0'); - dest.push_back('0'); - } - else // negatives (unlikely, but just in case let fmt deal with it) - { - fmt::format_to(dest, "{:06}", n); + dest.push_back('0' + n / 10); + dest.push_back('0' + n % 10); return; } - append_int(n, dest); + if (n >= 0) + { + dest.push_back('0'); + dest.push_back('0'); + dest.push_back('0' + n); + return; + } + // negatives (unlikely, but just in case let fmt deal with it) + fmt::format_to(dest, "{:03}", n); } } // namespace fmt_helper diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index b190e16c..278593b1 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -121,11 +121,11 @@ class c_formatter SPDLOG_FINAL : public flag_formatter void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { fmt::format_to(dest, "{} {} {} ", days[tm_time.tm_wday], months[tm_time.tm_mon], tm_time.tm_mday); // - fmt_helper::append_and_pad2(tm_time.tm_hour, dest); + fmt_helper::pad2(tm_time.tm_hour, dest); dest.push_back(':'); - fmt_helper::append_and_pad2(tm_time.tm_min, dest); + fmt_helper::pad2(tm_time.tm_min, dest); dest.push_back(':'); - fmt_helper::append_and_pad2(tm_time.tm_sec, dest); + fmt_helper::pad2(tm_time.tm_sec, dest); dest.push_back(' '); fmt_helper::append_int(tm_time.tm_year + 1900, dest); } @@ -136,7 +136,7 @@ class C_formatter SPDLOG_FINAL : public flag_formatter { void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { - fmt_helper::append_and_pad2(tm_time.tm_year % 100, dest); + fmt_helper::pad2(tm_time.tm_year % 100, dest); } }; @@ -145,11 +145,11 @@ class D_formatter SPDLOG_FINAL : public flag_formatter { void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { - fmt_helper::append_and_pad2(tm_time.tm_mon + 1, dest); + fmt_helper::pad2(tm_time.tm_mon + 1, dest); dest.push_back('/'); - fmt_helper::append_and_pad2(tm_time.tm_mday, dest); + fmt_helper::pad2(tm_time.tm_mday, dest); dest.push_back('/'); - fmt_helper::append_and_pad2(tm_time.tm_year % 100, dest); + fmt_helper::pad2(tm_time.tm_year % 100, dest); } }; @@ -167,7 +167,7 @@ class m_formatter SPDLOG_FINAL : public flag_formatter { void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { - fmt_helper::append_and_pad2(tm_time.tm_mon + 1, dest); + fmt_helper::pad2(tm_time.tm_mon + 1, dest); } }; @@ -177,7 +177,7 @@ class d_formatter SPDLOG_FINAL : public flag_formatter void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { // fmt::format_to(dest, "{:02}", tm_time.tm_mday); - fmt_helper::append_and_pad2(tm_time.tm_mday, dest); + fmt_helper::pad2(tm_time.tm_mday, dest); } }; @@ -186,7 +186,7 @@ class H_formatter SPDLOG_FINAL : public flag_formatter { void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { - fmt_helper::append_and_pad2(tm_time.tm_hour, dest); + fmt_helper::pad2(tm_time.tm_hour, dest); } }; @@ -195,7 +195,7 @@ class I_formatter SPDLOG_FINAL : public flag_formatter { void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { - fmt_helper::append_and_pad2(to12h(tm_time), dest); + fmt_helper::pad2(to12h(tm_time), dest); } }; @@ -204,7 +204,7 @@ class M_formatter SPDLOG_FINAL : public flag_formatter { void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { - fmt_helper::append_and_pad2(tm_time.tm_min, dest); + fmt_helper::pad2(tm_time.tm_min, dest); } }; @@ -213,7 +213,7 @@ class S_formatter SPDLOG_FINAL : public flag_formatter { void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { - fmt_helper::append_and_pad2(tm_time.tm_sec, dest); + fmt_helper::pad2(tm_time.tm_sec, dest); } }; @@ -224,7 +224,7 @@ class e_formatter SPDLOG_FINAL : public flag_formatter { auto duration = msg.time.time_since_epoch(); auto millis = std::chrono::duration_cast(duration).count() % 1000; - fmt_helper::append_and_pad3(static_cast(millis), dest); + fmt_helper::pad3(static_cast(millis), dest); } }; @@ -235,7 +235,7 @@ class f_formatter SPDLOG_FINAL : public flag_formatter { auto duration = msg.time.time_since_epoch(); auto micros = std::chrono::duration_cast(duration).count() % 1000000; - fmt_helper::append_and_pad6(static_cast(micros), dest); + fmt::format_to(dest, "{:06}", static_cast(micros)); } }; @@ -275,11 +275,11 @@ class r_formatter SPDLOG_FINAL : public flag_formatter void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { // fmt::format_to(dest, "{:02}:{:02}:{:02} {}", to12h(tm_time), tm_time.tm_min, tm_time.tm_sec, ampm(tm_time)); - fmt_helper::append_and_pad2(to12h(tm_time), dest); + fmt_helper::pad2(to12h(tm_time), dest); dest.push_back(':'); - fmt_helper::append_and_pad2(tm_time.tm_min, dest); + fmt_helper::pad2(tm_time.tm_min, dest); dest.push_back(':'); - fmt_helper::append_and_pad2(tm_time.tm_sec, dest); + fmt_helper::pad2(tm_time.tm_sec, dest); fmt::format_to(dest, " {}", ampm(tm_time)); } }; @@ -289,9 +289,9 @@ class R_formatter SPDLOG_FINAL : public flag_formatter { void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { - fmt_helper::append_and_pad2(tm_time.tm_hour, dest); + fmt_helper::pad2(tm_time.tm_hour, dest); dest.push_back(':'); - fmt_helper::append_and_pad2(tm_time.tm_min, dest); + fmt_helper::pad2(tm_time.tm_min, dest); } }; @@ -301,11 +301,11 @@ class T_formatter SPDLOG_FINAL : public flag_formatter void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { // fmt::format_to(dest, "{:02}:{:02}:{:02}", tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec); - fmt_helper::append_and_pad2(tm_time.tm_hour, dest); + fmt_helper::pad2(tm_time.tm_hour, dest); dest.push_back(':'); - fmt_helper::append_and_pad2(tm_time.tm_min, dest); + fmt_helper::pad2(tm_time.tm_min, dest); dest.push_back(':'); - fmt_helper::append_and_pad2(tm_time.tm_sec, dest); + fmt_helper::pad2(tm_time.tm_sec, dest); } }; @@ -344,9 +344,9 @@ public: int h = total_minutes / 60; int m = total_minutes % 60; dest.push_back(sign); - fmt_helper::append_and_pad2(h, dest); + fmt_helper::pad2(h, dest); dest.push_back(':'); - fmt_helper::append_and_pad2(m, dest); + fmt_helper::pad2(m, dest); } private: @@ -466,24 +466,24 @@ class full_formatter SPDLOG_FINAL : public flag_formatter auto seconds = std::chrono::duration_cast(duration).count(); if (cached_header_.size() == 0 || cached_seconds_ts_ != seconds) { - cached_header_ = std::move(fmt::memory_buffer()); + cached_header_ = fmt::memory_buffer(); cached_header_.push_back('['); fmt_helper::append_int(tm_time.tm_year + 1900, cached_header_); cached_header_.push_back('-'); - fmt_helper::append_and_pad2(tm_time.tm_mon + 1, cached_header_); + fmt_helper::pad2(tm_time.tm_mon + 1, cached_header_); cached_header_.push_back('-'); - fmt_helper::append_and_pad2(tm_time.tm_mday, cached_header_); + fmt_helper::pad2(tm_time.tm_mday, cached_header_); cached_header_.push_back(' '); - fmt_helper::append_and_pad2(tm_time.tm_hour, cached_header_); + fmt_helper::pad2(tm_time.tm_hour, cached_header_); cached_header_.push_back(':'); - fmt_helper::append_and_pad2(tm_time.tm_min, cached_header_); + fmt_helper::pad2(tm_time.tm_min, cached_header_); cached_header_.push_back(':'); - fmt_helper::append_and_pad2(tm_time.tm_sec, cached_header_); + fmt_helper::pad2(tm_time.tm_sec, cached_header_); cached_header_.push_back('.'); cached_seconds_ts_ = seconds; @@ -491,7 +491,7 @@ class full_formatter SPDLOG_FINAL : public flag_formatter fmt_helper::append_buf(cached_header_, dest); auto millis = std::chrono::duration_cast(duration).count() % 1000; - fmt_helper::append_and_pad3(static_cast(millis), dest); + fmt_helper::pad3(static_cast(millis), dest); dest.push_back(']'); dest.push_back(' '); #else // no datetime needed @@ -530,7 +530,9 @@ public: std::string eol = spdlog::details::os::default_eol) : eol_(std::move(eol)) , pattern_time_(pattern_time) + , last_log_secs_(0) { + std::memset(&cached_tm_, 0, sizeof(cached_tm_)); compile_pattern(pattern); } @@ -557,7 +559,7 @@ public: private: const std::string eol_; const pattern_time_type pattern_time_; - std::tm cached_tm_{}; + std::tm cached_tm_; std::chrono::seconds last_log_secs_; std::vector> formatters_; @@ -569,6 +571,7 @@ private: } return details::os::gmtime(log_clock::to_time_t(msg.time)); } + void handle_flag(char flag) { switch (flag) diff --git a/include/spdlog/sinks/sink.h b/include/spdlog/sinks/sink.h index 307afe21..513e0661 100644 --- a/include/spdlog/sinks/sink.h +++ b/include/spdlog/sinks/sink.h @@ -48,7 +48,7 @@ public: return static_cast(level_.load(std::memory_order_relaxed)); } - void set_pattern(const std::string& pattern) + void set_pattern(const std::string &pattern) { formatter_ = std::unique_ptr(new pattern_formatter(pattern)); } diff --git a/include/spdlog/sinks/stdout_color_sinks.h b/include/spdlog/sinks/stdout_color_sinks.h index 25aeedee..761e32a4 100644 --- a/include/spdlog/sinks/stdout_color_sinks.h +++ b/include/spdlog/sinks/stdout_color_sinks.h @@ -27,23 +27,27 @@ using stderr_color_sink_st = ansicolor_stderr_sink_st; #endif } // namespace sinks - template - inline std::shared_ptr stdout_color_mt(const std::string &logger_name) { - return Factory::template create(logger_name); - } +template +inline std::shared_ptr stdout_color_mt(const std::string &logger_name) +{ + return Factory::template create(logger_name); +} - template - inline std::shared_ptr stdout_color_st(const std::string &logger_name) { - return Factory::template create(logger_name); - } +template +inline std::shared_ptr stdout_color_st(const std::string &logger_name) +{ + return Factory::template create(logger_name); +} - template - inline std::shared_ptr stderr_color_mt(const std::string &logger_name) { - return Factory::template create(logger_name); - } +template +inline std::shared_ptr stderr_color_mt(const std::string &logger_name) +{ + return Factory::template create(logger_name); +} - template - inline std::shared_ptr stderr_color_st(const std::string &logger_name) { - return Factory::template create(logger_name); - } +template +inline std::shared_ptr stderr_color_st(const std::string &logger_name) +{ + return Factory::template create(logger_name); +} } // namespace spdlog