websocketpp  0.4.0
C++/Boost Asio based 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 <ctime>
44 #include <iostream>
45 #include <iomanip>
46 
47 #include <websocketpp/common/cpp11.hpp>
48 #include <websocketpp/common/stdint.hpp>
49 #include <websocketpp/common/time.hpp>
50 #include <websocketpp/logger/levels.hpp>
51 
52 namespace websocketpp {
53 namespace log {
54 
56 template <typename concurrency, typename names>
57 class basic {
58 public:
61  : m_static_channels(0xffffffff)
62  , m_dynamic_channels(0)
63  , m_out(h == channel_type_hint::error ? &std::cerr : &std::cout) {}
64 
65  basic<concurrency,names>(std::ostream * out)
66  : m_static_channels(0xffffffff)
67  , m_dynamic_channels(0)
68  , m_out(out) {}
69 
72  : m_static_channels(c)
73  , m_dynamic_channels(0)
74  , m_out(h == channel_type_hint::error ? &std::cerr : &std::cout) {}
75 
76  basic<concurrency,names>(level c, std::ostream * out)
77  : m_static_channels(c)
78  , m_dynamic_channels(0)
79  , m_out(out) {}
80 
81  void set_ostream(std::ostream * out = &std::cout) {
82  m_out = out;
83  }
84 
85  void set_channels(level channels) {
86  if (channels == names::none) {
87  clear_channels(names::all);
88  return;
89  }
90 
91  scoped_lock_type lock(m_lock);
92  m_dynamic_channels |= (channels & m_static_channels);
93  }
94 
95  void clear_channels(level channels) {
96  scoped_lock_type lock(m_lock);
97  m_dynamic_channels &= ~channels;
98  }
99 
100  void write(level channel, std::string const & msg) {
101  scoped_lock_type lock(m_lock);
102  if (!this->dynamic_test(channel)) { return; }
103  *m_out << "[" << timestamp << "] "
104  << "[" << names::channel_name(channel) << "] "
105  << msg << "\n";
106  m_out->flush();
107  }
108 
109  void write(level channel, char const * msg) {
110  scoped_lock_type lock(m_lock);
111  if (!this->dynamic_test(channel)) { return; }
112  *m_out << "[" << timestamp << "] "
113  << "[" << names::channel_name(channel) << "] "
114  << msg << "\n";
115  m_out->flush();
116  }
117 
118  _WEBSOCKETPP_CONSTEXPR_TOKEN_ bool static_test(level channel) const {
119  return ((channel & m_static_channels) != 0);
120  }
121 
122  bool dynamic_test(level channel) {
123  return ((channel & m_dynamic_channels) != 0);
124  }
125 private:
126  typedef typename concurrency::scoped_lock_type scoped_lock_type;
127  typedef typename concurrency::mutex_type mutex_type;
128 
129  // The timestamp does not include the time zone, because on Windows with the
130  // default registry settings, the time zone would be written out in full,
131  // which would be obnoxiously verbose.
132  //
133  // TODO: find a workaround for this or make this format user settable
134  static std::ostream & timestamp(std::ostream & os) {
135  std::time_t t = std::time(NULL);
136  std::tm lt = lib::localtime(t);
137  #ifdef _WEBSOCKETPP_PUTTIME_
138  return os << std::put_time(&lt,"%Y-%m-%d %H:%M:%S");
139  #else // Falls back to strftime, which requires a temporary copy of the string.
140  char buffer[20];
141  size_t result = std::strftime(buffer,sizeof(buffer),"%Y-%m-%d %H:%M:%S",&lt);
142  return os << (result == 0 ? "Unknown" : buffer);
143  #endif
144  }
145 
146  mutex_type m_lock;
147 
148  level const m_static_channels;
149  level m_dynamic_channels;
150  std::ostream * m_out;
151 };
152 
153 } // log
154 } // websocketpp
155 
156 #endif // WEBSOCKETPP_LOGGER_BASIC_HPP
uint32_t value
Type of a channel type hint value.
Definition: levels.hpp:48
static value const access
Access log.
Definition: levels.hpp:53
static value const error
Error log.
Definition: levels.hpp:55
Basic logger that outputs to an ostream.
Definition: basic.hpp:57
Namespace for the WebSocket++ project.
Definition: base64.hpp:41