formatter msvc support

This commit is contained in:
gabime 2014-03-18 19:23:24 +02:00
parent da82d8e245
commit d0f3b05f90
3 changed files with 46 additions and 38 deletions

View File

@ -1,6 +1,6 @@
CXX = clang++ CXX = clang++
CXXFLAGS = -march=native -Wall -Wextra -Wshadow -pedantic -std=c++11 -pthread -I../include -B/usr/lib/gold-ld/ 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 CXX_DEBUG_FLAGS= -g
OUTBIN = example-clang OUTBIN = example-clang

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
// Fast ostringstream like supprt which return its string by ref and nothing more // Faster than ostringstream--returns its string by ref
#include<streambuf> #include<streambuf>
#include<string> #include<string>
@ -52,9 +52,14 @@ protected:
int_type overflow(int_type ch) override int_type overflow(int_type ch) override
{ {
if (ch != traits_type::eof())
xsputn((char*)&ch, 1); bool not_eofile = traits_type::not_eof(ch);
return 1; if (not_eofile)
{
char c = traits_type::to_char_type(ch);
xsputn(&c, 1);
}
return not_eofile;
} }
private: private:
std::string _str; std::string _str;

View File

@ -7,6 +7,7 @@
#include <iomanip> #include <iomanip>
#include <thread> #include <thread>
#include <cstdlib> #include <cstdlib>
#include <cstring>
#include "common_types.h" #include "common_types.h"
#include "details/os.h" #include "details/os.h"
@ -46,48 +47,50 @@ private:
void _format_time(const log_clock::time_point& tp, std::ostream &dest); void _format_time(const log_clock::time_point& tp, std::ostream &dest);
}; };
} //namespace formatter } //namespace formatter
} //namespace c11log } //namespace c11log
// Format datetime like this: [2014-03-14 17:15:22] // 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 c11log::details::os;
using namespace std::chrono; using namespace std::chrono;
#ifdef _MSC_VER #ifdef _WIN32 //VS2013 doesn't support yet thread_local keyword
__declspec(thread) static std::tm s_last_tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0}; __declspec(thread) static char s_cache_str[64];
__declspec(thread) static details::fast_oss s_time_oss; __declspec(thread) static size_t s_cache_size;
__declspec(thread) static std::time_t s_cache_time_t = 0;
#else #else
thread_local static char s_cache_str[64];
thread_local static details::fast_oss s_time_oss; thread_local static size_t s_cache_size;
thread_local static std::time_t s_cache_time_t = 0; thread_local static std::time_t s_cache_time_t = 0;
#endif #endif
//Cache every second
std::time_t tp_time_t = log_clock::to_time_t(tp); std::time_t tp_time_t = log_clock::to_time_t(tp);
if(tp_time_t != s_cache_time_t)
{
//Cache every second auto tm_now = details::os::localtime(tp_time_t);
if(tp_time_t != s_cache_time_t) details::fast_oss time_oss;
{ time_oss.fill('0');
auto tm_now = details::os::localtime(tp_time_t); time_oss << '[' << tm_now.tm_year + 1900 << '-';
s_time_oss.reset_str(); time_oss.width(2);
s_time_oss.fill('0'); time_oss << tm_now.tm_mon + 1 << '-';
s_time_oss << '[' << tm_now.tm_year + 1900 << '-'; time_oss.width(2);
s_time_oss.width(2); time_oss << tm_now.tm_mday << ' ';
s_time_oss << tm_now.tm_mon + 1 << '-'; time_oss.width(2);
s_time_oss.width(2); time_oss << tm_now.tm_hour << ':';
s_time_oss << tm_now.tm_mday << ' '; time_oss.width(2);
s_time_oss.width(2); time_oss << tm_now.tm_min << ':';
s_time_oss << tm_now.tm_hour << ':'; time_oss.width(2);
s_time_oss.width(2); time_oss << tm_now.tm_sec << ']';
s_time_oss << tm_now.tm_min << ':'; //Cache the resulted string and its size
s_time_oss.width(2); s_cache_time_t = tp_time_t;
s_time_oss << tm_now.tm_sec << ']'; const std::string &s = time_oss.str_ref();
s_cache_time_t = tp_time_t; 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_cache_str, s_cache_size);
dest.write(s.c_str(), s.size());
} }