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 "opc/spdlog/common.h"
9 
10 #ifdef SPDLOG_ENABLE_SYSLOG
11 
12 #include "opc/spdlog/sinks/sink.h"
14 
15 #include <array>
16 #include <string>
17 #include <syslog.h>
18 
19 
20 namespace spdlog
21 {
22 namespace sinks
23 {
29 class syslog_sink : public sink
30 {
31 public:
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 
65 private:
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
#define LOG_DEBUG(__logger__,...)
Definition: common/logger.h:24
OStream< ChannelType > & flush(OStream< ChannelType > &os)
Definition: stream.h:147
#define LOG_INFO(__logger__,...)
Definition: common/logger.h:25


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:12:08