Commit b0926326 authored by gabi's avatar gabi

pattern_formatter_impl.h update

parent c5afdbdd
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include <vector> #include <vector>
#include <thread> #include <thread>
#include "../formatter.h" #include "../formatter.h"
#include "./log_msg.h" #include "./log_msg.h"
#include "./os.h" #include "./os.h"
...@@ -122,19 +123,31 @@ class B_formatter :public flag_formatter ...@@ -122,19 +123,31 @@ class B_formatter :public flag_formatter
} }
}; };
//write 2 ints seperated by sep with padding of 2
static fmt::MemoryWriter& pad_n_join(fmt::MemoryWriter& w, int v1, int v2, char sep)
{
w << fmt::pad(v1, 2, '0') << sep << fmt::pad(v2, 2, '0');
return w;
}
//write 3 ints seperated by sep with padding of 2
static fmt::MemoryWriter& pad_n_join(fmt::MemoryWriter& w, int v1, int v2, int v3, char sep)
{
w << fmt::pad(v1, 2, '0') << sep << fmt::pad(v2, 2, '0') << sep << fmt::pad(v3, 2, '0');
return w;
}
//Date and time representation (Thu Aug 23 15:35:46 2014) //Date and time representation (Thu Aug 23 15:35:46 2014)
class c_formatter :public flag_formatter class c_formatter :public flag_formatter
{ {
void format(details::log_msg& msg) override void format(details::log_msg& msg) override
{ {
msg.formatted.write("{} {} {:02d} {:02d}:{:02d}:{:02d} {:04d}", msg.formatted << days[msg.tm_time.tm_wday] << ' ' << months[msg.tm_time.tm_mon] << ' ' << msg.tm_time.tm_mday << ' ';
days[msg.tm_time.tm_wday], pad_n_join(msg.formatted, msg.tm_time.tm_hour, msg.tm_time.tm_min, msg.tm_time.tm_sec, ':') << ' ' << msg.tm_time.tm_year + 1900;
months[msg.tm_time.tm_mon],
msg.tm_time.tm_mday,
msg.tm_time.tm_hour,
msg.tm_time.tm_min,
msg.tm_time.tm_sec,
msg.tm_time.tm_year + 1900);
} }
}; };
...@@ -144,7 +157,7 @@ class C_formatter :public flag_formatter ...@@ -144,7 +157,7 @@ class C_formatter :public flag_formatter
{ {
void format(details::log_msg& msg) override void format(details::log_msg& msg) override
{ {
msg.formatted.write("{02:d}", msg.tm_time.tm_year % 100); msg.formatted << fmt::pad(msg.tm_time.tm_year % 100, 2, '0');
} }
}; };
...@@ -155,7 +168,7 @@ class D_formatter :public flag_formatter ...@@ -155,7 +168,7 @@ class D_formatter :public flag_formatter
{ {
void format(details::log_msg& msg) override void format(details::log_msg& msg) override
{ {
msg.formatted.write("{:02d}/{:02d}/{:02d}", msg.tm_time.tm_mon + 1, msg.tm_time.tm_mday, msg.tm_time.tm_year % 100); pad_n_join(msg.formatted, msg.tm_time.tm_mon + 1, msg.tm_time.tm_mday, msg.tm_time.tm_year % 100, '/');
} }
}; };
...@@ -165,7 +178,7 @@ class Y_formatter :public flag_formatter ...@@ -165,7 +178,7 @@ class Y_formatter :public flag_formatter
{ {
void format(details::log_msg& msg) override void format(details::log_msg& msg) override
{ {
msg.formatted.write("{:04d}", msg.tm_time.tm_year + 1900); msg.formatted << msg.tm_time.tm_year + 1900;
} }
}; };
...@@ -174,7 +187,7 @@ class m_formatter :public flag_formatter ...@@ -174,7 +187,7 @@ class m_formatter :public flag_formatter
{ {
void format(details::log_msg& msg) override void format(details::log_msg& msg) override
{ {
msg.formatted.write("{:02d}", msg.tm_time.tm_mon + 1); msg.formatted << fmt::pad(msg.tm_time.tm_mon + 1, 2, '0');
} }
}; };
...@@ -183,7 +196,7 @@ class d_formatter :public flag_formatter ...@@ -183,7 +196,7 @@ class d_formatter :public flag_formatter
{ {
void format(details::log_msg& msg) override void format(details::log_msg& msg) override
{ {
msg.formatted.write("{:02d}", msg.tm_time.tm_mday); msg.formatted << fmt::pad(msg.tm_time.tm_mday, 2, '0');
} }
}; };
...@@ -192,7 +205,7 @@ class H_formatter :public flag_formatter ...@@ -192,7 +205,7 @@ class H_formatter :public flag_formatter
{ {
void format(details::log_msg& msg) override void format(details::log_msg& msg) override
{ {
msg.formatted.write("{:02d}", msg.tm_time.tm_hour); msg.formatted << fmt::pad(msg.tm_time.tm_hour, 2, '0');
} }
}; };
...@@ -201,7 +214,7 @@ class I_formatter :public flag_formatter ...@@ -201,7 +214,7 @@ class I_formatter :public flag_formatter
{ {
void format(details::log_msg& msg) override void format(details::log_msg& msg) override
{ {
msg.formatted.write("{:02d}", to12h(msg.tm_time)); msg.formatted << fmt::pad(to12h(msg.tm_time), 2, '0');
} }
}; };
...@@ -210,7 +223,7 @@ class M_formatter :public flag_formatter ...@@ -210,7 +223,7 @@ class M_formatter :public flag_formatter
{ {
void format(details::log_msg& msg) override void format(details::log_msg& msg) override
{ {
msg.formatted.write("{:02d}", msg.tm_time.tm_min); msg.formatted << fmt::pad(msg.tm_time.tm_min, 2, '0');
} }
}; };
...@@ -219,7 +232,7 @@ class S_formatter :public flag_formatter ...@@ -219,7 +232,7 @@ class S_formatter :public flag_formatter
{ {
void format(details::log_msg& msg) override void format(details::log_msg& msg) override
{ {
msg.formatted.write("{:02d}", msg.tm_time.tm_sec); msg.formatted << fmt::pad(msg.tm_time.tm_sec, 2, '0');
} }
}; };
...@@ -230,7 +243,7 @@ class e_formatter :public flag_formatter ...@@ -230,7 +243,7 @@ class e_formatter :public flag_formatter
{ {
auto duration = msg.time.time_since_epoch(); auto duration = msg.time.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000; auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
msg.formatted.write("{:03d}", static_cast<int>(millis)); msg.formatted << fmt::pad(static_cast<int>(millis), 3, '0');
} }
}; };
...@@ -249,7 +262,7 @@ class r_formatter :public flag_formatter ...@@ -249,7 +262,7 @@ class r_formatter :public flag_formatter
{ {
void format(details::log_msg& msg) override void format(details::log_msg& msg) override
{ {
msg.formatted.write("{:02d}:{:02d}:{:02d} {}", to12h(msg.tm_time), msg.tm_time.tm_min, msg.tm_time.tm_sec, ampm(msg.tm_time)); pad_n_join(msg.formatted, to12h(msg.tm_time), msg.tm_time.tm_min, msg.tm_time.tm_sec, ':') << ' ' << ampm(msg.tm_time);
} }
}; };
...@@ -258,8 +271,7 @@ class R_formatter :public flag_formatter ...@@ -258,8 +271,7 @@ class R_formatter :public flag_formatter
{ {
void format(details::log_msg& msg) override void format(details::log_msg& msg) override
{ {
msg.formatted.write("{:02d}:{:02d}", msg.tm_time.tm_hour, msg.tm_time.tm_min); pad_n_join(msg.formatted, msg.tm_time.tm_hour, msg.tm_time.tm_min, ':');
} }
}; };
...@@ -268,47 +280,53 @@ class T_formatter :public flag_formatter ...@@ -268,47 +280,53 @@ class T_formatter :public flag_formatter
{ {
void format(details::log_msg& msg) override void format(details::log_msg& msg) override
{ {
msg.formatted.write("{:02d}:{:02d}:{:02d}", msg.tm_time.tm_hour, msg.tm_time.tm_min, msg.tm_time.tm_sec); pad_n_join(msg.formatted, msg.tm_time.tm_hour, msg.tm_time.tm_min, msg.tm_time.tm_sec, ':');
} }
}; };
// ISO 8601 offset from UTC in timezone (HH:MM) // ISO 8601 offset from UTC in timezone (+-HH:MM)
class z_formatter :public flag_formatter class z_formatter :public flag_formatter
{ {
public: public:
z_formatter() {} const std::chrono::seconds cache_refresh = const std::chrono::seconds(5);
z_formatter() :_last_update(std::chrono::seconds(0)) {}
z_formatter(const z_formatter&) = delete; z_formatter(const z_formatter&) = delete;
z_formatter& operator=(const z_formatter&) = delete; z_formatter& operator=(const z_formatter&) = delete;
void format(log_msg& msg) override void format(log_msg& msg) override
{ {
std::lock_guard<std::mutex> l(_mutex); #ifdef _WIN32
using namespace std::chrono; int total_minutes = get_cached_offset(msg);
auto diff = msg.time - _last_update; #else
auto secs_diff = std::abs((duration_cast<seconds>(diff)).count()); // No need to chache under gcc,
if (secs_diff >= 2) // it is very fast (already stored in tm.tm_gmtoff)
{ int total_minutes = os::utc_minutes_offset(msg.tm_time);
_value = get_value(msg); #endif
_last_update = msg.time;
} int h = total_minutes / 60;
msg.formatted << _value; int m = total_minutes % 60;
char sign = h >= 0 ? '+' : '-';
msg.formatted << sign;
pad_n_join(msg.formatted, h, m, ':');
} }
private: private:
log_clock::time_point _last_update; log_clock::time_point _last_update;
std::string _value; int _offset_minutes;
std::mutex _mutex; std::mutex _mutex;
std::string get_value(const log_msg& msg) int get_cached_offset(const log_msg& msg)
{ {
int total_minutes = os::utc_minutes_offset(msg.tm_time); using namespace std::chrono;
int h = total_minutes / 60; std::lock_guard<std::mutex> l(_mutex);
int m = total_minutes % 60; if (msg.time - _last_update >= cache_refresh)
fmt::MemoryWriter w; {
w.write("{} {:02d}:{:02d}", h >= 0 ? '+' : '-', h, m); _offset_minutes = os::utc_minutes_offset(msg.tm_time);
return w.str(); _last_update = msg.time;
}
return _offset_minutes;
} }
}; };
...@@ -372,8 +390,7 @@ class full_formatter :public flag_formatter ...@@ -372,8 +390,7 @@ class full_formatter :public flag_formatter
auto duration = msg.time.time_since_epoch(); auto duration = msg.time.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000; auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
/* Slower version(while still very fast - about 3.2 million lines/sec under 10 threads),
/* Slower version(while still very fast - about 3.2 million lines/sec),
msg.formatted.write("[{:d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}.{:03d}] [{}] [{}] {} ", msg.formatted.write("[{:d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}.{:03d}] [{}] [{}] {} ",
msg.tm_time.tm_year + 1900, msg.tm_time.tm_year + 1900,
msg.tm_time.tm_mon + 1, msg.tm_time.tm_mon + 1,
...@@ -386,7 +403,7 @@ class full_formatter :public flag_formatter ...@@ -386,7 +403,7 @@ class full_formatter :public flag_formatter
level::to_str(msg.level), level::to_str(msg.level),
msg.raw.str());*/ msg.raw.str());*/
// Faster (albeit uglier) way to format the line (5.6 million lines/sec) // Faster (albeit uglier) way to format the line (5.6 million lines/sec under 10 threads)
msg.formatted << '[' << msg.tm_time.tm_year + 1900 << '-' msg.formatted << '[' << msg.tm_time.tm_year + 1900 << '-'
<< fmt::pad(msg.tm_time.tm_mon + 1, 2, '0') << '-' << fmt::pad(msg.tm_time.tm_mon + 1, 2, '0') << '-'
<< fmt::pad(msg.tm_time.tm_mday, 2, '0') << ' ' << fmt::pad(msg.tm_time.tm_mday, 2, '0') << ' '
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment