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++
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

View File

@ -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<streambuf>
#include<string>
@ -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;

View File

@ -7,6 +7,7 @@
#include <iomanip>
#include <thread>
#include <cstdlib>
#include <cstring>
#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;
#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 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
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);
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 << ']';
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);
}