WebSocket++  0.8.0-dev
C++ websocket client/server library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
basic.hpp
1 /*
2  * Copyright (c) 2014, Peter Thorson. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the name of the WebSocket++ Project nor the
12  * names of its contributors may be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #ifndef WEBSOCKETPP_LOGGER_BASIC_HPP
29 #define WEBSOCKETPP_LOGGER_BASIC_HPP
30 
31 /* Need a way to print a message to the log
32  *
33  * - timestamps
34  * - channels
35  * - thread safe
36  * - output to stdout or file
37  * - selective output channels, both compile time and runtime
38  * - named channels
39  * - ability to test whether a log message will be printed at compile time
40  *
41  */
42 
43 #include <websocketpp/logger/levels.hpp>
44 
45 #include <websocketpp/common/cpp11.hpp>
46 #include <websocketpp/common/stdint.hpp>
47 #include <websocketpp/common/time.hpp>
48 
49 #include <ctime>
50 #include <iostream>
51 #include <iomanip>
52 #include <string>
53 
54 namespace websocketpp {
55 namespace log {
56 
57 /// Basic logger that outputs to an ostream
58 template <typename concurrency, typename names>
59 class basic {
60 public:
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) {}
66 
67  basic<concurrency,names>(std::ostream * out)
68  : m_static_channels(0xffffffff)
69  , m_dynamic_channels(0)
70  , m_out(out) {}
71 
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) {}
77 
78  basic<concurrency,names>(level c, std::ostream * out)
79  : m_static_channels(c)
80  , m_dynamic_channels(0)
81  , m_out(out) {}
82 
83  /// Destructor
84  ~basic<concurrency,names>() {}
85 
86  /// Copy constructor
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)
90  , m_out(other.m_out)
91  {}
92 
93 #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
94  // no copy assignment operator because of const member variables
95  basic<concurrency,names> & operator=(basic<concurrency,names> const &) = delete;
96 #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
97 
98 #ifdef _WEBSOCKETPP_MOVE_SEMANTICS_
99  /// Move constructor
100  basic<concurrency,names>(basic<concurrency,names> && other)
101  : m_static_channels(other.m_static_channels)
102  , m_dynamic_channels(other.m_dynamic_channels)
103  , m_out(other.m_out)
104  {}
105 
106 #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
107  // no move assignment operator because of const member variables
108  basic<concurrency,names> & operator=(basic<concurrency,names> &&) = delete;
109 #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
110 
111 #endif // _WEBSOCKETPP_MOVE_SEMANTICS_
112 
113  void set_ostream(std::ostream * out = &std::cout) {
114  m_out = out;
115  }
116 
117  void set_channels(level channels) {
118  if (channels == names::none) {
119  clear_channels(names::all);
120  return;
121  }
122 
123  scoped_lock_type lock(m_lock);
124  m_dynamic_channels |= (channels & m_static_channels);
125  }
126 
127  void clear_channels(level channels) {
128  scoped_lock_type lock(m_lock);
129  m_dynamic_channels &= ~channels;
130  }
131 
132  /// Write a string message to the given channel
133  /**
134  * @param channel The channel to write to
135  * @param msg The message to write
136  */
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) << "] "
142  << msg << "\n";
143  m_out->flush();
144  }
145 
146  /// Write a cstring message to the given channel
147  /**
148  * @param channel The channel to write to
149  * @param msg The message to write
150  */
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) << "] "
156  << msg << "\n";
157  m_out->flush();
158  }
159 
160  _WEBSOCKETPP_CONSTEXPR_TOKEN_ bool static_test(level channel) const {
161  return ((channel & m_static_channels) != 0);
162  }
163 
164  bool dynamic_test(level channel) {
165  return ((channel & m_dynamic_channels) != 0);
166  }
167 
168 protected:
169  typedef typename concurrency::scoped_lock_type scoped_lock_type;
170  typedef typename concurrency::mutex_type mutex_type;
171  mutex_type m_lock;
172 
173 private:
174  // The timestamp does not include the time zone, because on Windows with the
175  // default registry settings, the time zone would be written out in full,
176  // which would be obnoxiously verbose.
177  //
178  // TODO: find a workaround for this or make this format user settable
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(&lt,"%Y-%m-%d %H:%M:%S");
184  #else // Falls back to strftime, which requires a temporary copy of the string.
185  char buffer[20];
186  size_t result = std::strftime(buffer,sizeof(buffer),"%Y-%m-%d %H:%M:%S",&lt);
187  return os << (result == 0 ? "Unknown" : buffer);
188  #endif
189  }
190 
191  level const m_static_channels;
192  level m_dynamic_channels;
193  std::ostream * m_out;
194 };
195 
196 } // log
197 } // websocketpp
198 
199 #endif // WEBSOCKETPP_LOGGER_BASIC_HPP
void write(level channel, char const *msg)
Write a cstring message to the given channel.
Definition: basic.hpp:151
void write(level channel, std::string const &msg)
Write a string message to the given channel.
Definition: basic.hpp:137
void handle_accept(connection_ptr con, lib::error_code const &ec)
Handler callback for start_accept.