Line data Source code
1 : /*
2 : * License:
3 : * Copyright (c) 2013-2019 Made to Order Software Corp. All Rights Reserved
4 : *
5 : * https://snapwebsites.org/
6 : * contact@m2osw.com
7 : *
8 : * This program is free software; you can redistribute it and/or modify
9 : * it under the terms of the GNU General Public License as published by
10 : * the Free Software Foundation; either version 2 of the License, or
11 : * (at your option) any later version.
12 : *
13 : * This program is distributed in the hope that it will be useful,
14 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : * GNU General Public License for more details.
17 : *
18 : * You should have received a copy of the GNU General Public License along
19 : * with this program; if not, write to the Free Software Foundation, Inc.,
20 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 : *
22 : * Authors:
23 : * Alexis Wilke alexis@m2osw.com
24 : */
25 :
26 : /** \file
27 : * \brief Appenders are used to append data to somewhere.
28 : *
29 : * This file declares the base appender class.
30 : */
31 :
32 : // self
33 : //
34 : #include "snaplogger/syslog_appender.h"
35 :
36 :
37 : // C++ lib
38 : //
39 : #include <iostream>
40 :
41 :
42 : // C lib
43 : //
44 : #include <syslog.h>
45 :
46 :
47 : // last include
48 : //
49 : #include <snapdev/poison.h>
50 :
51 :
52 :
53 : namespace snaplogger
54 : {
55 :
56 :
57 :
58 : namespace
59 : {
60 :
61 :
62 8 : APPENDER_FACTORY(syslog);
63 :
64 :
65 : struct facility_by_name
66 : {
67 : char const * const f_name = nullptr;
68 : int const f_facility = LOG_USER;
69 : };
70 :
71 :
72 : constexpr facility_by_name g_facility_by_name[] =
73 : {
74 : { "auth", LOG_AUTH },
75 : { "authpriv", LOG_AUTHPRIV },
76 : { "cron", LOG_CRON },
77 : { "daemon", LOG_DAEMON },
78 : { "ftp", LOG_FTP },
79 : { "kern", LOG_KERN },
80 : { "local0", LOG_LOCAL0 },
81 : { "local1", LOG_LOCAL1 },
82 : { "local2", LOG_LOCAL2 },
83 : { "local3", LOG_LOCAL3 },
84 : { "local4", LOG_LOCAL4 },
85 : { "local5", LOG_LOCAL5 },
86 : { "local6", LOG_LOCAL6 },
87 : { "local7", LOG_LOCAL7 },
88 : { "lpr", LOG_LPR },
89 : { "mail", LOG_MAIL },
90 : { "news", LOG_NEWS },
91 : { "syslog", LOG_SYSLOG },
92 : { "user", LOG_USER },
93 : { "uucp", LOG_UUCP }
94 : };
95 :
96 :
97 :
98 :
99 : }
100 : // no name namespace
101 :
102 :
103 :
104 0 : syslog_appender::syslog_appender(std::string const name)
105 0 : : appender(name, "syslog")
106 : {
107 0 : }
108 :
109 :
110 0 : syslog_appender::~syslog_appender()
111 : {
112 0 : }
113 :
114 :
115 0 : bool syslog_appender::unique() const
116 : {
117 0 : return true;
118 : }
119 :
120 :
121 0 : void syslog_appender::set_config(advgetopt::getopt const & opts)
122 : {
123 0 : closelog();
124 :
125 0 : appender::set_config(opts);
126 :
127 : // FACILITY
128 : //
129 0 : int facility(LOG_USER);
130 0 : std::string const facility_field(get_name() + "::facility");
131 0 : if(opts.is_defined(facility_field))
132 : {
133 0 : std::string const facility_name(opts.get_string(facility_field));
134 0 : int i(0);
135 0 : int j(sizeof(g_facility_by_name) / sizeof(g_facility_by_name[0]));
136 0 : while(i < j)
137 : {
138 0 : int const p(i + (j - i) / 2);
139 0 : int const r(facility_name.compare(g_facility_by_name[p].f_name));
140 0 : if(r == 0)
141 : {
142 0 : facility = g_facility_by_name[p].f_facility;
143 0 : break;
144 : }
145 0 : if(r < 0)
146 : {
147 0 : i = p + 1;
148 : }
149 : else // if(r > 0)
150 : {
151 0 : j = p;
152 : }
153 : }
154 : }
155 :
156 : // IDENTITY
157 : //
158 0 : std::string const identity_field(get_name() + "::identity");
159 0 : if(opts.is_defined(identity_field))
160 : {
161 0 : f_identity = opts.get_string(identity_field);
162 : }
163 :
164 : // FALLBACK TO CONSOLE
165 : //
166 0 : int options(LOG_NDELAY | LOG_PID);
167 0 : std::string const fallback_to_console_field(get_name() + "::fallback_to_console");
168 0 : if(opts.is_defined(fallback_to_console_field))
169 : {
170 0 : if(opts.get_string(fallback_to_console_field) == "true")
171 : {
172 0 : options |= LOG_CONS;
173 : }
174 : }
175 :
176 : // open the log immediately
177 : //
178 0 : openlog(f_identity.c_str(), options, facility);
179 0 : }
180 :
181 :
182 0 : void syslog_appender::process_message(message const & msg, std::string const & formatted_message)
183 : {
184 0 : int priority(LOG_INFO);
185 0 : severity_t const sev(msg.get_severity());
186 0 : if(sev <= severity_t::SEVERITY_DEBUG)
187 : {
188 0 : priority = LOG_DEBUG;
189 : }
190 0 : else if(sev <= severity_t::SEVERITY_INFORMATION)
191 : {
192 0 : priority = LOG_INFO;
193 : }
194 0 : else if(sev <= severity_t::SEVERITY_MINOR)
195 : {
196 0 : priority = LOG_NOTICE;
197 : }
198 0 : else if(sev <= severity_t::SEVERITY_MAJOR)
199 : {
200 0 : priority = LOG_WARNING;
201 : }
202 0 : else if(sev <= severity_t::SEVERITY_ERROR)
203 : {
204 0 : priority = LOG_ERR;
205 : }
206 0 : else if(sev <= severity_t::SEVERITY_CRITICAL)
207 : {
208 0 : priority = LOG_CRIT;
209 : }
210 0 : else if(sev <= severity_t::SEVERITY_ALERT)
211 : {
212 0 : priority = LOG_ALERT;
213 : }
214 : else
215 : {
216 0 : priority = LOG_EMERG;
217 : }
218 :
219 0 : syslog(priority, "%s", formatted_message.c_str());
220 0 : }
221 :
222 :
223 :
224 :
225 :
226 6 : } // snaplogger namespace
227 : // vim: ts=4 sw=4 et
|