1//
2// SyslogChannel.cpp
3//
4// Library: Foundation
5// Package: Logging
6// Module: SyslogChannel
7//
8// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/SyslogChannel.h"
16#include "Poco/Message.h"
17#include "Poco/StringTokenizer.h"
18#include <syslog.h>
19
20
21namespace Poco {
22
23
24const std::string SyslogChannel::PROP_NAME = "name";
25const std::string SyslogChannel::PROP_FACILITY = "facility";
26const std::string SyslogChannel::PROP_OPTIONS = "options";
27
28
29SyslogChannel::SyslogChannel():
30 _options(SYSLOG_CONS),
31 _facility(SYSLOG_USER),
32 _open(false)
33{
34}
35
36
37SyslogChannel::SyslogChannel(const std::string& name, int options, int facility):
38 _name(name),
39 _options(options),
40 _facility(facility),
41 _open(false)
42{
43}
44
45
46SyslogChannel::~SyslogChannel()
47{
48 close();
49}
50
51
52void SyslogChannel::open()
53{
54 openlog(_name.c_str(), _options, _facility);
55 _open = true;
56}
57
58
59void SyslogChannel::close()
60{
61 if (_open)
62 {
63 closelog();
64 _open = false;
65 }
66}
67
68
69void SyslogChannel::log(const Message& msg)
70{
71 if (!_open) open();
72 syslog(getPrio(msg), "%s", msg.getText().c_str());
73}
74
75
76void SyslogChannel::setProperty(const std::string& name, const std::string& value)
77{
78 if (name == PROP_NAME)
79 {
80 _name = value;
81 }
82 else if (name == PROP_FACILITY)
83 {
84 if (value == "LOG_KERN")
85 _facility = SYSLOG_KERN;
86 else if (value == "LOG_USER")
87 _facility = SYSLOG_USER;
88 else if (value == "LOG_MAIL")
89 _facility = SYSLOG_MAIL;
90 else if (value == "LOG_DAEMON")
91 _facility = SYSLOG_DAEMON;
92 else if (value == "LOG_AUTH")
93 _facility = SYSLOG_AUTH;
94 else if (value == "LOG_AUTHPRIV")
95 _facility = SYSLOG_AUTHPRIV;
96 else if (value == "LOG_SYSLOG")
97 _facility = SYSLOG_SYSLOG;
98 else if (value == "LOG_LPR")
99 _facility = SYSLOG_LPR;
100 else if (value == "LOG_NEWS")
101 _facility = SYSLOG_NEWS;
102 else if (value == "LOG_UUCP")
103 _facility = SYSLOG_UUCP;
104 else if (value == "LOG_CRON")
105 _facility = SYSLOG_CRON;
106 else if (value == "LOG_FTP")
107 _facility = SYSLOG_FTP;
108 else if (value == "LOG_LOCAL0")
109 _facility = SYSLOG_LOCAL0;
110 else if (value == "LOG_LOCAL1")
111 _facility = SYSLOG_LOCAL1;
112 else if (value == "LOG_LOCAL2")
113 _facility = SYSLOG_LOCAL2;
114 else if (value == "LOG_LOCAL3")
115 _facility = SYSLOG_LOCAL3;
116 else if (value == "LOG_LOCAL4")
117 _facility = SYSLOG_LOCAL4;
118 else if (value == "LOG_LOCAL5")
119 _facility = SYSLOG_LOCAL5;
120 else if (value == "LOG_LOCAL6")
121 _facility = SYSLOG_LOCAL6;
122 else if (value == "LOG_LOCAL7")
123 _facility = SYSLOG_LOCAL7;
124 }
125 else if (name == PROP_OPTIONS)
126 {
127 _options = 0;
128 StringTokenizer tokenizer(value, "|+:;,", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
129 for (StringTokenizer::Iterator it = tokenizer.begin(); it != tokenizer.end(); ++it)
130 {
131 if (*it == "LOG_CONS")
132 _options |= SYSLOG_CONS;
133 else if (*it == "LOG_NDELAY")
134 _options |= SYSLOG_NDELAY;
135 else if (*it == "LOG_PERROR")
136 _options |= SYSLOG_PERROR;
137 else if (*it == "LOG_PID")
138 _options |= SYSLOG_PID;
139 }
140 }
141 else
142 {
143 Channel::setProperty(name, value);
144 }
145}
146
147
148std::string SyslogChannel::getProperty(const std::string& name) const
149{
150 if (name == PROP_NAME)
151 {
152 return _name;
153 }
154 else if (name == PROP_FACILITY)
155 {
156 if (_facility == SYSLOG_KERN)
157 return "LOG_KERN";
158 else if (_facility == SYSLOG_USER)
159 return "LOG_USER";
160 else if (_facility == SYSLOG_MAIL)
161 return "LOG_MAIL";
162 else if (_facility == SYSLOG_DAEMON)
163 return "LOG_DAEMON";
164 else if (_facility == SYSLOG_AUTH)
165 return "LOG_AUTH";
166 else if (_facility == SYSLOG_AUTHPRIV)
167 return "LOG_AUTHPRIV";
168 else if (_facility == SYSLOG_SYSLOG)
169 return "LOG_SYSLOG";
170 else if (_facility == SYSLOG_LPR)
171 return "LOG_LPR";
172 else if (_facility == SYSLOG_NEWS)
173 return "LOG_NEWS";
174 else if (_facility == SYSLOG_UUCP)
175 return "LOG_UUCP";
176 else if (_facility == SYSLOG_CRON)
177 return "LOG_CRON";
178 else if (_facility == SYSLOG_FTP)
179 return "LOG_FTP";
180 else if (_facility == SYSLOG_LOCAL0)
181 return "LOG_LOCAL0";
182 else if (_facility == SYSLOG_LOCAL1)
183 return "LOG_LOCAL1";
184 else if (_facility == SYSLOG_LOCAL2)
185 return "LOG_LOCAL2";
186 else if (_facility == SYSLOG_LOCAL3)
187 return "LOG_LOCAL3";
188 else if (_facility == SYSLOG_LOCAL4)
189 return "LOG_LOCAL4";
190 else if (_facility == SYSLOG_LOCAL5)
191 return "LOG_LOCAL5";
192 else if (_facility == SYSLOG_LOCAL6)
193 return "LOG_LOCAL6";
194 else if (_facility == SYSLOG_LOCAL7)
195 return "LOG_LOCAL7";
196 else
197 return "";
198 }
199 else if (name == PROP_OPTIONS)
200 {
201 std::string result;
202 if (_options & SYSLOG_CONS)
203 {
204 if (!result.empty()) result.append("|");
205 result.append("LOG_CONS");
206 }
207 if (_options & SYSLOG_NDELAY)
208 {
209 if (!result.empty()) result.append("|");
210 result.append("LOG_NDELAY");
211 }
212 if (_options & SYSLOG_PERROR)
213 {
214 if (!result.empty()) result.append("|");
215 result.append("LOG_PERROR");
216 }
217 if (_options & SYSLOG_PID)
218 {
219 if (!result.empty()) result.append("|");
220 result.append("LOG_PID");
221 }
222 return result;
223 }
224 else
225 {
226 return Channel::getProperty(name);
227 }
228}
229
230
231int SyslogChannel::getPrio(const Message& msg)
232{
233 switch (msg.getPriority())
234 {
235 case Message::PRIO_TRACE:
236 case Message::PRIO_DEBUG:
237 return LOG_DEBUG;
238 case Message::PRIO_INFORMATION:
239 return LOG_INFO;
240 case Message::PRIO_NOTICE:
241 return LOG_NOTICE;
242 case Message::PRIO_WARNING:
243 return LOG_WARNING;
244 case Message::PRIO_ERROR:
245 return LOG_ERR;
246 case Message::PRIO_CRITICAL:
247 return LOG_CRIT;
248 case Message::PRIO_FATAL:
249 return LOG_ALERT;
250 default:
251 return 0;
252 }
253}
254
255
256} // namespace Poco
257