syslog.hpp 5.2 KB

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