syslog_sink.h
Go to the documentation of this file.
1 //
2 // Copyright(c) 2015 Gabi Melman.
3 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
4 //
5 
6 #pragma once
7 
8 #ifndef SPDLOG_H
9 #include "spdlog/spdlog.h"
10 #endif
11 
12 #include "spdlog/sinks/base_sink.h"
13 
14 #include <array>
15 #include <string>
16 #include <syslog.h>
17 
18 namespace spdlog {
19 namespace sinks {
20 /**
21  * Sink that write to syslog using the `syscall()` library call.
22  *
23  * Locking is not needed, as `syslog()` itself is thread-safe.
24  */
25 template<typename Mutex>
26 class syslog_sink : public base_sink<Mutex>
27 {
28 public:
29  //
30  explicit syslog_sink(std::string ident = "", int syslog_option = 0, int syslog_facility = LOG_USER)
31  : ident_(std::move(ident))
32  {
33  priorities_[static_cast<size_t>(level::trace)] = LOG_DEBUG;
34  priorities_[static_cast<size_t>(level::debug)] = LOG_DEBUG;
35  priorities_[static_cast<size_t>(level::info)] = LOG_INFO;
36  priorities_[static_cast<size_t>(level::warn)] = LOG_WARNING;
37  priorities_[static_cast<size_t>(level::err)] = LOG_ERR;
38  priorities_[static_cast<size_t>(level::critical)] = LOG_CRIT;
39  priorities_[static_cast<size_t>(level::off)] = LOG_INFO;
40 
41  // set ident to be program name if empty
42  ::openlog(ident_.empty() ? nullptr : ident_.c_str(), syslog_option, syslog_facility);
43  }
44 
45  ~syslog_sink() override
46  {
47  ::closelog();
48  }
49 
50  syslog_sink(const syslog_sink &) = delete;
51  syslog_sink &operator=(const syslog_sink &) = delete;
52 
53 protected:
54  void sink_it_(const details::log_msg &msg) override
55  {
56  ::syslog(syslog_prio_from_level(msg), "%s", fmt::to_string(msg.payload).c_str());
57  }
58 
59  void flush_() override {}
60 
61 private:
62  std::array<int, 7> priorities_;
63  // must store the ident because the man says openlog might use the pointer as
64  // is and not a string copy
66 
67  //
68  // Simply maps spdlog's log level to syslog priority level.
69  //
71  {
72  return priorities_[static_cast<size_t>(msg.level)];
73  }
74 };
75 
78 } // namespace sinks
79 
80 // Create and register a syslog logger
81 template<typename Factory = default_factory>
82 inline std::shared_ptr<logger> syslog_logger_mt(
83  const std::string &logger_name, const std::string &syslog_ident = "", int syslog_option = 0, int syslog_facility = (1 << 3))
84 {
85  return Factory::template create<sinks::syslog_sink_mt>(logger_name, syslog_ident, syslog_option, syslog_facility);
86 }
87 
88 template<typename Factory = default_factory>
89 inline std::shared_ptr<logger> syslog_logger_st(
90  const std::string &logger_name, const std::string &syslog_ident = "", int syslog_option = 0, int syslog_facility = (1 << 3))
91 {
92  return Factory::template create<sinks::syslog_sink_st>(logger_name, syslog_ident, syslog_option, syslog_facility);
93 }
94 } // namespace spdlog
#define LOG_DEBUG(stream)
Definition: Messenger.h:148
void msg(const char *fmt,...)
Definition: message.cpp:107
std::string string
Definition: nybbler.cc:12
syslog_sink & operator=(const syslog_sink &)=delete
STL namespace.
std::array< int, 7 > priorities_
Definition: syslog_sink.h:62
#define LOG_CRIT(stream)
Definition: Messenger.h:123
int syslog_prio_from_level(const details::log_msg &msg) const
Definition: syslog_sink.h:70
Definition: async.h:27
void sink_it_(const details::log_msg &msg) override
Definition: syslog_sink.h:54
std::shared_ptr< logger > syslog_logger_st(const std::string &logger_name, const std::string &syslog_ident="", int syslog_option=0, int syslog_facility=(1<< 3))
Definition: syslog_sink.h:89
level::level_enum level
Definition: log_msg.h:42
def move(depos, offset)
Definition: depos.py:107
const std::string ident_
Definition: syslog_sink.h:65
std::shared_ptr< logger > syslog_logger_mt(const std::string &logger_name, const std::string &syslog_ident="", int syslog_option=0, int syslog_facility=(1<< 3))
Definition: syslog_sink.h:82
const string_view_t payload
Definition: log_msg.h:52
#define LOG_INFO(stream)
Definition: Messenger.h:143
void flush_() override
Definition: syslog_sink.h:59
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
syslog_sink(std::string ident="", int syslog_option=0, int syslog_facility=LOG_USER)
Definition: syslog_sink.h:30