basic.hpp 6.8 KB

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