From 5c5080d304cf85d14c710b166299e1d7be249eb7 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Tue, 30 May 2017 18:05:25 -0400 Subject: [PATCH 1/4] implement a flag (in this case, an enumeration) that allows control over the type; we make it an enum for possible expansions of time abstractions that might make it into the C++ standard in the future (see Howard Hinnant's date/timezone library) or might be usefully-available from the OS at some point in time --- include/spdlog/async_logger.h | 2 +- include/spdlog/common.h | 9 +++++ include/spdlog/details/async_logger_impl.h | 4 +-- include/spdlog/details/logger_impl.h | 8 ++--- .../spdlog/details/pattern_formatter_impl.h | 34 +++++++++++++++---- include/spdlog/formatter.h | 5 +-- include/spdlog/logger.h | 4 +-- 7 files changed, 49 insertions(+), 17 deletions(-) diff --git a/include/spdlog/async_logger.h b/include/spdlog/async_logger.h index 8b492657..16e7aef8 100644 --- a/include/spdlog/async_logger.h +++ b/include/spdlog/async_logger.h @@ -71,7 +71,7 @@ public: protected: void _sink_it(details::log_msg& msg) override; void _set_formatter(spdlog::formatter_ptr msg_formatter) override; - void _set_pattern(const std::string& pattern) override; + void _set_pattern(const std::string& pattern, pattern_time ptime) override; private: std::unique_ptr _async_log_helper; diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 14d5d149..d56b6777 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -106,6 +106,15 @@ enum class async_overflow_policy discard_log_msg // Discard the message it enqueue fails }; +// +// Pattern time - specific time getting to use for pattern_formatter. +// local time by default +// +enum class pattern_time +{ + local, // use variant of time similar to std::localtime for std::tm + utc // use variant of time similar to std::gmtime for std::tm +}; // // Log exception diff --git a/include/spdlog/details/async_logger_impl.h b/include/spdlog/details/async_logger_impl.h index 0b815f01..225f7350 100644 --- a/include/spdlog/details/async_logger_impl.h +++ b/include/spdlog/details/async_logger_impl.h @@ -76,9 +76,9 @@ inline void spdlog::async_logger::_set_formatter(spdlog::formatter_ptr msg_forma _async_log_helper->set_formatter(_formatter); } -inline void spdlog::async_logger::_set_pattern(const std::string& pattern) +inline void spdlog::async_logger::_set_pattern(const std::string& pattern, pattern_time ptime) { - _formatter = std::make_shared(pattern); + _formatter = std::make_shared(pattern, ptime); _async_log_helper->set_formatter(_formatter); } diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index a196a957..7c3584f2 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -53,9 +53,9 @@ inline void spdlog::logger::set_formatter(spdlog::formatter_ptr msg_formatter) _set_formatter(msg_formatter); } -inline void spdlog::logger::set_pattern(const std::string& pattern) +inline void spdlog::logger::set_pattern(const std::string& pattern, pattern_time ptime) { - _set_pattern(pattern); + _set_pattern(pattern, ptime); } @@ -316,9 +316,9 @@ inline void spdlog::logger::_sink_it(details::log_msg& msg) flush(); } -inline void spdlog::logger::_set_pattern(const std::string& pattern) +inline void spdlog::logger::_set_pattern(const std::string& pattern, pattern_time ptime) { - _formatter = std::make_shared(pattern); + _formatter = std::make_shared(pattern, ptime); } inline void spdlog::logger::_set_formatter(formatter_ptr msg_formatter) { diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index 8c096150..55b3912a 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -311,7 +311,15 @@ class R_formatter SPDLOG_FINAL:public flag_formatter { void format(details::log_msg& msg, const std::tm& tm_time) override { - pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, ':'); + msg.formatted << (tm_time.tm_year + 1900); + msg.formatted << '-'; + msg.formatted << tm_time.tm_mon; + msg.formatted << '-'; + msg.formatted << tm_time.tm_mday; + + msg.formatted << ' '; + + pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, ':'); } }; @@ -324,7 +332,6 @@ class T_formatter SPDLOG_FINAL:public flag_formatter } }; - // ISO 8601 offset from UTC in timezone (+-HH:MM) class z_formatter SPDLOG_FINAL:public flag_formatter { @@ -495,12 +502,13 @@ class full_formatter SPDLOG_FINAL:public flag_formatter /////////////////////////////////////////////////////////////////////////////// // pattern_formatter inline impl /////////////////////////////////////////////////////////////////////////////// -inline spdlog::pattern_formatter::pattern_formatter(const std::string& pattern) +inline spdlog::pattern_formatter::pattern_formatter(const std::string& pattern, pattern_time ptime) +: _time(ptime), _pattern(pattern) { - compile_pattern(pattern); + compile_pattern(_pattern, _time); } -inline void spdlog::pattern_formatter::compile_pattern(const std::string& pattern) +inline void spdlog::pattern_formatter::compile_pattern(const std::string& pattern, pattern_time ptime) { auto end = pattern.end(); std::unique_ptr user_chars; @@ -670,7 +678,21 @@ inline void spdlog::pattern_formatter::format(details::log_msg& msg) { #ifndef SPDLOG_NO_DATETIME - auto tm_time = details::os::localtime(log_clock::to_time_t(msg.time)); + auto tm_time = [this, &msg]() + { + switch (_time) + { + // it is always faster to put the most-common/default case first + case (pattern_time::local): + return details::os::localtime(log_clock::to_time_t(msg.time)); + + case (pattern_time::utc): + return details::os::gmtime(log_clock::to_time_t(msg.time)); + + default: + return details::os::localtime(log_clock::to_time_t(msg.time)); + } + }(); #else std::tm tm_time; #endif diff --git a/include/spdlog/formatter.h b/include/spdlog/formatter.h index 337ca596..b2b5a5ca 100644 --- a/include/spdlog/formatter.h +++ b/include/spdlog/formatter.h @@ -29,15 +29,16 @@ class pattern_formatter SPDLOG_FINAL : public formatter { public: - explicit pattern_formatter(const std::string& pattern); + explicit pattern_formatter(const std::string& pattern, pattern_time ptime = pattern_time::local); pattern_formatter(const pattern_formatter&) = delete; pattern_formatter& operator=(const pattern_formatter&) = delete; void format(details::log_msg& msg) override; private: const std::string _pattern; + const pattern_time _time; std::vector> _formatters; void handle_flag(char flag); - void compile_pattern(const std::string& pattern); + void compile_pattern(const std::string& pattern, pattern_time ptime); }; } diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index da2d8e41..e1762aa9 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -66,7 +66,7 @@ public: void set_level(level::level_enum); level::level_enum level() const; const std::string& name() const; - void set_pattern(const std::string&); + void set_pattern(const std::string&, pattern_time = pattern_time::local); void set_formatter(formatter_ptr); // automatically call flush() if message level >= log_level @@ -82,7 +82,7 @@ public: protected: virtual void _sink_it(details::log_msg&); - virtual void _set_pattern(const std::string&); + virtual void _set_pattern(const std::string&, pattern_time); virtual void _set_formatter(formatter_ptr); // default error handler: print the error to stderr with the max rate of 1 message/minute From 18a0455b9184187bdb4ed60092a1872b778ded9b Mon Sep 17 00:00:00 2001 From: ThePhD Date: Tue, 30 May 2017 18:15:30 -0400 Subject: [PATCH 2/4] _pattern was never set... but it was also never used. Intentional by @gabime, or a reflection of a refactor with some data member left behind? --- include/spdlog/details/pattern_formatter_impl.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index 55b3912a..38c03fa4 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -503,9 +503,9 @@ class full_formatter SPDLOG_FINAL:public flag_formatter // pattern_formatter inline impl /////////////////////////////////////////////////////////////////////////////// inline spdlog::pattern_formatter::pattern_formatter(const std::string& pattern, pattern_time ptime) -: _time(ptime), _pattern(pattern) +: _time(ptime) { - compile_pattern(_pattern, _time); + compile_pattern(pattern, _time); } inline void spdlog::pattern_formatter::compile_pattern(const std::string& pattern, pattern_time ptime) From d98d54896b1be50cbbf58e2310df65d96f65c970 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Wed, 31 May 2017 12:52:12 -0400 Subject: [PATCH 3/4] use if statement instead of switch (changes of adding new time specifications outside of standard are probably unlikely anyhow) pattern_time -> pattern_time_type ptime variable name -> pattern_time variable name make sure four spaces used, not tabs --- include/spdlog/async_logger.h | 2 +- include/spdlog/common.h | 6 +-- include/spdlog/details/async_logger_impl.h | 4 +- include/spdlog/details/logger_impl.h | 8 ++-- .../spdlog/details/pattern_formatter_impl.h | 40 ++++++------------- include/spdlog/formatter.h | 7 ++-- include/spdlog/logger.h | 4 +- 7 files changed, 28 insertions(+), 43 deletions(-) diff --git a/include/spdlog/async_logger.h b/include/spdlog/async_logger.h index 16e7aef8..9d7e08fa 100644 --- a/include/spdlog/async_logger.h +++ b/include/spdlog/async_logger.h @@ -71,7 +71,7 @@ public: protected: void _sink_it(details::log_msg& msg) override; void _set_formatter(spdlog::formatter_ptr msg_formatter) override; - void _set_pattern(const std::string& pattern, pattern_time ptime) override; + void _set_pattern(const std::string& pattern, pattern_time_type pattern_time) override; private: std::unique_ptr _async_log_helper; diff --git a/include/spdlog/common.h b/include/spdlog/common.h index d56b6777..994e847d 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -110,10 +110,10 @@ enum class async_overflow_policy // Pattern time - specific time getting to use for pattern_formatter. // local time by default // -enum class pattern_time +enum class pattern_time_type { - local, // use variant of time similar to std::localtime for std::tm - utc // use variant of time similar to std::gmtime for std::tm + local, // use variant of time similar to std::localtime for std::tm + utc // use variant of time similar to std::gmtime for std::tm }; // diff --git a/include/spdlog/details/async_logger_impl.h b/include/spdlog/details/async_logger_impl.h index 225f7350..6822cd5b 100644 --- a/include/spdlog/details/async_logger_impl.h +++ b/include/spdlog/details/async_logger_impl.h @@ -76,9 +76,9 @@ inline void spdlog::async_logger::_set_formatter(spdlog::formatter_ptr msg_forma _async_log_helper->set_formatter(_formatter); } -inline void spdlog::async_logger::_set_pattern(const std::string& pattern, pattern_time ptime) +inline void spdlog::async_logger::_set_pattern(const std::string& pattern, pattern_time_type pattern_time) { - _formatter = std::make_shared(pattern, ptime); + _formatter = std::make_shared(pattern, pattern_time); _async_log_helper->set_formatter(_formatter); } diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 7c3584f2..c6aa125b 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -53,9 +53,9 @@ inline void spdlog::logger::set_formatter(spdlog::formatter_ptr msg_formatter) _set_formatter(msg_formatter); } -inline void spdlog::logger::set_pattern(const std::string& pattern, pattern_time ptime) +inline void spdlog::logger::set_pattern(const std::string& pattern, pattern_time_type pattern_time) { - _set_pattern(pattern, ptime); + _set_pattern(pattern, pattern_time); } @@ -316,9 +316,9 @@ inline void spdlog::logger::_sink_it(details::log_msg& msg) flush(); } -inline void spdlog::logger::_set_pattern(const std::string& pattern, pattern_time ptime) +inline void spdlog::logger::_set_pattern(const std::string& pattern, pattern_time_type pattern_time) { - _formatter = std::make_shared(pattern, ptime); + _formatter = std::make_shared(pattern, pattern_time); } inline void spdlog::logger::_set_formatter(formatter_ptr msg_formatter) { diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index 38c03fa4..f17f90f8 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -311,15 +311,7 @@ class R_formatter SPDLOG_FINAL:public flag_formatter { void format(details::log_msg& msg, const std::tm& tm_time) override { - msg.formatted << (tm_time.tm_year + 1900); - msg.formatted << '-'; - msg.formatted << tm_time.tm_mon; - msg.formatted << '-'; - msg.formatted << tm_time.tm_mday; - - msg.formatted << ' '; - - pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, ':'); + pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, ':'); } }; @@ -502,13 +494,13 @@ class full_formatter SPDLOG_FINAL:public flag_formatter /////////////////////////////////////////////////////////////////////////////// // pattern_formatter inline impl /////////////////////////////////////////////////////////////////////////////// -inline spdlog::pattern_formatter::pattern_formatter(const std::string& pattern, pattern_time ptime) -: _time(ptime) +inline spdlog::pattern_formatter::pattern_formatter(const std::string& pattern, pattern_time_type pattern_time) +: _pattern_time(pattern_time) { - compile_pattern(pattern, _time); + compile_pattern(pattern); } -inline void spdlog::pattern_formatter::compile_pattern(const std::string& pattern, pattern_time ptime) +inline void spdlog::pattern_formatter::compile_pattern(const std::string& pattern) { auto end = pattern.end(); std::unique_ptr user_chars; @@ -673,26 +665,18 @@ inline void spdlog::pattern_formatter::handle_flag(char flag) } } +inline std::tm spdlog::pattern_formatter::get_time(details::log_msg& msg) { + if (_pattern_time == pattern_time_type::local) + return details::os::localtime(log_clock::to_time_t(msg.time)); + else + return details::os::gmtime(log_clock::to_time_t(msg.time)); +} inline void spdlog::pattern_formatter::format(details::log_msg& msg) { #ifndef SPDLOG_NO_DATETIME - auto tm_time = [this, &msg]() - { - switch (_time) - { - // it is always faster to put the most-common/default case first - case (pattern_time::local): - return details::os::localtime(log_clock::to_time_t(msg.time)); - - case (pattern_time::utc): - return details::os::gmtime(log_clock::to_time_t(msg.time)); - - default: - return details::os::localtime(log_clock::to_time_t(msg.time)); - } - }(); + auto tm_time = get_time(msg); #else std::tm tm_time; #endif diff --git a/include/spdlog/formatter.h b/include/spdlog/formatter.h index b2b5a5ca..6bba9025 100644 --- a/include/spdlog/formatter.h +++ b/include/spdlog/formatter.h @@ -29,16 +29,17 @@ class pattern_formatter SPDLOG_FINAL : public formatter { public: - explicit pattern_formatter(const std::string& pattern, pattern_time ptime = pattern_time::local); + explicit pattern_formatter(const std::string& pattern, pattern_time_type pattern_time = pattern_time_type::local); pattern_formatter(const pattern_formatter&) = delete; pattern_formatter& operator=(const pattern_formatter&) = delete; void format(details::log_msg& msg) override; private: const std::string _pattern; - const pattern_time _time; + const pattern_time_type _pattern_time; std::vector> _formatters; + std::tm get_time(details::log_msg& msg); void handle_flag(char flag); - void compile_pattern(const std::string& pattern, pattern_time ptime); + void compile_pattern(const std::string& pattern); }; } diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index e1762aa9..fce99f9a 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -66,7 +66,7 @@ public: void set_level(level::level_enum); level::level_enum level() const; const std::string& name() const; - void set_pattern(const std::string&, pattern_time = pattern_time::local); + void set_pattern(const std::string&, pattern_time_type = pattern_time_type::local); void set_formatter(formatter_ptr); // automatically call flush() if message level >= log_level @@ -82,7 +82,7 @@ public: protected: virtual void _sink_it(details::log_msg&); - virtual void _set_pattern(const std::string&, pattern_time); + virtual void _set_pattern(const std::string&, pattern_time_type); virtual void _set_formatter(formatter_ptr); // default error handler: print the error to stderr with the max rate of 1 message/minute From 7e09f018477b73f0a75c8c9c21302f2db8776892 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Wed, 31 May 2017 13:12:21 -0400 Subject: [PATCH 4/4] brace styling --- include/spdlog/details/pattern_formatter_impl.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index f17f90f8..81835618 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -665,7 +665,8 @@ inline void spdlog::pattern_formatter::handle_flag(char flag) } } -inline std::tm spdlog::pattern_formatter::get_time(details::log_msg& msg) { +inline std::tm spdlog::pattern_formatter::get_time(details::log_msg& msg) +{ if (_pattern_time == pattern_time_type::local) return details::os::localtime(log_clock::to_time_t(msg.time)); else