You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
6.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef WEBSOCKETPP_LOGGER_BASIC_HPP
  28. #define WEBSOCKETPP_LOGGER_BASIC_HPP
  29. /* Need a way to print a message to the log
  30. *
  31. * - timestamps
  32. * - channels
  33. * - thread safe
  34. * - output to stdout or file
  35. * - selective output channels, both compile time and runtime
  36. * - named channels
  37. * - ability to test whether a log message will be printed at compile time
  38. *
  39. */
  40. #include <ctime>
  41. #include <iomanip>
  42. #include <iostream>
  43. #include <string>
  44. #include <websocketpp/common/cpp11.hpp>
  45. #include <websocketpp/common/stdint.hpp>
  46. #include <websocketpp/common/time.hpp>
  47. #include <websocketpp/logger/levels.hpp>
  48. namespace websocketpp
  49. {
  50. namespace log
  51. {
  52. /// Basic logger that outputs to an ostream
  53. template <typename concurrency, typename names> class basic
  54. {
  55. public:
  56. basic<concurrency, names>(channel_type_hint::value h = channel_type_hint::access) :
  57. m_static_channels(0xffffffff),
  58. m_dynamic_channels(0),
  59. m_out(h == channel_type_hint::error ? &std::cerr : &std::cout)
  60. {
  61. }
  62. basic<concurrency, names>(std::ostream * out) :
  63. m_static_channels(0xffffffff),
  64. m_dynamic_channels(0),
  65. m_out(out)
  66. {
  67. }
  68. basic<concurrency, names>(level c, channel_type_hint::value h = channel_type_hint::access) :
  69. m_static_channels(c),
  70. m_dynamic_channels(0),
  71. m_out(h == channel_type_hint::error ? &std::cerr : &std::cout)
  72. {
  73. }
  74. basic<concurrency, names>(level c, std::ostream * out) :
  75. m_static_channels(c),
  76. m_dynamic_channels(0),
  77. m_out(out)
  78. {
  79. }
  80. /// Destructor
  81. ~basic<concurrency, names>() {}
  82. /// Copy constructor
  83. basic<concurrency, names>(const basic<concurrency, names> & other) :
  84. m_static_channels(other.m_static_channels),
  85. m_dynamic_channels(other.m_dynamic_channels),
  86. m_out(other.m_out)
  87. {
  88. }
  89. #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
  90. // no copy assignment operator because of const member variables
  91. basic<concurrency, names> & operator=(const basic<concurrency, names> &) = delete;
  92. #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
  93. #ifdef _WEBSOCKETPP_MOVE_SEMANTICS_
  94. /// Move constructor
  95. basic<concurrency, names>(basic<concurrency, names> && other) :
  96. m_static_channels(other.m_static_channels),
  97. m_dynamic_channels(other.m_dynamic_channels),
  98. m_out(other.m_out)
  99. {
  100. }
  101. #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
  102. // no move assignment operator because of const member variables
  103. basic<concurrency, names> & operator=(basic<concurrency, names> &&) = delete;
  104. #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
  105. #endif // _WEBSOCKETPP_MOVE_SEMANTICS_
  106. void set_ostream(std::ostream * out = &std::cout)
  107. {
  108. m_out = out;
  109. }
  110. void set_channels(level channels)
  111. {
  112. if (channels == names::none)
  113. {
  114. clear_channels(names::all);
  115. return;
  116. }
  117. scoped_lock_type lock(m_lock);
  118. m_dynamic_channels |= (channels & m_static_channels);
  119. }
  120. void clear_channels(level channels)
  121. {
  122. scoped_lock_type lock(m_lock);
  123. m_dynamic_channels &= ~channels;
  124. }
  125. /// Write a string message to the given channel
  126. /**
  127. * @param channel The channel to write to
  128. * @param msg The message to write
  129. */
  130. void write(level channel, const std::string & msg)
  131. {
  132. scoped_lock_type lock(m_lock);
  133. if (! this->dynamic_test(channel))
  134. {
  135. return;
  136. }
  137. *m_out << "[" << timestamp << "] "
  138. << "[" << names::channel_name(channel) << "] " << msg << "\n";
  139. m_out->flush();
  140. }
  141. /// Write a cstring message to the given channel
  142. /**
  143. * @param channel The channel to write to
  144. * @param msg The message to write
  145. */
  146. void write(level channel, const char * msg)
  147. {
  148. scoped_lock_type lock(m_lock);
  149. if (! this->dynamic_test(channel))
  150. {
  151. return;
  152. }
  153. *m_out << "[" << timestamp << "] "
  154. << "[" << names::channel_name(channel) << "] " << msg << "\n";
  155. m_out->flush();
  156. }
  157. _WEBSOCKETPP_CONSTEXPR_TOKEN_ bool static_test(level channel) const
  158. {
  159. return ((channel & m_static_channels) != 0);
  160. }
  161. bool dynamic_test(level channel)
  162. {
  163. return ((channel & m_dynamic_channels) != 0);
  164. }
  165. protected:
  166. typedef typename concurrency::scoped_lock_type scoped_lock_type;
  167. typedef typename concurrency::mutex_type mutex_type;
  168. mutex_type m_lock;
  169. private:
  170. // The timestamp does not include the time zone, because on Windows with the
  171. // default registry settings, the time zone would be written out in full,
  172. // which would be obnoxiously verbose.
  173. //
  174. // TODO: find a workaround for this or make this format user settable
  175. static std::ostream & timestamp(std::ostream & os)
  176. {
  177. std::time_t t = std::time(NULL);
  178. std::tm lt = lib::localtime(t);
  179. #ifdef _WEBSOCKETPP_PUTTIME_
  180. return os << std::put_time(&lt, "%Y-%m-%d %H:%M:%S");
  181. #else // Falls back to strftime, which requires a temporary copy of the string.
  182. char buffer[20];
  183. size_t result = std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &lt);
  184. return os << (result == 0 ? "Unknown" : buffer);
  185. #endif
  186. }
  187. const level m_static_channels;
  188. level m_dynamic_channels;
  189. std::ostream * m_out;
  190. };
  191. } // namespace log
  192. } // namespace websocketpp
  193. #endif // WEBSOCKETPP_LOGGER_BASIC_HPP