Merge pull request #262 from breyed/streamed-timestamp

Write timestamp directly to output stream
This commit is contained in:
Peter Thorson
2013-06-22 17:59:03 -07:00

View File

@@ -42,6 +42,7 @@
#include <ctime>
#include <iostream>
#include <iomanip>
#include <websocketpp/common/stdint.hpp>
#include <websocketpp/logger/levels.hpp>
@@ -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;