Added pattern formatter and updated related stuff

This commit is contained in:
gabi
2014-10-14 03:44:40 +03:00
parent 793d16d547
commit 481fdbcbb1
9 changed files with 351 additions and 53 deletions

View File

@@ -33,7 +33,7 @@ public:
void format(details::log_msg& msg) override
{
details::fast_oss oss;
_format_time(msg.time, oss);
_format_time(msg, oss);
if(!msg.logger_name.empty())
oss << " [" << msg.logger_name << ':' << c11log::level::to_str(msg.level) << "] ";
@@ -44,7 +44,7 @@ public:
msg.formatted = oss.str();
}
private:
void _format_time(const log_clock::time_point& tp, std::ostream& output);
void _format_time(const details::log_msg& msg, std::ostream &output);
};
@@ -53,44 +53,15 @@ private:
} //namespace c11log
// Format datetime like this: [2014-03-14 17:15:22]
inline void c11log::formatters::default_formatter::_format_time(const log_clock::time_point& tp, std::ostream &output)
inline void c11log::formatters::default_formatter::_format_time(const details::log_msg& msg, std::ostream &output)
{
using namespace c11log::details::os;
using namespace std::chrono;
#ifdef _WIN32 //VS2013 doesn't support yet thread_local keyword
__declspec(thread) static char s_cache_timestr[128];
__declspec(thread) static int s_cache_timesize = 0;
__declspec(thread) static std::time_t s_cache_time_t = 0;
#else
thread_local static char s_cache_timestr[128];
thread_local static int s_cache_timesize = 0;
thread_local static std::time_t s_cache_time_t = 0;
#endif
//Cache every second
std::time_t tp_time_t = log_clock::to_time_t(tp);
if(tp_time_t != s_cache_time_t)
{
auto tm_now = details::os::localtime(tp_time_t);
std::ostringstream time_oss;
time_oss.fill('0');
time_oss << '[' << tm_now.tm_year + 1900 << '-';
time_oss.width(2);
time_oss << tm_now.tm_mon + 1 << '-';
time_oss.width(2);
time_oss << tm_now.tm_mday << ' ';
time_oss.width(2);
time_oss << tm_now.tm_hour << ':';
time_oss.width(2);
time_oss << tm_now.tm_min << ':';
time_oss.width(2);
time_oss << tm_now.tm_sec << ']';
//Cache the resulted string and its size
s_cache_time_t = tp_time_t;
const std::string s = time_oss.str();
s_cache_timesize = s.size();
std::memcpy(s_cache_timestr, s.c_str(), s_cache_timesize);
}
output.write(s_cache_timestr, s_cache_timesize);
output.fill('0');
output << '[' << msg.tm_time.tm_year + 1900 << '-';
output.width(2);
output << msg.tm_time.tm_mon + 1 << '-';
output << msg.tm_time.tm_mday << ' ';
output << msg.tm_time.tm_hour << ':';
output << msg.tm_time.tm_min << ':';
output << msg.tm_time.tm_sec << ']';
}