From 2be33c19fc4e915280e5a8ebd038ffa4f1822d0a Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 4 Mar 2014 00:08:30 +0200 Subject: [PATCH] cache sprintf of time in default formatter --- example/example.cpp | 13 ++++++++++--- include/c11log/details/fast_oss.h | 4 ++-- include/c11log/details/os.h | 21 +++++++++++++++++---- include/c11log/formatter.h | 27 ++++++++++++++++----------- 4 files changed, 45 insertions(+), 20 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 0499f4de..809bb7aa 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -48,20 +48,26 @@ void testlog(int threads) int main(int argc, char* argv[]) { + if(argc || argv){}; using namespace std::chrono; using namespace c11log; using namespace utils; using std::cout; using std::endl; - //auto fsink2 = std::make_shared("log", "txt", 1024*1024*50 , 5, seconds(1)); + auto fsink = std::make_shared("log", "txt", 1024*1024*50 , 5, seconds(1)); auto &my_logger = get_logger("example"); + auto null_sink = std::make_shared(); + //auto async = std::make_shared(1000); + //async->add_sink(fsink); + my_logger.add_sink(null_sink); auto start = system_clock::now(); const unsigned int howmany = 10000000; for(unsigned int i = 0; i < howmany ; i++) my_logger.info() << "Hello logger " << i; - + + //async->shutdown(seconds(3)); auto delta = system_clock::now() - start; auto delta_d = duration_cast> (delta); cout << "Total " << format(howmany) << endl; @@ -71,7 +77,7 @@ int main(int argc, char* argv[]) return 0; - + /* if(argc !=3) { std::cerr << "Usage: " << argv[0] << " qsize, threads" << std::endl; return 0; @@ -93,5 +99,6 @@ int main(int argc, char* argv[]) testlog(threads); + */ } diff --git a/include/c11log/details/fast_oss.h b/include/c11log/details/fast_oss.h index 3b880185..8ca13cf3 100644 --- a/include/c11log/details/fast_oss.h +++ b/include/c11log/details/fast_oss.h @@ -15,7 +15,7 @@ public: _str = other._str; return *this; } - + const std::string& str_ref() const { return _str; } @@ -53,7 +53,7 @@ public: _dev = other._dev; return *this; } - + const std::string& str_ref() const { return _dev.str_ref(); } diff --git a/include/c11log/details/os.h b/include/c11log/details/os.h index b45be277..a869e284 100644 --- a/include/c11log/details/os.h +++ b/include/c11log/details/os.h @@ -32,7 +32,20 @@ inline std::tm c11log::details::os::localtime() return localtime(now_t); } -// Take care of snprintf in visual studio -#ifdef _MSC_VER -#define snprintf _snprintf -#endif + +inline bool operator==(const std::tm& tm1, const std::tm& tm2) +{ + return (tm1.tm_sec == tm2.tm_sec && + tm1.tm_min == tm2.tm_min && + tm1.tm_hour == tm2.tm_hour && + tm1.tm_mday == tm2.tm_mday && + tm1.tm_mon == tm2.tm_mon && + tm1.tm_year == tm2.tm_year && + tm1.tm_isdst == tm2.tm_isdst && + tm1.tm_gmtoff == tm2.tm_gmtoff); +} + +inline bool operator!=(const std::tm& tm1, const std::tm& tm2) +{ + return !(tm1==tm2); +} diff --git a/include/c11log/formatter.h b/include/c11log/formatter.h index 45fffa96..ad6a9341 100644 --- a/include/c11log/formatter.h +++ b/include/c11log/formatter.h @@ -43,15 +43,20 @@ private: inline void c11log::formatters::default_formatter::_format_time(const log_clock::time_point& tp, std::ostream &dest) { - auto tm = details::os::localtime(log_clock::to_time_t(tp)); - char buff[64]; - int size = snprintf(buff, sizeof(buff), "[%d-%02d-%02d %02d:%02d:%02d]", - tm.tm_year + 1900, - tm.tm_mon + 1, - tm.tm_mday, - tm.tm_hour, - tm.tm_min, - tm.tm_sec); - - dest.write(buff, size); + static thread_local std::tm last_tm = {0,0,0,0,0,0,0,0,0,0,0}; + static thread_local char last_time_str[64]; + auto tm_now = details::os::localtime(log_clock::to_time_t(tp)); + + if(last_tm != tm_now) + { + sprintf(last_time_str, "[%d-%02d-%02d %02d:%02d:%02d]", + tm_now.tm_year + 1900, + tm_now.tm_mon + 1, + tm_now.tm_mday, + tm_now.tm_hour, + tm_now.tm_min, + tm_now.tm_sec); + last_tm = tm_now; + } + dest << last_time_str; }