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 
35 namespace 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  */
46 SyslogLogger::SyslogLogger(LogLevel log_level) : 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 /** Constructor with ident.
55  * @param ident ident string passed to openlog.
56  * @param log_level minimum level to log
57  */
58 SyslogLogger::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 
82 void
83 SyslogLogger::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 
98 void
99 SyslogLogger::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 
114 void
115 SyslogLogger::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 
130 void
131 SyslogLogger::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 
146 void
147 SyslogLogger::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 
155 void
156 SyslogLogger::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 
164 void
165 SyslogLogger::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 
173 void
174 SyslogLogger::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 
182 void
183 SyslogLogger::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 
194 void
195 SyslogLogger::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 
206 void
207 SyslogLogger::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 
218 void
219 SyslogLogger::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 
230 void
231 SyslogLogger::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 
239 void
240 SyslogLogger::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 
248 void
249 SyslogLogger::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 
257 void
258 SyslogLogger::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 
266 void
267 SyslogLogger::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 
286 void
287 SyslogLogger::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 
306 void
307 SyslogLogger::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 
326 void
327 SyslogLogger::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 
346 void
347 SyslogLogger::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 
370 void
371 SyslogLogger::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 
394 void
395 SyslogLogger::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 
418 void
419 SyslogLogger::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
fawkes::Mutex::lock
void lock()
Lock this mutex.
Definition: mutex.cpp:93
fawkes::Exception::end
iterator end()
Get end iterator for messages.
Definition: exception.cpp:692
fawkes::SyslogLogger::vlog_debug
virtual void vlog_debug(const char *component, const char *format, va_list va)
Definition: syslog.cpp:89
fawkes::SyslogLogger::log_error
virtual void log_error(const char *component, const char *format,...)
Definition: syslog.cpp:180
fawkes::Logger::log_level
LogLevel log_level
Minimum log level.
Definition: logger.h:126
fawkes::Mutex
Definition: mutex.h:38
fawkes::SyslogLogger::tlog_debug
virtual void tlog_debug(struct timeval *t, const char *component, const char *format,...)
Definition: syslog.cpp:237
fawkes::SyslogLogger::vtlog_info
virtual void vtlog_info(struct timeval *t, const char *component, const char *format, va_list va)
Definition: syslog.cpp:377
fawkes::Mutex::unlock
void unlock()
Unlock the mutex.
Definition: mutex.cpp:137
fawkes::Logger::LL_DEBUG
@ LL_DEBUG
debug output, relevant only when tracking down problems
Definition: logger.h:52
fawkes::SyslogLogger::tlog_error
virtual void tlog_error(struct timeval *t, const char *component, const char *format,...)
Definition: syslog.cpp:264
fawkes::SyslogLogger::log_info
virtual void log_info(const char *component, const char *format,...)
Definition: syslog.cpp:162
fawkes::SyslogLogger::log_debug
virtual void log_debug(const char *component, const char *format,...)
Definition: syslog.cpp:153
fawkes::Logger::LL_INFO
@ LL_INFO
informational output about normal procedures
Definition: logger.h:53
fawkes::Logger
Definition: logger.h:41
fawkes::SyslogLogger::vtlog_error
virtual void vtlog_error(struct timeval *t, const char *component, const char *format, va_list va)
Definition: syslog.cpp:425
fawkes
fawkes::SyslogLogger::vlog_warn
virtual void vlog_warn(const char *component, const char *format, va_list va)
Definition: syslog.cpp:121
fawkes::SyslogLogger::log_warn
virtual void log_warn(const char *component, const char *format,...)
Definition: syslog.cpp:171
fawkes::Logger::LL_ERROR
@ LL_ERROR
error, may be recoverable (software still running) or not (software has to terminate).
Definition: logger.h:57
fawkes::SyslogLogger::vtlog_warn
virtual void vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va)
Definition: syslog.cpp:401
fawkes::Exception::begin
iterator begin()
Get iterator for messages.
Definition: exception.cpp:676
fawkes::SyslogLogger::tlog_info
virtual void tlog_info(struct timeval *t, const char *component, const char *format,...)
Definition: syslog.cpp:246
fawkes::SyslogLogger::vtlog_debug
virtual void vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va)
Definition: syslog.cpp:353
fawkes::SyslogLogger::SyslogLogger
SyslogLogger(LogLevel log_level=LL_DEBUG)
Constructor.
Definition: syslog.cpp:52
fawkes::Logger::LL_WARN
@ LL_WARN
warning, should be investigated but software still functions, an example is that something was reques...
Definition: logger.h:54
fawkes::SyslogLogger::vlog_info
virtual void vlog_info(const char *component, const char *format, va_list va)
Definition: syslog.cpp:105
fawkes::Exception::iterator
Definition: exception.h:78
fawkes::SyslogLogger::vlog_error
virtual void vlog_error(const char *component, const char *format, va_list va)
Definition: syslog.cpp:137
fawkes::SyslogLogger::~SyslogLogger
virtual ~SyslogLogger()
Destructor.
Definition: syslog.cpp:78
fawkes::SyslogLogger::tlog_warn
virtual void tlog_warn(struct timeval *t, const char *component, const char *format,...)
Definition: syslog.cpp:255
fawkes::Exception
Definition: exception.h:41