systemd_sink.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/details/null_mutex.h>
  5. #include <spdlog/details/os.h>
  6. #include <spdlog/details/synchronous_factory.h>
  7. #include <spdlog/sinks/base_sink.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. public:
  21. systemd_sink(std::string ident = "", bool enable_formatting = false)
  22. : ident_{std::move(ident)},
  23. enable_formatting_{enable_formatting},
  24. syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
  25. /* spdlog::level::debug */ LOG_DEBUG,
  26. /* spdlog::level::info */ LOG_INFO,
  27. /* spdlog::level::warn */ LOG_WARNING,
  28. /* spdlog::level::err */ LOG_ERR,
  29. /* spdlog::level::critical */ LOG_CRIT,
  30. /* spdlog::level::off */ LOG_INFO}} {}
  31. ~systemd_sink() override {}
  32. systemd_sink(const systemd_sink &) = delete;
  33. systemd_sink &operator=(const systemd_sink &) = delete;
  34. protected:
  35. const std::string ident_;
  36. bool enable_formatting_ = false;
  37. using levels_array = std::array<int, 7>;
  38. levels_array syslog_levels_;
  39. void sink_it_(const details::log_msg &msg) override {
  40. int err;
  41. string_view_t payload;
  42. memory_buf_t formatted;
  43. if (enable_formatting_) {
  44. base_sink<Mutex>::formatter_->format(msg, formatted);
  45. payload = string_view_t(formatted.data(), formatted.size());
  46. } else {
  47. payload = msg.payload;
  48. }
  49. size_t length = payload.size();
  50. // limit to max int
  51. if (length > static_cast<size_t>(std::numeric_limits<int>::max())) {
  52. length = static_cast<size_t>(std::numeric_limits<int>::max());
  53. }
  54. const string_view_t syslog_identifier = ident_.empty() ? msg.logger_name : ident_;
  55. // Do not send source location if not available
  56. if (msg.source.empty()) {
  57. // Note: function call inside '()' to avoid macro expansion
  58. err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(),
  59. "PRIORITY=%d", syslog_level(msg.level),
  60. #ifndef SPDLOG_NO_THREAD_ID
  61. "TID=%zu", msg.thread_id,
  62. #endif
  63. "SYSLOG_IDENTIFIER=%.*s",
  64. static_cast<int>(syslog_identifier.size()),
  65. syslog_identifier.data(), nullptr);
  66. } else {
  67. err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(),
  68. "PRIORITY=%d", syslog_level(msg.level),
  69. #ifndef SPDLOG_NO_THREAD_ID
  70. "TID=%zu", msg.thread_id,
  71. #endif
  72. "SYSLOG_IDENTIFIER=%.*s",
  73. static_cast<int>(syslog_identifier.size()),
  74. syslog_identifier.data(), "CODE_FILE=%s", msg.source.filename,
  75. "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s",
  76. msg.source.funcname, nullptr);
  77. }
  78. if (err) {
  79. throw_spdlog_ex("Failed writing to systemd", errno);
  80. }
  81. }
  82. int syslog_level(level::level_enum l) {
  83. return syslog_levels_.at(static_cast<levels_array::size_type>(l));
  84. }
  85. void flush_() override {}
  86. };
  87. using systemd_sink_mt = systemd_sink<std::mutex>;
  88. using systemd_sink_st = systemd_sink<details::null_mutex>;
  89. } // namespace sinks
  90. // Create and register a syslog logger
  91. template <typename Factory = spdlog::synchronous_factory>
  92. inline std::shared_ptr<logger> systemd_logger_mt(const std::string &logger_name,
  93. const std::string &ident = "",
  94. bool enable_formatting = false) {
  95. return Factory::template create<sinks::systemd_sink_mt>(logger_name, ident, enable_formatting);
  96. }
  97. template <typename Factory = spdlog::synchronous_factory>
  98. inline std::shared_ptr<logger> systemd_logger_st(const std::string &logger_name,
  99. const std::string &ident = "",
  100. bool enable_formatting = false) {
  101. return Factory::template create<sinks::systemd_sink_st>(logger_name, ident, enable_formatting);
  102. }
  103. } // namespace spdlog