From a626ebbbec9074cc6f9dde91d10e5683e8997d47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Wed, 15 Feb 2017 14:31:51 +0100 Subject: [PATCH 1/3] Replace static global std::string arrays by Meyer singletons. This improves thread-safety. --- .../spdlog/details/pattern_formatter_impl.h | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index 3f4e9a2c..c170324c 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -18,6 +18,7 @@ #include #include #include +#include namespace spdlog { @@ -78,42 +79,54 @@ static int to12h(const tm& t) } //Abbreviated weekday name -static const std::string days[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; +static const auto& days() { + static std::array arr{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; + return arr; +} class a_formatter:public flag_formatter { void format(details::log_msg& msg, const std::tm& tm_time) override { - msg.formatted << days[tm_time.tm_wday]; + msg.formatted << days()[tm_time.tm_wday]; } }; //Full weekday name -static const std::string full_days[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; +static const auto& full_days() { + static std::array arr{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; + return arr; +} class A_formatter:public flag_formatter { void format(details::log_msg& msg, const std::tm& tm_time) override { - msg.formatted << full_days[tm_time.tm_wday]; + msg.formatted << full_days()[tm_time.tm_wday]; } }; //Abbreviated month -static const std::string months[] { "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" }; +static const auto& months() { + static std::array arr{ "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" }; + return arr; +} class b_formatter:public flag_formatter { void format(details::log_msg& msg, const std::tm& tm_time) override { - msg.formatted << months[tm_time.tm_mon]; + msg.formatted << months()[tm_time.tm_mon]; } }; //Full month name -static const std::string full_months[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; +static const auto& full_months() { + static std::array arr{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; + return arr; +} class B_formatter:public flag_formatter { void format(details::log_msg& msg, const std::tm& tm_time) override { - msg.formatted << full_months[tm_time.tm_mon]; + msg.formatted << full_months()[tm_time.tm_mon]; } }; @@ -138,7 +151,7 @@ class c_formatter:public flag_formatter { void format(details::log_msg& msg, const std::tm& tm_time) override { - msg.formatted << days[tm_time.tm_wday] << ' ' << months[tm_time.tm_mon] << ' ' << tm_time.tm_mday << ' '; + msg.formatted << days()[tm_time.tm_wday] << ' ' << months()[tm_time.tm_mon] << ' ' << tm_time.tm_mday << ' '; pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << tm_time.tm_year + 1900; } }; From 06b8193a141588b32e0606574ba3e2bb7733d58a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Wed, 15 Feb 2017 14:41:57 +0100 Subject: [PATCH 2/3] Add missing consts --- include/spdlog/details/pattern_formatter_impl.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index c170324c..95082022 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -80,7 +80,7 @@ static int to12h(const tm& t) //Abbreviated weekday name static const auto& days() { - static std::array arr{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; + static const std::array arr{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; return arr; } class a_formatter:public flag_formatter @@ -93,7 +93,7 @@ class a_formatter:public flag_formatter //Full weekday name static const auto& full_days() { - static std::array arr{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; + static const std::array arr{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; return arr; } class A_formatter:public flag_formatter @@ -106,7 +106,7 @@ class A_formatter:public flag_formatter //Abbreviated month static const auto& months() { - static std::array arr{ "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" }; + static const std::array arr{ "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" }; return arr; } class b_formatter:public flag_formatter @@ -119,7 +119,7 @@ class b_formatter:public flag_formatter //Full month name static const auto& full_months() { - static std::array arr{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; + static const std::array arr{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; return arr; } class B_formatter:public flag_formatter From ef665e959f35e5c4a26217f13501dad7cdf8e741 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Thu, 16 Feb 2017 13:17:13 +0100 Subject: [PATCH 3/3] Don't deduce return types for days / months Required for C++11 support --- .../spdlog/details/pattern_formatter_impl.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index 95082022..07895485 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -79,8 +79,9 @@ static int to12h(const tm& t) } //Abbreviated weekday name -static const auto& days() { - static const std::array arr{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; +using days_array = std::array; +static const days_array& days() { + static const days_array arr{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; return arr; } class a_formatter:public flag_formatter @@ -92,8 +93,8 @@ class a_formatter:public flag_formatter }; //Full weekday name -static const auto& full_days() { - static const std::array arr{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; +static const days_array& full_days() { + static const days_array arr{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; return arr; } class A_formatter:public flag_formatter @@ -105,8 +106,9 @@ class A_formatter:public flag_formatter }; //Abbreviated month -static const auto& months() { - static const std::array arr{ "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" }; +using months_array = std::array; +static const months_array& months() { + static const months_array arr{ "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" }; return arr; } class b_formatter:public flag_formatter @@ -118,8 +120,8 @@ class b_formatter:public flag_formatter }; //Full month name -static const auto& full_months() { - static const std::array arr{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; +static const months_array& full_months() { + static const months_array arr{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; return arr; } class B_formatter:public flag_formatter