source: trunk/GDE/SINA/builddir/include/spdlog/sinks/syslog_sink.h

Last change on this file was 19170, checked in by westram, 2 years ago
  • sina source
    • unpack + remove tarball
    • no longer ignore sina builddir.
File size: 2.9 KB
Line 
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
18namespace spdlog {
19namespace 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 */
25template<typename Mutex>
26class syslog_sink : public base_sink<Mutex>
27{
28public:
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
53protected:
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
61private:
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
65    const std::string ident_;
66
67    //
68    // Simply maps spdlog's log level to syslog priority level.
69    //
70    int syslog_prio_from_level(const details::log_msg &msg) const
71    {
72        return priorities_[static_cast<size_t>(msg.level)];
73    }
74};
75
76using syslog_sink_mt = syslog_sink<std::mutex>;
77using syslog_sink_st = syslog_sink<details::null_mutex>;
78} // namespace sinks
79
80// Create and register a syslog logger
81template<typename Factory = default_factory>
82inline 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
88template<typename Factory = default_factory>
89inline 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
Note: See TracBrowser for help on using the repository browser.