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 c11log {
namespace details { namespace details {
namespace os { namespace os {
std::tm localtime(const std::time_t &time_tt);
std::tm localtime();
} inline std::tm localtime(const std::time_t &time_tt)
}
}
inline std::tm c11log::details::os::localtime(const std::time_t &time_tt)
{ {
std::tm tm; std::tm tm;
@ -26,7 +19,7 @@ inline std::tm c11log::details::os::localtime(const std::time_t &time_tt)
return tm; return tm;
} }
inline std::tm c11log::details::os::localtime() inline std::tm localtime()
{ {
std::time_t now_t = time(0); std::time_t now_t = time(0);
return localtime(now_t); return localtime(now_t);
@ -41,11 +34,14 @@ inline bool operator==(const std::tm& tm1, const std::tm& tm2)
tm1.tm_mday == tm2.tm_mday && tm1.tm_mday == tm2.tm_mday &&
tm1.tm_mon == tm2.tm_mon && tm1.tm_mon == tm2.tm_mon &&
tm1.tm_year == tm2.tm_year && tm1.tm_year == tm2.tm_year &&
tm1.tm_isdst == tm2.tm_isdst && tm1.tm_isdst == tm2.tm_isdst);
tm1.tm_gmtoff == tm2.tm_gmtoff);
} }
inline bool operator!=(const std::tm& tm1, const std::tm& tm2) 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 <functional>
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
#include <thread>
#include "common_types.h" #include "common_types.h"
#include "details/os.h" #include "details/os.h"
@ -43,13 +44,25 @@ private:
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)
{ {
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) 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_year + 1900,
tm_now.tm_mon + 1, tm_now.tm_mon + 1,
tm_now.tm_mday, tm_now.tm_mday,
@ -57,6 +70,8 @@ inline void c11log::formatters::default_formatter::_format_time(const log_clock:
tm_now.tm_min, tm_now.tm_min,
tm_now.tm_sec); tm_now.tm_sec);
last_tm = tm_now; last_tm = tm_now;
} }
dest << last_time_str; dest << last_time_str;
} }

View File

@ -140,11 +140,14 @@ private:
return system_clock::time_point(midnight + hours(24)); 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) { static std::string _calc_filename(const std::string& basename, const std::string& extension) {
std::tm tm = c11log::details::os::localtime(); std::tm tm = c11log::details::os::localtime();
char buf[32]; std::ostringstream oss;
sprintf(buf, ".%d-%02d-%02d.", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); oss << basename << '.';
return basename+buf+extension; 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; std::string _base_filename;

View File

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