Agora  1.2.0
Agora project
syslog_sink.h
Go to the documentation of this file.
1 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
3 
4 #pragma once
5 
9 
10 #include <array>
11 #include <string>
12 #include <syslog.h>
13 
14 namespace spdlog {
15 namespace sinks {
19 template<typename Mutex>
20 class syslog_sink : public base_sink<Mutex>
21 {
22 
23 public:
24  syslog_sink(std::string ident, int syslog_option, int syslog_facility, bool enable_formatting)
25  : enable_formatting_{enable_formatting}
26  , syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
27  /* spdlog::level::debug */ LOG_DEBUG,
28  /* spdlog::level::info */ LOG_INFO,
29  /* spdlog::level::warn */ LOG_WARNING,
30  /* spdlog::level::err */ LOG_ERR,
31  /* spdlog::level::critical */ LOG_CRIT,
32  /* spdlog::level::off */ LOG_INFO}}
33  , ident_{std::move(ident)}
34  {
35  // set ident to be program name if empty
36  ::openlog(ident_.empty() ? nullptr : ident_.c_str(), syslog_option, syslog_facility);
37  }
38 
39  ~syslog_sink() override
40  {
41  ::closelog();
42  }
43 
44  syslog_sink(const syslog_sink &) = delete;
45  syslog_sink &operator=(const syslog_sink &) = delete;
46 
47 protected:
48  void sink_it_(const details::log_msg &msg) override
49  {
50  string_view_t payload;
51  memory_buf_t formatted;
53  {
54  base_sink<Mutex>::formatter_->format(msg, formatted);
55  payload = string_view_t(formatted.data(), formatted.size());
56  }
57  else
58  {
59  payload = msg.payload;
60  }
61 
62  size_t length = payload.size();
63  // limit to max int
64  if (length > static_cast<size_t>(std::numeric_limits<int>::max()))
65  {
66  length = static_cast<size_t>(std::numeric_limits<int>::max());
67  }
68 
69  ::syslog(syslog_prio_from_level(msg), "%.*s", static_cast<int>(length), payload.data());
70  }
71 
72  void flush_() override {}
73  bool enable_formatting_ = false;
74 
75 private:
76  using levels_array = std::array<int, 7>;
78  // must store the ident because the man says openlog might use the pointer as
79  // is and not a string copy
80  const std::string ident_;
81 
82  //
83  // Simply maps spdlog's log level to syslog priority level.
84  //
86  {
87  return syslog_levels_.at(static_cast<levels_array::size_type>(msg.level));
88  }
89 };
90 
93 } // namespace sinks
94 
95 // Create and register a syslog logger
96 template<typename Factory = spdlog::synchronous_factory>
97 inline std::shared_ptr<logger> syslog_logger_mt(const std::string &logger_name, const std::string &syslog_ident = "", int syslog_option = 0,
98  int syslog_facility = LOG_USER, bool enable_formatting = false)
99 {
100  return Factory::template create<sinks::syslog_sink_mt>(logger_name, syslog_ident, syslog_option, syslog_facility, enable_formatting);
101 }
102 
103 template<typename Factory = spdlog::synchronous_factory>
104 inline std::shared_ptr<logger> syslog_logger_st(const std::string &logger_name, const std::string &syslog_ident = "", int syslog_option = 0,
105  int syslog_facility = LOG_USER, bool enable_formatting = false)
106 {
107  return Factory::template create<sinks::syslog_sink_st>(logger_name, syslog_ident, syslog_option, syslog_facility, enable_formatting);
108 }
109 } // namespace spdlog
spdlog::sinks::syslog_sink::enable_formatting_
bool enable_formatting_
Definition: syslog_sink.h:73
spdlog::sinks::syslog_sink::syslog_levels_
levels_array syslog_levels_
Definition: syslog_sink.h:77
spdlog::sinks::syslog_sink::ident_
const std::string ident_
Definition: syslog_sink.h:80
spdlog::syslog_logger_mt
std::shared_ptr< logger > syslog_logger_mt(const std::string &logger_name, const std::string &syslog_ident="", int syslog_option=0, int syslog_facility=LOG_USER, bool enable_formatting=false)
Definition: syslog_sink.h:97
base_sink.h
spdlog::details::log_msg::payload
string_view_t payload
Definition: log_msg.h:30
spdlog::string_view_t
fmt::basic_string_view< char > string_view_t
Definition: common.h:154
fmt::v8::detail::buffer::size
constexpr auto size() const -> size_t
Definition: core.h:820
synchronous_factory.h
fmt::v8::basic_string_view
Definition: core.h:448
spdlog::sinks::syslog_sink::~syslog_sink
~syslog_sink() override
Definition: syslog_sink.h:39
spdlog::sinks::syslog_sink::sink_it_
void sink_it_(const details::log_msg &msg) override
Definition: syslog_sink.h:48
length
end IFFT Reshape the symbol vector into two different spatial streams length(tx_syms)/NUM_UE
fmt::v8::basic_string_view::size
constexpr auto size() const -> size_t
Definition: core.h:495
spdlog::sinks::syslog_sink::levels_array
std::array< int, 7 > levels_array
Definition: syslog_sink.h:76
spdlog::sinks::syslog_sink::syslog_sink
syslog_sink(std::string ident, int syslog_option, int syslog_facility, bool enable_formatting)
Definition: syslog_sink.h:24
null_mutex.h
fmt::v8::basic_memory_buffer
Definition: format.h:677
spdlog::sinks::syslog_sink::flush_
void flush_() override
Definition: syslog_sink.h:72
spdlog
Definition: async.h:25
spdlog::sinks::base_sink
Definition: base_sink.h:19
spdlog::syslog_logger_st
std::shared_ptr< logger > syslog_logger_st(const std::string &logger_name, const std::string &syslog_ident="", int syslog_option=0, int syslog_facility=LOG_USER, bool enable_formatting=false)
Definition: syslog_sink.h:104
spdlog::sinks::syslog_sink::syslog_prio_from_level
int syslog_prio_from_level(const details::log_msg &msg) const
Definition: syslog_sink.h:85
spdlog::details::log_msg::level
level::level_enum level
Definition: log_msg.h:21
fmt::v8::detail::buffer::data
auto data() -> T *
Definition: core.h:826
fmt::v8::basic_string_view::data
constexpr auto data() const -> const Char *
Definition: core.h:492
spdlog::details::log_msg
Definition: log_msg.h:11
spdlog::sinks::syslog_sink
Definition: syslog_sink.h:20
max
max(y1, y1_1)
spdlog::sinks::syslog_sink::operator=
syslog_sink & operator=(const syslog_sink &)=delete