vs2013 support

This commit is contained in:
gabime 2014-03-05 00:31:13 +02:00
parent 8b27eb0f01
commit 4b5364d356
4 changed files with 64 additions and 42 deletions

View File

@ -6,15 +6,8 @@
namespace c11log {
namespace details {
namespace os {
std::tm localtime(const std::time_t &time_tt);
std::tm localtime();
}
}
}
inline std::tm c11log::details::os::localtime(const std::time_t &time_tt)
inline std::tm localtime(const std::time_t &time_tt)
{
std::tm tm;
@ -26,7 +19,7 @@ inline std::tm c11log::details::os::localtime(const std::time_t &time_tt)
return tm;
}
inline std::tm c11log::details::os::localtime()
inline std::tm localtime()
{
std::time_t now_t = time(0);
return localtime(now_t);
@ -35,17 +28,20 @@ inline std::tm c11log::details::os::localtime()
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);
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);
}
inline bool operator!=(const std::tm& tm1, const std::tm& tm2)
{
return !(tm1==tm2);
return !(tm1==tm2);
}
}
}
}

View File

@ -5,6 +5,7 @@
#include <functional>
#include <sstream>
#include <iomanip>
#include <thread>
#include "common_types.h"
#include "details/os.h"
@ -43,20 +44,34 @@ private:
inline void c11log::formatters::default_formatter::_format_time(const log_clock::time_point& tp, std::ostream &dest)
{
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));
#ifdef _MSC_VER
__declspec(thread) static std::tm last_tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0};
__declspec(thread) static char last_time_str[64];
#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];
#endif
auto tm_now = details::os::localtime(log_clock::to_time_t(tp));
using namespace c11log::details::os;
if(last_tm != tm_now)
{
sprintf(last_time_str, "[%d-%02d-%02d %02d:%02d:%02d]",
{
#ifdef _MSC_VER
::sprintf_s
#else
::snprintf
#endif
(last_time_str, sizeof(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;
last_tm = tm_now;
}
dest << last_time_str;
}

View File

@ -140,11 +140,14 @@ private:
return system_clock::time_point(midnight + hours(24));
}
//Create filename for the form basename.YYYY-MM-DD.extension
static std::string _calc_filename(const std::string& basename, const std::string& extension) {
std::tm tm = c11log::details::os::localtime();
char buf[32];
sprintf(buf, ".%d-%02d-%02d.", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
return basename+buf+extension;
std::tm tm = c11log::details::os::localtime();
std::ostringstream oss;
oss << basename << '.';
oss << tm.tm_year + 1900 << '-' << std::setw(2) << std::setfill('0') << tm.tm_mon + 1 << '-' << tm.tm_mday;
oss << '.' << extension;
return oss.str();
}
std::string _base_filename;

View File

@ -1,32 +1,40 @@
#pragma once
#include <iostream>
#include <mutex>
#include <memory>
#include "base_sink.h"
namespace c11log {
namespace sinks {
class ostream_sink: public base_sink {
public:
ostream_sink(std::ostream& os):_ostream(os) {}
explicit ostream_sink(std::ostream& os):_ostream(os) {}
ostream_sink(const ostream_sink&) = delete;
ostream_sink& operator=(const ostream_sink&) = delete;
virtual ~ostream_sink() = default;
protected:
virtual void _sink_it(const std::string& msg) override {
std::lock_guard<std::mutex> lock(_mutex);
_ostream << msg;
}
std::ostream& _ostream;
std::mutex _mutex;
};
class stdout_sink:public ostream_sink {
public:
stdout_sink():ostream_sink(std::cout) {}
};
class stderr_sink:public ostream_sink {
public:
stderr_sink():ostream_sink(std::cerr) {}
};
inline std::shared_ptr<ostream_sink> cout_sink() {
static const ostream_sink& instance{std::cout};
return std::shared_ptr<ostream_sink>(&instance, [=](ostream_sink*) {});
}
inline std::shared_ptr<ostream_sink> cerr_sink() {
static const ostream_sink& instance = ostream_sink(std::cerr);
return std::shared_ptr<ostream_sink>(&instance, [=](ostream_sink*) {});
}
}
}
}