Agora  1.2.0
Agora project
systemd_sink.h
Go to the documentation of this file.
1 // Copyright(c) 2019 ZVYAGIN.Alexander@gmail.com
2 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
3 
4 #pragma once
5 
9 
10 #include <array>
11 #ifndef SD_JOURNAL_SUPPRESS_LOCATION
12 # define SD_JOURNAL_SUPPRESS_LOCATION
13 #endif
14 #include <systemd/sd-journal.h>
15 
16 namespace spdlog {
17 namespace sinks {
18 
22 template<typename Mutex>
23 class systemd_sink : public base_sink<Mutex>
24 {
25 public:
26  systemd_sink(std::string ident = "", bool enable_formatting = false)
27  : ident_{std::move(ident)}
28  , enable_formatting_{enable_formatting}
29  , syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
30  /* spdlog::level::debug */ LOG_DEBUG,
31  /* spdlog::level::info */ LOG_INFO,
32  /* spdlog::level::warn */ LOG_WARNING,
33  /* spdlog::level::err */ LOG_ERR,
34  /* spdlog::level::critical */ LOG_CRIT,
35  /* spdlog::level::off */ LOG_INFO}}
36  {}
37 
38  ~systemd_sink() override {}
39 
40  systemd_sink(const systemd_sink &) = delete;
41  systemd_sink &operator=(const systemd_sink &) = delete;
42 
43 protected:
44  const std::string ident_;
45  bool enable_formatting_ = false;
46  using levels_array = std::array<int, 7>;
48 
49  void sink_it_(const details::log_msg &msg) override
50  {
51  int err;
52  string_view_t payload;
53  memory_buf_t formatted;
55  {
56  base_sink<Mutex>::formatter_->format(msg, formatted);
57  payload = string_view_t(formatted.data(), formatted.size());
58  }
59  else
60  {
61  payload = msg.payload;
62  }
63 
64  size_t length = payload.size();
65  // limit to max int
66  if (length > static_cast<size_t>(std::numeric_limits<int>::max()))
67  {
68  length = static_cast<size_t>(std::numeric_limits<int>::max());
69  }
70 
71  const string_view_t syslog_identifier = ident_.empty() ? msg.logger_name : ident_;
72 
73  // Do not send source location if not available
74  if (msg.source.empty())
75  {
76  // Note: function call inside '()' to avoid macro expansion
77  err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(), "PRIORITY=%d", syslog_level(msg.level),
78  "SYSLOG_IDENTIFIER=%.*s", static_cast<int>(syslog_identifier.size()), syslog_identifier.data(), nullptr);
79  }
80  else
81  {
82  err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(), "PRIORITY=%d", syslog_level(msg.level),
83  "SYSLOG_IDENTIFIER=%.*s", static_cast<int>(syslog_identifier.size()), syslog_identifier.data(), "CODE_FILE=%s",
84  msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr);
85  }
86 
87  if (err)
88  {
89  throw_spdlog_ex("Failed writing to systemd", errno);
90  }
91  }
92 
94  {
95  return syslog_levels_.at(static_cast<levels_array::size_type>(l));
96  }
97 
98  void flush_() override {}
99 };
100 
103 } // namespace sinks
104 
105 // Create and register a syslog logger
106 template<typename Factory = spdlog::synchronous_factory>
107 inline std::shared_ptr<logger> systemd_logger_mt(
108  const std::string &logger_name, const std::string &ident = "", bool enable_formatting = false)
109 {
110  return Factory::template create<sinks::systemd_sink_mt>(logger_name, ident, enable_formatting);
111 }
112 
113 template<typename Factory = spdlog::synchronous_factory>
114 inline std::shared_ptr<logger> systemd_logger_st(
115  const std::string &logger_name, const std::string &ident = "", bool enable_formatting = false)
116 {
117  return Factory::template create<sinks::systemd_sink_st>(logger_name, ident, enable_formatting);
118 }
119 } // namespace spdlog
spdlog::sinks::systemd_sink::operator=
systemd_sink & operator=(const systemd_sink &)=delete
spdlog::source_loc::funcname
const char * funcname
Definition: common.h:305
spdlog::source_loc::filename
const char * filename
Definition: common.h:303
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
spdlog::sinks::systemd_sink::sink_it_
void sink_it_(const details::log_msg &msg) override
Definition: systemd_sink.h:49
fmt::v8::detail::buffer::size
constexpr auto size() const -> size_t
Definition: core.h:820
synchronous_factory.h
spdlog::sinks::systemd_sink
Definition: systemd_sink.h:23
fmt::v8::basic_string_view
Definition: core.h:448
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::level::level_enum
level_enum
Definition: common.h:211
spdlog::sinks::systemd_sink::enable_formatting_
bool enable_formatting_
Definition: systemd_sink.h:45
spdlog::sinks::systemd_sink::syslog_level
int syslog_level(level::level_enum l)
Definition: systemd_sink.h:93
spdlog::level::err
@ err
Definition: common.h:217
null_mutex.h
fmt::v8::basic_memory_buffer
Definition: format.h:677
spdlog
Definition: async.h:25
spdlog::throw_spdlog_ex
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
Definition: common-inl.h:72
spdlog::sinks::base_sink
Definition: base_sink.h:19
spdlog::sinks::systemd_sink::flush_
void flush_() override
Definition: systemd_sink.h:98
spdlog::details::log_msg::logger_name
string_view_t logger_name
Definition: log_msg.h:20
spdlog::sinks::systemd_sink::systemd_sink
systemd_sink(std::string ident="", bool enable_formatting=false)
Definition: systemd_sink.h:26
spdlog::sinks::systemd_sink::ident_
const std::string ident_
Definition: systemd_sink.h:44
spdlog::source_loc::line
int line
Definition: common.h:304
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::systemd_logger_mt
std::shared_ptr< logger > systemd_logger_mt(const std::string &logger_name, const std::string &ident="", bool enable_formatting=false)
Definition: systemd_sink.h:107
spdlog::details::log_msg
Definition: log_msg.h:11
spdlog::sinks::systemd_sink::syslog_levels_
levels_array syslog_levels_
Definition: systemd_sink.h:47
spdlog::details::log_msg::source
source_loc source
Definition: log_msg.h:29
spdlog::systemd_logger_st
std::shared_ptr< logger > systemd_logger_st(const std::string &logger_name, const std::string &ident="", bool enable_formatting=false)
Definition: systemd_sink.h:114
l
l
Definition: parse_all_dl.m:71
max
max(y1, y1_1)
spdlog::source_loc::empty
constexpr bool empty() const noexcept
Definition: common.h:299
spdlog::sinks::systemd_sink::~systemd_sink
~systemd_sink() override
Definition: systemd_sink.h:38
spdlog::sinks::systemd_sink::levels_array
std::array< int, 7 > levels_array
Definition: systemd_sink.h:46