From d0f3b05f90302e600788de986051e371932ecd38 Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 18 Mar 2014 19:23:24 +0200 Subject: [PATCH] formatter msvc support --- example/makefile.clang | 2 +- include/c11log/details/fast_oss.h | 13 ++++-- include/c11log/formatter.h | 69 ++++++++++++++++--------------- 3 files changed, 46 insertions(+), 38 deletions(-) diff --git a/example/makefile.clang b/example/makefile.clang index b68f4848..0f738d49 100644 --- a/example/makefile.clang +++ b/example/makefile.clang @@ -1,6 +1,6 @@ CXX = clang++ CXXFLAGS = -march=native -Wall -Wextra -Wshadow -pedantic -std=c++11 -pthread -I../include -B/usr/lib/gold-ld/ -CXX_RELEASE_FLAGS = -O3 -flto +CXX_RELEASE_FLAGS = -O3 CXX_DEBUG_FLAGS= -g OUTBIN = example-clang diff --git a/include/c11log/details/fast_oss.h b/include/c11log/details/fast_oss.h index 67d50e6a..5f0119c3 100644 --- a/include/c11log/details/fast_oss.h +++ b/include/c11log/details/fast_oss.h @@ -1,6 +1,6 @@ #pragma once -// Fast ostringstream like supprt which return its string by ref and nothing more +// Faster than ostringstream--returns its string by ref #include #include @@ -52,9 +52,14 @@ protected: int_type overflow(int_type ch) override { - if (ch != traits_type::eof()) - xsputn((char*)&ch, 1); - return 1; + + bool not_eofile = traits_type::not_eof(ch); + if (not_eofile) + { + char c = traits_type::to_char_type(ch); + xsputn(&c, 1); + } + return not_eofile; } private: std::string _str; diff --git a/include/c11log/formatter.h b/include/c11log/formatter.h index b0bf1863..695a7c18 100644 --- a/include/c11log/formatter.h +++ b/include/c11log/formatter.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "common_types.h" #include "details/os.h" @@ -46,48 +47,50 @@ private: void _format_time(const log_clock::time_point& tp, std::ostream &dest); }; + + } //namespace formatter } //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) { - using namespace c11log::details::os; - using namespace std::chrono; + using namespace c11log::details::os; + using namespace std::chrono; -#ifdef _MSC_VER - __declspec(thread) static std::tm s_last_tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0}; - __declspec(thread) static details::fast_oss s_time_oss; +#ifdef _WIN32 //VS2013 doesn't support yet thread_local keyword + __declspec(thread) static char s_cache_str[64]; + __declspec(thread) static size_t s_cache_size; + __declspec(thread) static std::time_t s_cache_time_t = 0; #else - - thread_local static details::fast_oss s_time_oss; - thread_local static std::time_t s_cache_time_t = 0; + thread_local static char s_cache_str[64]; + thread_local static size_t s_cache_size; + thread_local static std::time_t s_cache_time_t = 0; #endif - - std::time_t tp_time_t = log_clock::to_time_t(tp); - - - //Cache every second - if(tp_time_t != s_cache_time_t) - { - auto tm_now = details::os::localtime(tp_time_t); - s_time_oss.reset_str(); - s_time_oss.fill('0'); - s_time_oss << '[' << tm_now.tm_year + 1900 << '-'; - s_time_oss.width(2); - s_time_oss << tm_now.tm_mon + 1 << '-'; - s_time_oss.width(2); - s_time_oss << tm_now.tm_mday << ' '; - s_time_oss.width(2); - s_time_oss << tm_now.tm_hour << ':'; - s_time_oss.width(2); - s_time_oss << tm_now.tm_min << ':'; - s_time_oss.width(2); - s_time_oss << tm_now.tm_sec << ']'; - s_cache_time_t = tp_time_t; + //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); + details::fast_oss 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_ref(); + std::memcpy(s_cache_str, s.c_str(), s.size()); + s_cache_size = s.size(); } - const std::string &s = s_time_oss.str_ref(); - dest.write(s.c_str(), s.size()); + dest.write(s_cache_str, s_cache_size); }