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