systemd_sink.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/os.h>
  6. #include <spdlog/details/null_mutex.h>
  7. #include <spdlog/details/synchronous_factory.h>
  8. #include <array>
  9. #ifndef SD_JOURNAL_SUPPRESS_LOCATION
  10. # define SD_JOURNAL_SUPPRESS_LOCATION
  11. #endif
  12. #include <systemd/sd-journal.h>
  13. namespace spdlog {
  14. namespace sinks {
  15. /**
  16. * Sink that write to systemd journal using the `sd_journal_send()` library call.
  17. */
  18. template<typename Mutex>
  19. class systemd_sink : public base_sink<Mutex>
  20. {
  21. public:
  22. systemd_sink(std::string ident = "", bool enable_formatting = false)
  23. : ident_{std::move(ident)}
  24. , enable_formatting_{enable_formatting}
  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. const std::string ident_;
  38. bool enable_formatting_ = false;
  39. using levels_array = std::array<int, 7>;
  40. levels_array syslog_levels_;
  41. void sink_it_(const details::log_msg &msg) override
  42. {
  43. int err;
  44. string_view_t payload;
  45. memory_buf_t formatted;
  46. if (enable_formatting_)
  47. {
  48. base_sink<Mutex>::formatter_->format(msg, formatted);
  49. payload = string_view_t(formatted.data(), formatted.size());
  50. }
  51. else
  52. {
  53. payload = msg.payload;
  54. }
  55. size_t length = payload.size();
  56. // limit to max int
  57. if (length > static_cast<size_t>(std::numeric_limits<int>::max()))
  58. {
  59. length = static_cast<size_t>(std::numeric_limits<int>::max());
  60. }
  61. const string_view_t syslog_identifier = ident_.empty() ? msg.logger_name : ident_;
  62. // Do not send source location if not available
  63. if (msg.source.empty())
  64. {
  65. // Note: function call inside '()' to avoid macro expansion
  66. err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(), "PRIORITY=%d", syslog_level(msg.level),
  67. #ifndef SPDLOG_NO_THREAD_ID
  68. "TID=%zu", details::os::thread_id(),
  69. #endif
  70. "SYSLOG_IDENTIFIER=%.*s", static_cast<int>(syslog_identifier.size()), syslog_identifier.data(), nullptr);
  71. }
  72. else
  73. {
  74. err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(), "PRIORITY=%d", syslog_level(msg.level),
  75. #ifndef SPDLOG_NO_THREAD_ID
  76. "TID=%zu", details::os::thread_id(),
  77. #endif
  78. "SYSLOG_IDENTIFIER=%.*s", static_cast<int>(syslog_identifier.size()), syslog_identifier.data(), "CODE_FILE=%s",
  79. msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr);
  80. }
  81. if (err)
  82. {
  83. throw_spdlog_ex("Failed writing to systemd", errno);
  84. }
  85. }
  86. int syslog_level(level::level_enum l)
  87. {
  88. return syslog_levels_.at(static_cast<levels_array::size_type>(l));
  89. }
  90. void flush_() override {}
  91. };
  92. using systemd_sink_mt = systemd_sink<std::mutex>;
  93. using systemd_sink_st = systemd_sink<details::null_mutex>;
  94. } // namespace sinks
  95. // Create and register a syslog logger
  96. template<typename Factory = spdlog::synchronous_factory>
  97. inline std::shared_ptr<logger> systemd_logger_mt(
  98. const std::string &logger_name, const std::string &ident = "", bool enable_formatting = false)
  99. {
  100. return Factory::template create<sinks::systemd_sink_mt>(logger_name, ident, enable_formatting);
  101. }
  102. template<typename Factory = spdlog::synchronous_factory>
  103. inline std::shared_ptr<logger> systemd_logger_st(
  104. const std::string &logger_name, const std::string &ident = "", bool enable_formatting = false)
  105. {
  106. return Factory::template create<sinks::systemd_sink_st>(logger_name, ident, enable_formatting);
  107. }
  108. } // namespace spdlog