Ark Server API (ASE) - Wiki
Loading...
Searching...
No Matches
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#include "../common.h"
9
10#ifdef SPDLOG_ENABLE_SYSLOG
11
12#include "sink.h"
13#include "../details/log_msg.h"
14
15#include <array>
16#include <string>
17#include <syslog.h>
18
19
20namespace spdlog
21{
22namespace sinks
23{
24/**
25 * Sink that write to syslog using the `syscall()` library call.
26 *
27 * Locking is not needed, as `syslog()` itself is thread-safe.
28 */
29class syslog_sink : public sink
30{
31public:
32 //
33 syslog_sink(const std::string& ident = "", int syslog_option=0, int syslog_facility=LOG_USER):
34 _ident(ident)
35 {
36 _priorities[static_cast<int>(level::trace)] = LOG_DEBUG;
37 _priorities[static_cast<int>(level::debug)] = LOG_DEBUG;
38 _priorities[static_cast<int>(level::info)] = LOG_INFO;
39 _priorities[static_cast<int>(level::warn)] = LOG_WARNING;
40 _priorities[static_cast<int>(level::err)] = LOG_ERR;
41 _priorities[static_cast<int>(level::critical)] = LOG_CRIT;
42 _priorities[static_cast<int>(level::off)] = LOG_INFO;
43
44 //set ident to be program name if empty
45 ::openlog(_ident.empty()? nullptr:_ident.c_str(), syslog_option, syslog_facility);
46 }
47 ~syslog_sink()
48 {
49 ::closelog();
50 }
51
52 syslog_sink(const syslog_sink&) = delete;
53 syslog_sink& operator=(const syslog_sink&) = delete;
54
55 void log(const details::log_msg &msg) override
56 {
57 ::syslog(syslog_prio_from_level(msg), "%s", msg.raw.str().c_str());
58 }
59
60 void flush() override
61 {
62 }
63
64
65private:
66 std::array<int, 7> _priorities;
67 //must store the ident because the man says openlog might use the pointer as is and not a string copy
68 const std::string _ident;
69
70 //
71 // Simply maps spdlog's log level to syslog priority level.
72 //
73 int syslog_prio_from_level(const details::log_msg &msg) const
74 {
75 return _priorities[static_cast<int>(msg.level)];
76 }
77};
78}
79}
80
81#endif