default format_time performance improvement
This commit is contained in:
		@@ -33,7 +33,7 @@ int main(int argc, char* argv[])
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    auto start = system_clock::now();
 | 
					    auto start = system_clock::now();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const unsigned int howmany = 5000000;
 | 
					    const unsigned int howmany = 15000000;
 | 
				
			||||||
    for(unsigned int i = 0; i < howmany ; i++)
 | 
					    for(unsigned int i = 0; i < howmany ; i++)
 | 
				
			||||||
        my_logger.info() << "Hello logger " << i;
 | 
					        my_logger.info() << "Hello logger " << i;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,7 +27,7 @@ public:
 | 
				
			|||||||
        return _str;
 | 
					        return _str;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void clear()
 | 
					    void reset_str()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        _str.clear();
 | 
					        _str.clear();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -68,6 +68,10 @@ public:
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        return _dev.str_ref();
 | 
					        return _dev.str_ref();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
						void reset_str()
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							_dev.reset_str();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    str_devicebuf _dev;
 | 
					    str_devicebuf _dev;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -42,9 +42,7 @@ public:
 | 
				
			|||||||
        // The move ctor should only be called on start of logging line,
 | 
					        // The move ctor should only be called on start of logging line,
 | 
				
			||||||
        // where no logging happened yet for this line so no need to copy the string from the other
 | 
					        // where no logging happened yet for this line so no need to copy the string from the other
 | 
				
			||||||
        _oss(),
 | 
					        _oss(),
 | 
				
			||||||
        _level(other._level)
 | 
					        _level(other._level) {};
 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ~line_logger()
 | 
					    ~line_logger()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,9 +6,11 @@
 | 
				
			|||||||
#include <sstream>
 | 
					#include <sstream>
 | 
				
			||||||
#include <iomanip>
 | 
					#include <iomanip>
 | 
				
			||||||
#include <thread>
 | 
					#include <thread>
 | 
				
			||||||
 | 
					#include <cstdlib>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "common_types.h"
 | 
					#include "common_types.h"
 | 
				
			||||||
#include "details/os.h"
 | 
					#include "details/os.h"
 | 
				
			||||||
 | 
					#include "details/fast_oss.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace c11log
 | 
					namespace c11log
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -35,7 +37,7 @@ public:
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        _format_time(tp, dest);
 | 
					        _format_time(tp, dest);
 | 
				
			||||||
        if(!logger_name.empty())
 | 
					        if(!logger_name.empty())
 | 
				
			||||||
            dest << " [" <<  logger_name << ":" << c11log::level::to_str(level) << "] ";
 | 
					            dest << " [" <<  logger_name << ':' << c11log::level::to_str(level) << "] ";
 | 
				
			||||||
        else
 | 
					        else
 | 
				
			||||||
            dest << " [" << c11log::level::to_str(level) << "] ";
 | 
					            dest << " [" << c11log::level::to_str(level) << "] ";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -48,35 +50,44 @@ private:
 | 
				
			|||||||
} //namespace c11log
 | 
					} //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 &dest)
 | 
					inline void c11log::formatters::default_formatter::_format_time(const log_clock::time_point& tp, std::ostream &dest)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
						using namespace c11log::details::os;
 | 
				
			||||||
 | 
						using namespace std::chrono;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef _MSC_VER
 | 
					#ifdef _MSC_VER
 | 
				
			||||||
    __declspec(thread) static std::tm last_tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0};
 | 
					    __declspec(thread) static std::tm s_last_tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0};
 | 
				
			||||||
    __declspec(thread) static char last_time_str[64];
 | 
					    __declspec(thread) static details::fast_oss s_time_oss;
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
    thread_local static std::tm last_tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 | 
					
 | 
				
			||||||
    thread_local static char last_time_str[64];
 | 
						thread_local static details::fast_oss s_time_oss;
 | 
				
			||||||
 | 
						thread_local static std::time_t s_cache_time_t = 0;
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    auto tm_now = details::os::localtime(log_clock::to_time_t(tp));
 | 
					
 | 
				
			||||||
    using namespace c11log::details::os;
 | 
						std::time_t tp_time_t = log_clock::to_time_t(tp);
 | 
				
			||||||
    if(last_tm != tm_now)
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						//Cache every second
 | 
				
			||||||
 | 
						if(tp_time_t != s_cache_time_t)
 | 
				
			||||||
    {		
 | 
					    {		
 | 
				
			||||||
#ifdef _MSC_VER
 | 
							auto tm_now = details::os::localtime(tp_time_t);
 | 
				
			||||||
        ::sprintf_s
 | 
							s_time_oss.reset_str();
 | 
				
			||||||
#else
 | 
							s_time_oss.fill('0');
 | 
				
			||||||
        ::snprintf
 | 
							s_time_oss << '[' << tm_now.tm_year + 1900 << '-';
 | 
				
			||||||
#endif
 | 
							s_time_oss.width(2);
 | 
				
			||||||
        (last_time_str, sizeof(last_time_str), "[%d-%02d-%02d %02d:%02d:%02d]",
 | 
							s_time_oss << tm_now.tm_mon + 1 << '-';
 | 
				
			||||||
         tm_now.tm_year + 1900,
 | 
							s_time_oss.width(2);
 | 
				
			||||||
         tm_now.tm_mon + 1,
 | 
							s_time_oss << tm_now.tm_mday << ' ';
 | 
				
			||||||
         tm_now.tm_mday,
 | 
							s_time_oss.width(2);
 | 
				
			||||||
         tm_now.tm_hour,
 | 
							s_time_oss << tm_now.tm_hour << ':';
 | 
				
			||||||
         tm_now.tm_min,
 | 
							s_time_oss.width(2);
 | 
				
			||||||
         tm_now.tm_sec);
 | 
							s_time_oss << tm_now.tm_min << ':';
 | 
				
			||||||
        last_tm = tm_now;
 | 
							s_time_oss.width(2);
 | 
				
			||||||
 | 
							s_time_oss << tm_now.tm_sec << ']';		
 | 
				
			||||||
 | 
							s_cache_time_t = tp_time_t;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    dest << last_time_str;
 | 
						const std::string &s = s_time_oss.str_ref();
 | 
				
			||||||
 | 
						dest.write(s.c_str(), s.size());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user