From 4b5364d356285440992aca6226427819041bf288 Mon Sep 17 00:00:00 2001 From: gabime Date: Wed, 5 Mar 2014 00:31:13 +0200 Subject: [PATCH] vs2013 support --- include/c11log/details/os.h | 32 +++++++++++++---------------- include/c11log/formatter.h | 31 ++++++++++++++++++++-------- include/c11log/sinks/file_sinks.h | 11 ++++++---- include/c11log/sinks/stdout_sinks.h | 32 ++++++++++++++++++----------- 4 files changed, 64 insertions(+), 42 deletions(-) diff --git a/include/c11log/details/os.h b/include/c11log/details/os.h index a869e284..6d560c82 100644 --- a/include/c11log/details/os.h +++ b/include/c11log/details/os.h @@ -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); +} + +} +} } diff --git a/include/c11log/formatter.h b/include/c11log/formatter.h index ad6a9341..b53d04db 100644 --- a/include/c11log/formatter.h +++ b/include/c11log/formatter.h @@ -5,6 +5,7 @@ #include #include #include +#include #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; } diff --git a/include/c11log/sinks/file_sinks.h b/include/c11log/sinks/file_sinks.h index 4caa439f..539fbb06 100644 --- a/include/c11log/sinks/file_sinks.h +++ b/include/c11log/sinks/file_sinks.h @@ -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; diff --git a/include/c11log/sinks/stdout_sinks.h b/include/c11log/sinks/stdout_sinks.h index 81864a44..cffd397f 100644 --- a/include/c11log/sinks/stdout_sinks.h +++ b/include/c11log/sinks/stdout_sinks.h @@ -1,32 +1,40 @@ #pragma once #include +#include +#include + #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 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 cout_sink() { + static const ostream_sink& instance{std::cout}; + return std::shared_ptr(&instance, [=](ostream_sink*) {}); +} + +inline std::shared_ptr cerr_sink() { + static const ostream_sink& instance = ostream_sink(std::cerr); + return std::shared_ptr(&instance, [=](ostream_sink*) {}); +} + + +} } -} \ No newline at end of file