syslog_sink.h 4.1 KB

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