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#include "spdlog/sinks/base_sink.h"
9#include "spdlog/spdlog.h"
10
11#include <array>
12#include <string>
13#include <syslog.h>
14
15namespace spdlog {
16namespace sinks {
17/**
18 * Sink that write to syslog using the `syscall()` library call.
19 *
20 * Locking is not needed, as `syslog()` itself is thread-safe.
21 */
22template<typename Mutex>
23class syslog_sink : public base_sink<Mutex>
24{
25public:
26    //
27    explicit syslog_sink(std::string ident = "", int syslog_option = 0, int syslog_facility = LOG_USER)
28        : ident_(std::move(ident))
29    {
30        priorities_[static_cast<size_t>(level::trace)] = LOG_DEBUG;
31        priorities_[static_cast<size_t>(level::debug)] = LOG_DEBUG;
32        priorities_[static_cast<size_t>(level::info)] = LOG_INFO;
33        priorities_[static_cast<size_t>(level::warn)] = LOG_WARNING;
34        priorities_[static_cast<size_t>(level::err)] = LOG_ERR;
35        priorities_[static_cast<size_t>(level::critical)] = LOG_CRIT;
36        priorities_[static_cast<size_t>(level::off)] = LOG_INFO;
37
38        // set ident to be program name if empty
39        ::openlog(ident_.empty() ? nullptr : ident_.c_str(), syslog_option, syslog_facility);
40    }
41
42    ~syslog_sink() override
43    {
44        ::closelog();
45    }
46
47    syslog_sink(const syslog_sink &) = delete;
48    syslog_sink &operator=(const syslog_sink &) = delete;
49
50protected:
51    void sink_it_(const details::log_msg &msg) override
52    {
53        ::syslog(syslog_prio_from_level(msg), "%s", fmt::to_string(msg.raw).c_str());
54    }
55
56    void flush_() override {}
57
58private:
59    std::array<int, 7> priorities_;
60    // must store the ident because the man says openlog might use the pointer as
61    // is and not a string copy
62    const std::string ident_;
63
64    //
65    // Simply maps spdlog's log level to syslog priority level.
66    //
67    int syslog_prio_from_level(const details::log_msg &msg) const
68    {
69        return priorities_[static_cast<size_t>(msg.level)];
70    }
71};
72
73using syslog_sink_mt = syslog_sink<std::mutex>;
74using syslog_sink_st = syslog_sink<details::null_mutex>;
75} // namespace sinks
76
77// Create and register a syslog logger
78template<typename Factory = default_factory>
79inline std::shared_ptr<logger> syslog_logger_mt(
80    const std::string &logger_name, const std::string &syslog_ident = "", int syslog_option = 0, int syslog_facility = (1 << 3))
81{
82    return Factory::template create<sinks::syslog_sink_mt>(logger_name, syslog_ident, syslog_option, syslog_facility);
83}
84
85template<typename Factory = default_factory>
86inline std::shared_ptr<logger> syslog_logger_st(
87    const std::string &logger_name, const std::string &syslog_ident = "", int syslog_option = 0, int syslog_facility = (1 << 3))
88{
89    return Factory::template create<sinks::syslog_sink_st>(logger_name, syslog_ident, syslog_option, syslog_facility);
90}
91} // namespace spdlog
92