28 #ifndef WEBSOCKETPP_LOGGER_BASIC_HPP
29 #define WEBSOCKETPP_LOGGER_BASIC_HPP
43 #include <websocketpp/logger/levels.hpp>
45 #include <websocketpp/common/cpp11.hpp>
46 #include <websocketpp/common/stdint.hpp>
47 #include <websocketpp/common/time.hpp>
58 template <
typename concurrency,
typename names>
61 basic<concurrency,names>(channel_type_hint::value h =
62 channel_type_hint::access)
63 : m_static_channels(0xffffffff)
64 , m_dynamic_channels(0)
65 , m_out(h == channel_type_hint::error ? &std::cerr : &std::cout) {}
67 basic<concurrency,names>(std::ostream * out)
68 : m_static_channels(0xffffffff)
69 , m_dynamic_channels(0)
72 basic<concurrency,names>(level c, channel_type_hint::value h =
73 channel_type_hint::access)
74 : m_static_channels(c)
75 , m_dynamic_channels(0)
76 , m_out(h == channel_type_hint::error ? &std::cerr : &std::cout) {}
78 basic<concurrency,names>(level c, std::ostream * out)
79 : m_static_channels(c)
80 , m_dynamic_channels(0)
84 ~basic<concurrency,names>() {}
87 basic<concurrency,names>(
basic<concurrency,names>
const & other)
88 : m_static_channels(other.m_static_channels)
89 , m_dynamic_channels(other.m_dynamic_channels)
93 #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
98 #ifdef _WEBSOCKETPP_MOVE_SEMANTICS_
106 #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
113 void set_ostream(std::ostream * out = &std::cout) {
117 void set_channels(level channels) {
118 if (channels == names::none) {
119 clear_channels(names::all);
123 scoped_lock_type lock(m_lock);
124 m_dynamic_channels |= (channels & m_static_channels);
127 void clear_channels(level channels) {
128 scoped_lock_type lock(m_lock);
129 m_dynamic_channels &= ~channels;
137 void write(level channel, std::string
const & msg) {
138 scoped_lock_type lock(m_lock);
139 if (!
this->dynamic_test(channel)) {
return; }
140 *m_out <<
"[" << timestamp <<
"] "
141 <<
"[" << names::channel_name(channel) <<
"] "
151 void write(level channel,
char const * msg) {
152 scoped_lock_type lock(m_lock);
153 if (!
this->dynamic_test(channel)) {
return; }
154 *m_out <<
"[" << timestamp <<
"] "
155 <<
"[" << names::channel_name(channel) <<
"] "
160 _WEBSOCKETPP_CONSTEXPR_TOKEN_
bool static_test(level channel)
const {
161 return ((channel & m_static_channels) != 0);
164 bool dynamic_test(level channel) {
165 return ((channel & m_dynamic_channels) != 0);
169 typedef typename concurrency::scoped_lock_type scoped_lock_type;
170 typedef typename concurrency::mutex_type mutex_type;
179 static std::ostream & timestamp(std::ostream & os) {
180 std::time_t t = std::time(NULL);
181 std::tm lt = lib::localtime(t);
182 #ifdef _WEBSOCKETPP_PUTTIME_
183 return os << std::put_time(<,
"%Y-%m-%d %H:%M:%S");
186 size_t result = std::strftime(buffer,
sizeof(buffer),
"%Y-%m-%d %H:%M:%S",<);
187 return os << (result == 0 ?
"Unknown" : buffer);
191 level
const m_static_channels;
192 level m_dynamic_channels;
193 std::ostream * m_out;