diff --git a/websocketpp/logger/basic.hpp b/websocketpp/logger/basic.hpp index 695c9c9cae..1be34aacce 100644 --- a/websocketpp/logger/basic.hpp +++ b/websocketpp/logger/basic.hpp @@ -42,6 +42,7 @@ #include #include +#include #include #include @@ -85,7 +86,7 @@ public: void write(level channel, const std::string& msg) { scoped_lock_type lock(m_lock); if (!this->dynamic_test(channel)) { return; } - *m_out << "[" << get_timestamp() << "] " + *m_out << "[" << timestamp << "] " << "[" << names::channel_name(channel) << "] " << msg << "\n"; m_out->flush(); @@ -94,7 +95,7 @@ public: void write(level channel, const char* msg) { scoped_lock_type lock(m_lock); if (!this->dynamic_test(channel)) { return; } - *m_out << "[" << get_timestamp() << "] " + *m_out << "[" << timestamp << "] " << "[" << names::channel_name(channel) << "] " << msg << "\n"; m_out->flush(); @@ -116,16 +117,20 @@ private: // which would be obnoxiously verbose. // // TODO: find a workaround for this or make this format user settable - char const * get_timestamp() { + static std::ostream& timestamp(std::ostream& os) { std::time_t t = std::time(NULL); - std::strftime(m_buffer,sizeof(m_buffer),"%Y-%m-%d %H:%M:%S", - std::localtime(&t)); - return m_buffer; + std::tm* lt = std::localtime(&t); + #ifdef _WEBSOCKETPP_CPP11_CHRONO_ + return os << std::put_time(lt,"%Y-%m-%d %H:%M:%S"); + #else // Falls back to strftime, which requires a temporary copy of the string. + char buffer[20]; + std::strftime(buffer,sizeof(buffer),"%Y-%m-%d %H:%M:%S",lt); + return os << buffer; + #endif } mutex_type m_lock; - char m_buffer[20]; const level m_static_channels; level m_dynamic_channels; std::ostream* m_out;