Photon 1.0.0
Loading...
Searching...
No Matches
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
14namespace spdlog
15{
16 namespace sinks
17 {
21 template <typename Mutex>
22 class syslog_sink : public base_sink<Mutex>
23 {
24
25 public:
26 syslog_sink(std::string ident, int syslog_option, int syslog_facility, bool enable_formatting)
27 : enable_formatting_{enable_formatting}, syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
28 /* spdlog::level::debug */ LOG_DEBUG,
29 /* spdlog::level::info */ LOG_INFO,
30 /* spdlog::level::warn */ LOG_WARNING,
31 /* spdlog::level::err */ LOG_ERR,
32 /* spdlog::level::critical */ LOG_CRIT,
33 /* spdlog::level::off */ LOG_INFO}},
34 ident_{std::move(ident)}
35 {
36 // set ident to be program name if empty
37 ::openlog(ident_.empty() ? nullptr : ident_.c_str(), syslog_option, syslog_facility);
38 }
39
40 ~syslog_sink() override
41 {
42 ::closelog();
43 }
44
45 syslog_sink(const syslog_sink&) = delete;
47
48 protected:
49 void sink_it_(const details::log_msg& msg) override
50 {
51 string_view_t payload;
52 memory_buf_t formatted;
53 if (enable_formatting_)
54 {
55 base_sink<Mutex>::formatter_->format(msg, formatted);
56 payload = string_view_t(formatted.data(), formatted.size());
57 }
58 else
59 {
60 payload = msg.payload;
61 }
62
63 size_t length = payload.size();
64 // limit to max int
65 if (length > static_cast<size_t>(std::numeric_limits<int>::max()))
66 {
67 length = static_cast<size_t>(std::numeric_limits<int>::max());
68 }
69
70 ::syslog(syslog_prio_from_level(msg), "%.*s", static_cast<int>(length), payload.data());
71 }
72
73 void flush_() override
74 {
75 }
76 bool enable_formatting_ = false;
77
78 private:
79 using levels_array = std::array<int, 7>;
81 // must store the ident because the man says openlog might use the pointer as
82 // is and not a string copy
83 const std::string ident_;
84
85 //
86 // Simply maps spdlog's log level to syslog priority level.
87 //
89 {
90 return syslog_levels_.at(static_cast<levels_array::size_type>(msg.level));
91 }
92 };
93
96 } // namespace sinks
97
98 // Create and register a syslog logger
99 template <typename Factory = spdlog::synchronous_factory>
100 inline 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)
101 {
102 return Factory::template create<sinks::syslog_sink_mt>(logger_name, syslog_ident, syslog_option, syslog_facility, enable_formatting);
103 }
104
105 template <typename Factory = spdlog::synchronous_factory>
106 inline 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)
107 {
108 return Factory::template create<sinks::syslog_sink_st>(logger_name, syslog_ident, syslog_option, syslog_facility, enable_formatting);
109 }
110} // namespace spdlog
Definition base_sink.h:22
Definition syslog_sink.h:23
int syslog_prio_from_level(const details::log_msg &msg) const
Definition syslog_sink.h:88
void sink_it_(const details::log_msg &msg) override
Definition syslog_sink.h:49
syslog_sink(std::string ident, int syslog_option, int syslog_facility, bool enable_formatting)
Definition syslog_sink.h:26
syslog_sink(const syslog_sink &)=delete
levels_array syslog_levels_
Definition syslog_sink.h:80
void flush_() override
Definition syslog_sink.h:73
bool enable_formatting_
Definition syslog_sink.h:76
const std::string ident_
Definition syslog_sink.h:83
~syslog_sink() override
Definition syslog_sink.h:40
std::array< int, 7 > levels_array
Definition syslog_sink.h:79
syslog_sink & operator=(const syslog_sink &)=delete
syslog_sink< std::mutex > syslog_sink_mt
Definition syslog_sink.h:94
syslog_sink< details::null_mutex > syslog_sink_st
Definition syslog_sink.h:95
Definition async.h:26
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:106
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:100
fmt::basic_string_view< char > string_view_t
Definition common.h:172
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:173
Definition log_msg.h:14
string_view_t payload
Definition log_msg.h:32
level::level_enum level
Definition log_msg.h:23