From e148b939fdb512388a64d918b16d27af76bd902a Mon Sep 17 00:00:00 2001 From: gabi Date: Sun, 19 Oct 2014 17:44:57 +0300 Subject: [PATCH] utc offset support (%z) in pattern formatter --- include/c11log/details/os.h | 35 ++++-- include/c11log/details/pattern_formatter.h | 118 ++++++++++++++------- 2 files changed, 107 insertions(+), 46 deletions(-) diff --git a/include/c11log/details/os.h b/include/c11log/details/os.h index 11823644..71a6c0c1 100644 --- a/include/c11log/details/os.h +++ b/include/c11log/details/os.h @@ -33,6 +33,25 @@ inline std::tm localtime() return localtime(now_t); } + +inline std::tm gmtime(const std::time_t &time_tt) +{ + +#ifdef _WIN32 + std::tm tm; + gmtime_s(&tm, &time_tt); +#else + std::tm tm; + lgmtime_r(&time_tt, &tm); +#endif + return tm; +} + +inline std::tm gmtime() +{ + std::time_t now_t = time(0); + return gmtime(now_t); +} inline bool operator==(const std::tm& tm1, const std::tm& tm2) { return (tm1.tm_sec == tm2.tm_sec && @@ -46,7 +65,7 @@ inline bool operator==(const std::tm& tm1, const std::tm& tm2) inline bool operator!=(const std::tm& tm1, const std::tm& tm2) { - return !(tm1==tm2); + return !(tm1 == tm2); } #ifdef _WIN32 @@ -84,14 +103,18 @@ inline bool fopen_s(FILE** fp, const std::string& filename, const char* mode) #endif } -inline float tz_offset() +//Return utc offset in minutes or -1 on failure +inline int utc_minutes_offset(const std::tm& tm = localtime()) { + #ifdef _WIN32 - TIME_ZONE_INFORMATION tzinfo; - GetTimeZoneInformation(&tzinfo); - return tzinfo.Bias / -60.0f; + DYNAMIC_TIME_ZONE_INFORMATION tzinfo; + auto rv = GetDynamicTimeZoneInformation(&tzinfo); + if (!rv) + return -1; + return -1 * (tzinfo.Bias + tzinfo.DaylightBias); #else - return 0.0f; + return tm.tm_gmtoff / 60; #endif } diff --git a/include/c11log/details/pattern_formatter.h b/include/c11log/details/pattern_formatter.h index 5d4c4f92..2578b0fe 100644 --- a/include/c11log/details/pattern_formatter.h +++ b/include/c11log/details/pattern_formatter.h @@ -9,7 +9,6 @@ #include "./log_msg.h" #include "./fast_oss.h" #include "./os.h" -#include "Windows.h" namespace c11log { @@ -58,7 +57,7 @@ class a_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_str(days[msg.tm_time.tm_wday]); + msg.formatted.put_str(days[msg.tm_time.tm_wday]); } }; @@ -68,7 +67,7 @@ class A_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_str(full_days[msg.tm_time.tm_wday]); + msg.formatted.put_str(full_days[msg.tm_time.tm_wday]); } }; @@ -78,7 +77,7 @@ class b_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_str(months[msg.tm_time.tm_mon]); + msg.formatted.put_str(months[msg.tm_time.tm_mon]); } }; @@ -88,7 +87,7 @@ class B_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_str(full_months[msg.tm_time.tm_mon]); + msg.formatted.put_str(full_months[msg.tm_time.tm_mon]); } }; @@ -97,17 +96,17 @@ class c_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_str(days[msg.tm_time.tm_wday]); + msg.formatted.put_str(days[msg.tm_time.tm_wday]); msg.formatted.putc(' '); - msg.formatted.write_str(months[msg.tm_time.tm_mon]); + msg.formatted.put_str(months[msg.tm_time.tm_mon]); msg.formatted.putc(' '); - msg.formatted.write_int(msg.tm_time.tm_mday, 2); + msg.formatted.put_int(msg.tm_time.tm_mday, 2); msg.formatted.putc(' '); - msg.formatted.write_int(msg.tm_time.tm_hour, 2); + msg.formatted.put_int(msg.tm_time.tm_hour, 2); msg.formatted.putc(':'); - msg.formatted.write_int(msg.tm_time.tm_min, 2); + msg.formatted.put_int(msg.tm_time.tm_min, 2); msg.formatted.putc(':'); - msg.formatted.write_int(msg.tm_time.tm_sec, 2); + msg.formatted.put_int(msg.tm_time.tm_sec, 2); } }; @@ -117,7 +116,7 @@ class C_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_int(msg.tm_time.tm_year%100, 2); + msg.formatted.put_int(msg.tm_time.tm_year % 100, 2); } }; @@ -128,11 +127,11 @@ class D_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_int(msg.tm_time.tm_mon + 1, 2); + msg.formatted.put_int(msg.tm_time.tm_mon + 1, 2); msg.formatted.putc('/'); - msg.formatted.write_int(msg.tm_time.tm_mday, 2); + msg.formatted.put_int(msg.tm_time.tm_mday, 2); msg.formatted.putc('/'); - msg.formatted.write_int(msg.tm_time.tm_year % 100, 2); + msg.formatted.put_int(msg.tm_time.tm_year % 100, 2); } }; @@ -142,7 +141,7 @@ class Y_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_int(msg.tm_time.tm_year + 1900, 4); + msg.formatted.put_int(msg.tm_time.tm_year + 1900, 4); } }; @@ -151,7 +150,7 @@ class m_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_int(msg.tm_time.tm_mon + 1, 2); + msg.formatted.put_int(msg.tm_time.tm_mon + 1, 2); } }; @@ -160,7 +159,7 @@ class d_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_int(msg.tm_time.tm_mday, 2); + msg.formatted.put_int(msg.tm_time.tm_mday, 2); } }; @@ -169,7 +168,7 @@ class H_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_int(msg.tm_time.tm_hour, 2); + msg.formatted.put_int(msg.tm_time.tm_hour, 2); } }; @@ -178,7 +177,7 @@ class I_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_int(to12h(msg.tm_time), 2); + msg.formatted.put_int(to12h(msg.tm_time), 2); } }; @@ -187,7 +186,7 @@ class M_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_int(msg.tm_time.tm_min, 2); + msg.formatted.put_int(msg.tm_time.tm_min, 2); } }; @@ -196,7 +195,7 @@ class S_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_int(msg.tm_time.tm_sec, 2); + msg.formatted.put_int(msg.tm_time.tm_sec, 2); } }; @@ -207,7 +206,7 @@ class e_formatter :public flag_formatter { auto duration = msg.time.time_since_epoch(); auto millis = std::chrono::duration_cast(duration).count() % 1000; - msg.formatted.write_int(static_cast(millis), 3); + msg.formatted.put_int(static_cast(millis), 3); } }; @@ -216,7 +215,7 @@ class p_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_data(ampm(msg.tm_time), 2); + msg.formatted.put_data(ampm(msg.tm_time), 2); } }; @@ -227,13 +226,13 @@ class r_formatter :public flag_formatter void format(details::log_msg& msg) override { int hours = to12h(msg.tm_time); - msg.formatted.write_int(to12h(msg.tm_time), 2); + msg.formatted.put_int(to12h(msg.tm_time), 2); msg.formatted.putc(':'); - msg.formatted.write_int(msg.tm_time.tm_min, 2); + msg.formatted.put_int(msg.tm_time.tm_min, 2); msg.formatted.putc(':'); - msg.formatted.write_int(msg.tm_time.tm_sec, 2); + msg.formatted.put_int(msg.tm_time.tm_sec, 2); msg.formatted.putc(' '); - msg.formatted.write_data(ampm(msg.tm_time), 2); + msg.formatted.put_data(ampm(msg.tm_time), 2); } }; @@ -242,9 +241,9 @@ class R_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_int(msg.tm_time.tm_hour, 2); + msg.formatted.put_int(msg.tm_time.tm_hour, 2); msg.formatted.putc(':'); - msg.formatted.write_int(msg.tm_time.tm_min, 2); + msg.formatted.put_int(msg.tm_time.tm_min, 2); } }; @@ -254,15 +253,50 @@ class T_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_int(msg.tm_time.tm_hour, 2); + msg.formatted.put_int(msg.tm_time.tm_hour, 2); msg.formatted.putc(':'); - msg.formatted.write_int(msg.tm_time.tm_min, 2); + msg.formatted.put_int(msg.tm_time.tm_min, 2); msg.formatted.putc(':'); - msg.formatted.write_int(msg.tm_time.tm_sec, 2); - + msg.formatted.put_int(msg.tm_time.tm_sec, 2); } }; +// ISO 8601 offset from UTC in timezone (HH:MM) +class z_formatter :public flag_formatter +{ +public: + + void format(log_msg& msg) override + { + std::lock_guard l(_mutex); + using namespace std::chrono; + auto diff = msg.time - _last_update; + auto secs_diff = abs((duration_cast(diff)).count()); + if (secs_diff >= 2) + { + _value = get_value(msg); + _last_update = msg.time; + } + msg.formatted.put_str(_value); + } +private: + log_clock::time_point _last_update; + std::string _value; + std::string get_value(const log_msg& msg) + { + int total_minutes = os::utc_minutes_offset(msg.tm_time); + int h = total_minutes / 60; + int m = total_minutes % 60; + fast_oss oss; + oss.putc(h < 0 ? '-' : '+'); + oss.put_int(h, 2); + oss.putc(':'); + oss.put_int(m, 2); + return oss.str(); + } + std::mutex _mutex; +}; + @@ -271,7 +305,7 @@ class t_formatter :public flag_formatter { void format(details::log_msg& msg) override { - msg.formatted.write_fast_oss(msg.raw); + msg.formatted.put_fast_oss(msg.raw); } }; @@ -301,7 +335,7 @@ public: } void format(details::log_msg& msg) override { - msg.formatted.write_str(_str); + msg.formatted.put_str(_str); } private: std::string _str; @@ -386,7 +420,7 @@ inline void c11log::details::pattern_formatter::handle_flag(char flag) _formatters.push_back(std::unique_ptr(new details::A_formatter())); break; - case('b'): + case('b') : case('h') : _formatters.push_back(std::unique_ptr(new details::b_formatter())); break; @@ -436,11 +470,11 @@ inline void c11log::details::pattern_formatter::handle_flag(char flag) _formatters.push_back(std::unique_ptr(new details::S_formatter())); break; - case('e'): + case('e') : _formatters.push_back(std::unique_ptr(new details::e_formatter())); break; - case('p'): + case('p') : _formatters.push_back(std::unique_ptr(new details::p_formatter())); break; @@ -453,10 +487,14 @@ inline void c11log::details::pattern_formatter::handle_flag(char flag) break; case('T') : - case('X'): + case('X') : _formatters.push_back(std::unique_ptr(new details::T_formatter())); break; + case('z') : + _formatters.push_back(std::unique_ptr(new details::z_formatter())); + break; + default: //Unkown flag appears as is _formatters.push_back(std::unique_ptr(new details::ch_formatter('%'))); _formatters.push_back(std::unique_ptr(new details::ch_formatter(flag)));