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.

157 lines
5.1 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. * The initial version of this logging policy was contributed to the WebSocket++
  28. * project by Tom Hughes.
  29. */
  30. #ifndef WEBSOCKETPP_LOGGER_SYSLOG_HPP
  31. #define WEBSOCKETPP_LOGGER_SYSLOG_HPP
  32. #include <syslog.h>
  33. #include <websocketpp/common/cpp11.hpp>
  34. #include <websocketpp/logger/basic.hpp>
  35. #include <websocketpp/logger/levels.hpp>
  36. namespace websocketpp
  37. {
  38. namespace log
  39. {
  40. /// Basic logger that outputs to syslog
  41. template <typename concurrency, typename names> class syslog : public basic<concurrency, names>
  42. {
  43. public:
  44. typedef basic<concurrency, names> base;
  45. /// Construct the logger
  46. /**
  47. * @param hint A channel type specific hint for how to construct the logger
  48. */
  49. syslog<concurrency, names>(channel_type_hint::value hint = channel_type_hint::access) :
  50. basic<concurrency, names>(hint),
  51. m_channel_type_hint(hint)
  52. {
  53. }
  54. /// Construct the logger
  55. /**
  56. * @param channels A set of channels to statically enable
  57. * @param hint A channel type specific hint for how to construct the logger
  58. */
  59. syslog<concurrency, names>(level channels, channel_type_hint::value hint = channel_type_hint::access) :
  60. basic<concurrency, names>(channels, hint),
  61. m_channel_type_hint(hint)
  62. {
  63. }
  64. /// Write a string message to the given channel
  65. /**
  66. * @param channel The channel to write to
  67. * @param msg The message to write
  68. */
  69. void write(level channel, const std::string & msg)
  70. {
  71. write(channel, msg.c_str());
  72. }
  73. /// Write a cstring message to the given channel
  74. /**
  75. * @param channel The channel to write to
  76. * @param msg The message to write
  77. */
  78. void write(level channel, const char * msg)
  79. {
  80. scoped_lock_type lock(base::m_lock);
  81. if (! this->dynamic_test(channel))
  82. {
  83. return;
  84. }
  85. ::syslog(syslog_priority(channel), "[%s] %s", names::channel_name(channel), msg);
  86. }
  87. private:
  88. typedef typename base::scoped_lock_type scoped_lock_type;
  89. /// The default level is used for all access logs and any error logs that
  90. /// don't trivially map to one of the standard syslog levels.
  91. static const int default_level = LOG_INFO;
  92. /// retrieve the syslog priority code given a WebSocket++ channel
  93. /**
  94. * @param channel The level to look up
  95. * @return The syslog level associated with `channel`
  96. */
  97. int syslog_priority(level channel) const
  98. {
  99. if (m_channel_type_hint == channel_type_hint::access)
  100. {
  101. return syslog_priority_access(channel);
  102. }
  103. else
  104. {
  105. return syslog_priority_error(channel);
  106. }
  107. }
  108. /// retrieve the syslog priority code given a WebSocket++ error channel
  109. /**
  110. * @param channel The level to look up
  111. * @return The syslog level associated with `channel`
  112. */
  113. int syslog_priority_error(level channel) const
  114. {
  115. switch (channel)
  116. {
  117. case elevel::devel : return LOG_DEBUG;
  118. case elevel::library: return LOG_DEBUG;
  119. case elevel::info : return LOG_INFO;
  120. case elevel::warn : return LOG_WARNING;
  121. case elevel::rerror : return LOG_ERR;
  122. case elevel::fatal : return LOG_CRIT;
  123. default : return default_level;
  124. }
  125. }
  126. /// retrieve the syslog priority code given a WebSocket++ access channel
  127. /**
  128. * @param channel The level to look up
  129. * @return The syslog level associated with `channel`
  130. */
  131. _WEBSOCKETPP_CONSTEXPR_TOKEN_ int syslog_priority_access(level) const
  132. {
  133. return default_level;
  134. }
  135. channel_type_hint::value m_channel_type_hint;
  136. };
  137. } // namespace log
  138. } // namespace websocketpp
  139. #endif // WEBSOCKETPP_LOGGER_SYSLOG_HPP