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>
63 : m_static_channels(0xffffffff)
64 , m_dynamic_channels(0)
68 : m_static_channels(0xffffffff)
69 , m_dynamic_channels(0)
74 : m_static_channels(c)
75 , m_dynamic_channels(0)
79 : m_static_channels(c)
80 , m_dynamic_channels(0)
83 void set_ostream(std::ostream * out = &std::cout) {
87 void set_channels(level channels) {
88 if (channels == names::none) {
89 clear_channels(names::all);
93 scoped_lock_type lock(m_lock);
94 m_dynamic_channels |= (channels & m_static_channels);
97 void clear_channels(level channels) {
98 scoped_lock_type lock(m_lock);
99 m_dynamic_channels &= ~channels;
107 void write(level channel, std::string
const & msg) {
108 scoped_lock_type lock(m_lock);
109 if (!this->dynamic_test(channel)) {
return; }
110 *m_out <<
"[" << timestamp <<
"] "
111 <<
"[" << names::channel_name(channel) <<
"] "
121 void write(level channel,
char const * msg) {
122 scoped_lock_type lock(m_lock);
123 if (!this->dynamic_test(channel)) {
return; }
124 *m_out <<
"[" << timestamp <<
"] "
125 <<
"[" << names::channel_name(channel) <<
"] "
130 _WEBSOCKETPP_CONSTEXPR_TOKEN_
bool static_test(level channel)
const {
131 return ((channel & m_static_channels) != 0);
134 bool dynamic_test(level channel) {
135 return ((channel & m_dynamic_channels) != 0);
139 typedef typename concurrency::scoped_lock_type scoped_lock_type;
140 typedef typename concurrency::mutex_type mutex_type;
149 static std::ostream & timestamp(std::ostream & os) {
150 std::time_t t = std::time(NULL);
151 std::tm lt = lib::localtime(t);
152 #ifdef _WEBSOCKETPP_PUTTIME_
153 return os << std::put_time(<,
"%Y-%m-%d %H:%M:%S");
154 #else // Falls back to strftime, which requires a temporary copy of the string.
156 size_t result = std::strftime(buffer,
sizeof(buffer),
"%Y-%m-%d %H:%M:%S",<);
157 return os << (result == 0 ?
"Unknown" : buffer);
161 level
const m_static_channels;
162 level m_dynamic_channels;
163 std::ostream * m_out;
169 #endif // WEBSOCKETPP_LOGGER_BASIC_HPP
uint32_t value
Type of a channel type hint value.
static value const access
Access log.
void write(level channel, char const *msg)
Write a cstring message to the given channel.
void write(level channel, std::string const &msg)
Write a string message to the given channel.
static value const error
Error log.
Basic logger that outputs to an ostream.
Namespace for the WebSocket++ project.