诸暨麻将添加redis
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.
 
 
 
 
 
 

104 lines
3.3 KiB

  1. // Copyright(c) 2019 ZVYAGIN.Alexander@gmail.com
  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 <spdlog/details/synchronous_factory.h>
  7. #include <array>
  8. #ifndef SD_JOURNAL_SUPPRESS_LOCATION
  9. #define SD_JOURNAL_SUPPRESS_LOCATION
  10. #endif
  11. #include <systemd/sd-journal.h>
  12. namespace spdlog {
  13. namespace sinks {
  14. /**
  15. * Sink that write to systemd journal using the `sd_journal_send()` library call.
  16. *
  17. * Locking is not needed, as `sd_journal_send()` itself is thread-safe.
  18. */
  19. template<typename Mutex>
  20. class systemd_sink : public base_sink<Mutex>
  21. {
  22. public:
  23. //
  24. systemd_sink()
  25. : syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
  26. /* spdlog::level::debug */ LOG_DEBUG,
  27. /* spdlog::level::info */ LOG_INFO,
  28. /* spdlog::level::warn */ LOG_WARNING,
  29. /* spdlog::level::err */ LOG_ERR,
  30. /* spdlog::level::critical */ LOG_CRIT,
  31. /* spdlog::level::off */ LOG_INFO}}
  32. {}
  33. ~systemd_sink() override {}
  34. systemd_sink(const systemd_sink &) = delete;
  35. systemd_sink &operator=(const systemd_sink &) = delete;
  36. protected:
  37. using levels_array = std::array<int, 7>;
  38. levels_array syslog_levels_;
  39. void sink_it_(const details::log_msg &msg) override
  40. {
  41. int err;
  42. size_t length = msg.payload.size();
  43. // limit to max int
  44. if (length > static_cast<size_t>(std::numeric_limits<int>::max()))
  45. {
  46. length = static_cast<size_t>(std::numeric_limits<int>::max());
  47. }
  48. // Do not send source location if not available
  49. if (msg.source.empty())
  50. {
  51. // Note: function call inside '()' to avoid macro expansion
  52. err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level),
  53. "SYSLOG_IDENTIFIER=%.*s", static_cast<int>(msg.logger_name.size()), msg.logger_name.data(), nullptr);
  54. }
  55. else
  56. {
  57. err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level),
  58. "SYSLOG_IDENTIFIER=%.*s", static_cast<int>(msg.logger_name.size()), msg.logger_name.data(), "CODE_FILE=%s",
  59. msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr);
  60. }
  61. if (err)
  62. {
  63. SPDLOG_THROW(spdlog_ex("Failed writing to systemd", errno));
  64. }
  65. }
  66. int syslog_level(level::level_enum l)
  67. {
  68. return syslog_levels_.at(static_cast<levels_array::size_type>(l));
  69. }
  70. void flush_() override {}
  71. };
  72. using systemd_sink_mt = systemd_sink<std::mutex>;
  73. using systemd_sink_st = systemd_sink<details::null_mutex>;
  74. } // namespace sinks
  75. // Create and register a syslog logger
  76. template<typename Factory = spdlog::synchronous_factory>
  77. inline std::shared_ptr<logger> systemd_logger_mt(const std::string &logger_name)
  78. {
  79. return Factory::template create<sinks::systemd_sink_mt>(logger_name);
  80. }
  81. template<typename Factory = spdlog::synchronous_factory>
  82. inline std::shared_ptr<logger> systemd_logger_st(const std::string &logger_name)
  83. {
  84. return Factory::template create<sinks::systemd_sink_st>(logger_name);
  85. }
  86. } // namespace spdlog