syslog_sink.h 3.6 KB

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