Fawkes API Fawkes Development Version
syslog.cpp
1
2/***************************************************************************
3 * syslog.cpp - Fawkes syslog logger
4 *
5 * Created: Thu Aug 18 17:15:30 2011
6 * Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version. A runtime exception applies to
14 * this software (see LICENSE.GPL_WRE file mentioned below for details).
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
20 *
21 * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22 */
23
24#include <core/threading/mutex.h>
25#include <logging/syslog.h>
26#include <sys/syslog.h>
27#include <sys/time.h>
28
29#include <cstdio>
30#include <cstdlib>
31#include <cstring>
32#include <ctime>
33#include <syslog.h>
34
35namespace fawkes {
36
37/** @class SyslogLogger <logging/syslog.h>
38 * Interface for logging to syslog.
39 * The SyslogLogger will pipe all output to the syslog.
40 * @author Tim Niemueller
41 */
42
43/** Constructor.
44 * @param log_level minimum level to log
45 */
47{
48 now_s = (struct ::tm *)malloc(sizeof(struct ::tm));
49 mutex = new Mutex();
50 ident_ = NULL;
51 openlog("Fawkes", LOG_CONS | LOG_NDELAY, LOG_USER);
52}
53
54/** Constructor with ident.
55 * @param ident ident string passed to openlog.
56 * @param log_level minimum level to log
57 */
58SyslogLogger::SyslogLogger(const char *ident, LogLevel log_level) : Logger(log_level)
59{
60 now_s = (struct ::tm *)malloc(sizeof(struct ::tm));
61 mutex = new Mutex();
62 if (ident == NULL) {
63 ident_ = NULL;
64 openlog("Fawkes", LOG_CONS | LOG_NDELAY, LOG_USER);
65 } else {
66 ident_ = strdup(ident);
67 openlog(ident_, LOG_CONS | LOG_NDELAY, LOG_USER);
68 }
69}
70
71/** Destructor. */
73{
74 free(now_s);
75 delete mutex;
76 closelog();
77 if (ident_)
78 free(ident_);
79 ident_ = NULL;
80}
81
82void
83SyslogLogger::vlog_debug(const char *component, const char *format, va_list va)
84{
85 if (log_level <= LL_DEBUG) {
86 mutex->lock();
87 char *message;
88 if (vasprintf(&message, format, va) != -1) {
89 syslog(LOG_DEBUG, "%s: %s", component, message);
90 free(message);
91 } else {
92 vsyslog(LOG_DEBUG, format, va);
93 }
94 mutex->unlock();
95 }
96}
97
98void
99SyslogLogger::vlog_info(const char *component, const char *format, va_list va)
100{
101 if (log_level <= LL_INFO) {
102 mutex->lock();
103 char *message;
104 if (vasprintf(&message, format, va) != -1) {
105 syslog(LOG_INFO, "%s: %s", component, message);
106 free(message);
107 } else {
108 vsyslog(LOG_INFO, format, va);
109 }
110 mutex->unlock();
111 }
112}
113
114void
115SyslogLogger::vlog_warn(const char *component, const char *format, va_list va)
116{
117 if (log_level <= LL_WARN) {
118 mutex->lock();
119 char *message;
120 if (vasprintf(&message, format, va) != -1) {
121 syslog(LOG_WARNING, "%s: %s", component, message);
122 free(message);
123 } else {
124 vsyslog(LOG_WARNING, format, va);
125 }
126 mutex->unlock();
127 }
128}
129
130void
131SyslogLogger::vlog_error(const char *component, const char *format, va_list va)
132{
133 if (log_level <= LL_ERROR) {
134 mutex->lock();
135 char *message;
136 if (vasprintf(&message, format, va) != -1) {
137 syslog(LOG_ERR, "%s: %s", component, message);
138 free(message);
139 } else {
140 vsyslog(LOG_ERR, format, va);
141 }
142 mutex->unlock();
143 }
144}
145
146void
147SyslogLogger::log_debug(const char *component, const char *format, ...)
148{
149 va_list arg;
150 va_start(arg, format);
151 vlog_debug(component, format, arg);
152 va_end(arg);
153}
154
155void
156SyslogLogger::log_info(const char *component, const char *format, ...)
157{
158 va_list arg;
159 va_start(arg, format);
160 vlog_info(component, format, arg);
161 va_end(arg);
162}
163
164void
165SyslogLogger::log_warn(const char *component, const char *format, ...)
166{
167 va_list arg;
168 va_start(arg, format);
169 vlog_warn(component, format, arg);
170 va_end(arg);
171}
172
173void
174SyslogLogger::log_error(const char *component, const char *format, ...)
175{
176 va_list arg;
177 va_start(arg, format);
178 vlog_error(component, format, arg);
179 va_end(arg);
180}
181
182void
183SyslogLogger::log_debug(const char *component, Exception &e)
184{
185 if (log_level <= LL_DEBUG) {
186 mutex->lock();
187 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
188 syslog(LOG_DEBUG, "%s: [EXC] %s", component, *i);
189 }
190 mutex->unlock();
191 }
192}
193
194void
195SyslogLogger::log_info(const char *component, Exception &e)
196{
197 if (log_level <= LL_INFO) {
198 mutex->lock();
199 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
200 syslog(LOG_INFO, "%s: [EXC] %s", component, *i);
201 }
202 mutex->unlock();
203 }
204}
205
206void
207SyslogLogger::log_warn(const char *component, Exception &e)
208{
209 if (log_level <= LL_WARN) {
210 mutex->lock();
211 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
212 syslog(LOG_WARNING, "%s: [EXC] %s", component, *i);
213 }
214 mutex->unlock();
215 }
216}
217
218void
219SyslogLogger::log_error(const char *component, Exception &e)
220{
221 if (log_level <= LL_DEBUG) {
222 mutex->lock();
223 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
224 syslog(LOG_ERR, "%s: [EXC] %s", component, *i);
225 }
226 mutex->unlock();
227 }
228}
229
230void
231SyslogLogger::tlog_debug(struct timeval *t, const char *component, const char *format, ...)
232{
233 va_list arg;
234 va_start(arg, format);
235 vtlog_debug(t, component, format, arg);
236 va_end(arg);
237}
238
239void
240SyslogLogger::tlog_info(struct timeval *t, const char *component, const char *format, ...)
241{
242 va_list arg;
243 va_start(arg, format);
244 vtlog_info(t, component, format, arg);
245 va_end(arg);
246}
247
248void
249SyslogLogger::tlog_warn(struct timeval *t, const char *component, const char *format, ...)
250{
251 va_list arg;
252 va_start(arg, format);
253 vtlog_warn(t, component, format, arg);
254 va_end(arg);
255}
256
257void
258SyslogLogger::tlog_error(struct timeval *t, const char *component, const char *format, ...)
259{
260 va_list arg;
261 va_start(arg, format);
262 vtlog_error(t, component, format, arg);
263 va_end(arg);
264}
265
266void
267SyslogLogger::tlog_debug(struct timeval *t, const char *component, Exception &e)
268{
269 if (log_level <= LL_DEBUG) {
270 mutex->lock();
271 localtime_r(&t->tv_sec, now_s);
272 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
273 syslog(LOG_DEBUG,
274 "%s @ %02d:%02d:%02d.%06ld: [EXC] %s",
275 component,
276 now_s->tm_hour,
277 now_s->tm_min,
278 now_s->tm_sec,
279 (long)t->tv_usec,
280 *i);
281 }
282 mutex->unlock();
283 }
284}
285
286void
287SyslogLogger::tlog_info(struct timeval *t, const char *component, Exception &e)
288{
289 if (log_level <= LL_INFO) {
290 mutex->lock();
291 localtime_r(&t->tv_sec, now_s);
292 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
293 syslog(LOG_INFO,
294 "%s @ %02d:%02d:%02d.%06ld: [EXC] %s",
295 component,
296 now_s->tm_hour,
297 now_s->tm_min,
298 now_s->tm_sec,
299 (long)t->tv_usec,
300 *i);
301 }
302 mutex->unlock();
303 }
304}
305
306void
307SyslogLogger::tlog_warn(struct timeval *t, const char *component, Exception &e)
308{
309 if (log_level <= LL_WARN) {
310 mutex->lock();
311 localtime_r(&t->tv_sec, now_s);
312 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
313 syslog(LOG_WARNING,
314 "%s @ %02d:%02d:%02d.%06ld: [EXC] %s",
315 component,
316 now_s->tm_hour,
317 now_s->tm_min,
318 now_s->tm_sec,
319 (long)t->tv_usec,
320 *i);
321 }
322 mutex->unlock();
323 }
324}
325
326void
327SyslogLogger::tlog_error(struct timeval *t, const char *component, Exception &e)
328{
329 if (log_level <= LL_DEBUG) {
330 mutex->lock();
331 localtime_r(&t->tv_sec, now_s);
332 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
333 syslog(LOG_ERR,
334 "%s @ %02d:%02d:%02d.%06ld: [EXC] %s",
335 component,
336 now_s->tm_hour,
337 now_s->tm_min,
338 now_s->tm_sec,
339 (long)t->tv_usec,
340 *i);
341 }
342 mutex->unlock();
343 }
344}
345
346void
347SyslogLogger::vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va)
348{
349 if (log_level <= LL_DEBUG) {
350 mutex->lock();
351 localtime_r(&t->tv_sec, now_s);
352 char *message;
353 if (vasprintf(&message, format, va) != -1) {
354 syslog(LOG_DEBUG,
355 "%s @ %02d:%02d:%02d.%06ld: %s",
356 component,
357 now_s->tm_hour,
358 now_s->tm_min,
359 now_s->tm_sec,
360 (long)t->tv_usec,
361 message);
362 free(message);
363 } else {
364 vsyslog(LOG_DEBUG, format, va);
365 }
366 mutex->unlock();
367 }
368}
369
370void
371SyslogLogger::vtlog_info(struct timeval *t, const char *component, const char *format, va_list va)
372{
373 if (log_level <= LL_INFO) {
374 mutex->lock();
375 localtime_r(&t->tv_sec, now_s);
376 char *message;
377 if (vasprintf(&message, format, va) != -1) {
378 syslog(LOG_INFO,
379 "%s @ %02d:%02d:%02d.%06ld: %s",
380 component,
381 now_s->tm_hour,
382 now_s->tm_min,
383 now_s->tm_sec,
384 (long)t->tv_usec,
385 message);
386 free(message);
387 } else {
388 vsyslog(LOG_INFO, format, va);
389 }
390 mutex->unlock();
391 }
392}
393
394void
395SyslogLogger::vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va)
396{
397 if (log_level <= LL_WARN) {
398 mutex->lock();
399 localtime_r(&t->tv_sec, now_s);
400 char *message;
401 if (vasprintf(&message, format, va) != -1) {
402 syslog(LOG_WARNING,
403 "%s @ %02d:%02d:%02d.%06ld: %s",
404 component,
405 now_s->tm_hour,
406 now_s->tm_min,
407 now_s->tm_sec,
408 (long)t->tv_usec,
409 message);
410 free(message);
411 } else {
412 vsyslog(LOG_WARNING, format, va);
413 }
414 mutex->unlock();
415 }
416}
417
418void
419SyslogLogger::vtlog_error(struct timeval *t, const char *component, const char *format, va_list va)
420{
421 if (log_level <= LL_ERROR) {
422 mutex->lock();
423 localtime_r(&t->tv_sec, now_s);
424 char *message;
425 if (vasprintf(&message, format, va) != -1) {
426 syslog(LOG_ERR,
427 "%s @ %02d:%02d:%02d.%06ld: %s",
428 component,
429 now_s->tm_hour,
430 now_s->tm_min,
431 now_s->tm_sec,
432 (long)t->tv_usec,
433 message);
434 free(message);
435 } else {
436 vsyslog(LOG_ERR, format, va);
437 }
438 mutex->unlock();
439 }
440}
441
442} // end namespace fawkes
Message iterator for exceptions.
Definition: exception.h:73
Base class for exceptions in Fawkes.
Definition: exception.h:36
iterator end() noexcept
Get end iterator for messages.
Definition: exception.cpp:692
iterator begin() noexcept
Get iterator for messages.
Definition: exception.cpp:676
Interface for logging.
Definition: logger.h:42
LogLevel
Log level.
Definition: logger.h:51
@ LL_INFO
informational output about normal procedures
Definition: logger.h:53
@ LL_WARN
warning, should be investigated but software still functions, an example is that something was reques...
Definition: logger.h:54
@ LL_ERROR
error, may be recoverable (software still running) or not (software has to terminate).
Definition: logger.h:57
@ LL_DEBUG
debug output, relevant only when tracking down problems
Definition: logger.h:52
LogLevel log_level
Minimum log level.
Definition: logger.h:126
Mutex mutual exclusion lock.
Definition: mutex.h:33
void lock()
Lock this mutex.
Definition: mutex.cpp:87
void unlock()
Unlock the mutex.
Definition: mutex.cpp:131
SyslogLogger(LogLevel log_level=LL_DEBUG)
Constructor.
Definition: syslog.cpp:46
virtual void vtlog_info(struct timeval *t, const char *component, const char *format, va_list va)
Log informational message for specific time.
Definition: syslog.cpp:371
virtual void tlog_warn(struct timeval *t, const char *component, const char *format,...)
Log warning message for specific time.
Definition: syslog.cpp:249
virtual void vlog_error(const char *component, const char *format, va_list va)
Log error message.
Definition: syslog.cpp:131
virtual void vlog_debug(const char *component, const char *format, va_list va)
Log debug message.
Definition: syslog.cpp:83
virtual void log_info(const char *component, const char *format,...)
Log informational message.
Definition: syslog.cpp:156
virtual void vlog_warn(const char *component, const char *format, va_list va)
Log warning message.
Definition: syslog.cpp:115
virtual void vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va)
Log debug message for specific time.
Definition: syslog.cpp:347
virtual void vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va)
Log warning message for specific time.
Definition: syslog.cpp:395
virtual void log_error(const char *component, const char *format,...)
Log error message.
Definition: syslog.cpp:174
virtual void tlog_error(struct timeval *t, const char *component, const char *format,...)
Log error message for specific time.
Definition: syslog.cpp:258
virtual void tlog_info(struct timeval *t, const char *component, const char *format,...)
Log informational message for specific time.
Definition: syslog.cpp:240
virtual void log_debug(const char *component, const char *format,...)
Log debug message.
Definition: syslog.cpp:147
virtual void log_warn(const char *component, const char *format,...)
Log warning message.
Definition: syslog.cpp:165
virtual void vtlog_error(struct timeval *t, const char *component, const char *format, va_list va)
Log error message for specific time.
Definition: syslog.cpp:419
virtual void vlog_info(const char *component, const char *format, va_list va)
Log informational message.
Definition: syslog.cpp:99
virtual ~SyslogLogger()
Destructor.
Definition: syslog.cpp:72
virtual void tlog_debug(struct timeval *t, const char *component, const char *format,...)
Log debug message for specific time.
Definition: syslog.cpp:231
Fawkes library namespace.