诸暨麻将添加redis
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

109 líneas
3.6 KiB

  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #include <spdlog/sinks/base_sink.h>
  5. #include <spdlog/details/null_mutex.h>
  6. #include <array>
  7. #include <string>
  8. #include <syslog.h>
  9. namespace spdlog {
  10. namespace sinks {
  11. /**
  12. * Sink that write to syslog using the `syscall()` library call.
  13. */
  14. template<typename Mutex>
  15. class syslog_sink : public base_sink<Mutex>
  16. {
  17. public:
  18. syslog_sink(std::string ident, int syslog_option, int syslog_facility, bool enable_formatting)
  19. : enable_formatting_{enable_formatting}
  20. , syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
  21. /* spdlog::level::debug */ LOG_DEBUG,
  22. /* spdlog::level::info */ LOG_INFO,
  23. /* spdlog::level::warn */ LOG_WARNING,
  24. /* spdlog::level::err */ LOG_ERR,
  25. /* spdlog::level::critical */ LOG_CRIT,
  26. /* spdlog::level::off */ LOG_INFO}}
  27. , ident_{std::move(ident)}
  28. {
  29. // set ident to be program name if empty
  30. ::openlog(ident_.empty() ? nullptr : ident_.c_str(), syslog_option, syslog_facility);
  31. }
  32. ~syslog_sink() override
  33. {
  34. ::closelog();
  35. }
  36. syslog_sink(const syslog_sink &) = delete;
  37. syslog_sink &operator=(const syslog_sink &) = delete;
  38. protected:
  39. void sink_it_(const details::log_msg &msg) override
  40. {
  41. string_view_t payload;
  42. memory_buf_t formatted;
  43. if (enable_formatting_)
  44. {
  45. base_sink<Mutex>::formatter_->format(msg, formatted);
  46. payload = string_view_t(formatted.data(), formatted.size());
  47. }
  48. else
  49. {
  50. payload = msg.payload;
  51. }
  52. size_t length = payload.size();
  53. // limit to max int
  54. if (length > static_cast<size_t>(std::numeric_limits<int>::max()))
  55. {
  56. length = static_cast<size_t>(std::numeric_limits<int>::max());
  57. }
  58. ::syslog(syslog_prio_from_level(msg), "%.*s", static_cast<int>(length), payload.data());
  59. }
  60. void flush_() override {}
  61. bool enable_formatting_ = false;
  62. private:
  63. using levels_array = std::array<int, 7>;
  64. levels_array syslog_levels_;
  65. // must store the ident because the man says openlog might use the pointer as
  66. // is and not a string copy
  67. const std::string ident_;
  68. //
  69. // Simply maps spdlog's log level to syslog priority level.
  70. //
  71. int syslog_prio_from_level(const details::log_msg &msg) const
  72. {
  73. return syslog_levels_.at(static_cast<levels_array::size_type>(msg.level));
  74. }
  75. };
  76. using syslog_sink_mt = syslog_sink<std::mutex>;
  77. using syslog_sink_st = syslog_sink<details::null_mutex>;
  78. } // namespace sinks
  79. // Create and register a syslog logger
  80. template<typename Factory = default_factory>
  81. inline std::shared_ptr<logger> syslog_logger_mt(const std::string &logger_name, const std::string &syslog_ident = "", int syslog_option = 0,
  82. int syslog_facility = LOG_USER, bool enable_formatting = false)
  83. {
  84. return Factory::template create<sinks::syslog_sink_mt>(logger_name, syslog_ident, syslog_option, syslog_facility, enable_formatting);
  85. }
  86. template<typename Factory = default_factory>
  87. inline std::shared_ptr<logger> syslog_logger_st(const std::string &logger_name, const std::string &syslog_ident = "", int syslog_option = 0,
  88. int syslog_facility = LOG_USER, bool enable_formatting = false)
  89. {
  90. return Factory::template create<sinks::syslog_sink_st>(logger_name, syslog_ident, syslog_option, syslog_facility, enable_formatting);
  91. }
  92. } // namespace spdlog