systemd_sink.h 4.0 KB

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