Fawkes API  Fawkes Development Version
logger.cpp
1 
2 /***************************************************************************
3  * logger.cpp - Fawkes logging interface
4  *
5  * Created: Tue Jan 16 20:40:15 2007
6  * Copyright 2006-2007 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 <logging/logger.h>
25 
26 namespace fawkes {
27 
28 /** @class Logger <logging/logger.h>
29  * Interface for logging.
30  * This interface facilitates a way to collect all output, be it debugging
31  * output, informational output, warning or error messages.
32  *
33  * There should be no need no more for usage of printf in the code but
34  * rather a logger should be used instead.
35  *
36  * The LoggingAspect should be added to a Thread that has to log something
37  * (which is likely to be the case).
38  * For library use QuickLog is the recommended way of logging. Do NOT use
39  * these in plugins as it hides a dependency of this plugin.
40  *
41  * A special note to logging hackers: A logger may never ever bounce. This
42  * means that not printing a message is ok in case of an internal error in
43  * the logger, but it may never indicate that error with an exception!
44  * If a logger cannot deliver the messages as it should be (like a network
45  * logger that cannot send because the connection is dead) it should at
46  * least dump it to stderr!
47  *
48  * Loggers have to be fast - damn fast. If a lengthy operations is needed
49  * (like a network connection that may block) messages shall be enqueued
50  * and processed later (for example in a separate thread). This is because
51  * everywhere in the software (even in libraries like the utils) a logger
52  * may be used to log an error that occured (but which is not that critical
53  * that the application should die). In that case a logger which takes to
54  * long is absolutely the wrong thing because this would influence the
55  * performance of the whole software at unpredicted times - while if the
56  * operations are carried out at a specified time or in a separate thread
57  * they do not harm the performance.
58  *
59  * Caution: The line between log_* methods and vlog_* methods is very thin.
60  * You can actually call log_info() with a va_list as the only variadic
61  * parameter in some cases. The call is syntactically correct, but the
62  * result is not what you intended. Thus make sure that you always use the
63  * vlog_* method if you pass along a va_list!
64  *
65  * @fn void Logger::log_debug(const char *component, const char *format, ...) = 0
66  * Log debug message.
67  * @param component component, used to distuinguish logged messages
68  * @param format format of the message, see man page of sprintf for available
69  * tokens.
70  *
71  * @fn void Logger::log_info(const char *component, const char *format, ...) = 0
72  * Log informational message.
73  * @param component component, used to distuinguish logged messages
74  * @param format format of the message, see man page of sprintf for available
75  * tokens.
76  *
77  * @fn void Logger::log_warn(const char *component, const char *format, ...) = 0
78  * Log warning message.
79  * @param component component, used to distuinguish logged messages
80  * @param format format of the message, see man page of sprintf for available
81  * tokens.
82  *
83  * @fn void Logger::log_error(const char *component, const char *format, ...) = 0
84  * Log error message.
85  * @param component component, used to distuinguish logged messages
86  * @param format format of the message, see man page of sprintf for available
87  * tokens.
88  *
89  * @fn void Logger::vlog_debug(const char *component, const char *format, va_list va) = 0
90  * Log debug message.
91  * @param component component, used to distuinguish logged messages
92  * @param format format of the message, see man page of sprintf for available
93  * tokens.
94  * @param va variable argument list
95  *
96  * @fn void Logger::vlog_info(const char *component, const char *format, va_list va) = 0
97  * Log informational message.
98  * @param component component, used to distuinguish logged messages
99  * @param format format of the message, see man page of sprintf for available
100  * tokens.
101  * @param va variable argument list
102  *
103  * @fn void Logger::vlog_warn(const char *component, const char *format, va_list va) = 0
104  * Log warning message.
105  * @param component component, used to distuinguish logged messages
106  * @param format format of the message, see man page of sprintf for available
107  * tokens.
108  * @param va variable argument list
109  *
110  * @fn void Logger::vlog_error(const char *component, const char *format, va_list va) = 0
111  * Log error message.
112  * @param component component, used to distuinguish logged messages
113  * @param format format of the message, see man page of sprintf for available
114  * tokens.
115  * @param va variable argument list
116  *
117  * @fn void Logger::log_debug(const char *component, Exception &e) = 0
118  * Log debug exception.
119  * @param component component, used to distuinguish logged messages
120  * @param e exception to log, exception messages will be logged
121  *
122  * @fn void Logger::log_info(const char *component, Exception &e) = 0
123  * Log informational exception.
124  * @param component component, used to distuinguish logged messages
125  * @param e exception to log, exception messages will be logged
126  *
127  * @fn void Logger::log_warn(const char *component, Exception &e) = 0
128  * Log warning exception.
129  * @param component component, used to distuinguish logged messages
130  * @param e exception to log, exception messages will be logged
131  *
132  * @fn void Logger::log_error(const char *component, Exception &e) = 0
133  * Log error exception.
134  * @param component component, used to distuinguish logged messages
135  * @param e exception to log, exception messages will be logged
136  *
137  * @fn void Logger::tlog_debug(struct timeval *t, const char *component, const char *format, ...) = 0
138  * Log debug message for specific time.
139  * @param t time for this message to log
140  * @param component component, used to distuinguish logged messages
141  * @param format format of the message, see man page of sprintf for available
142  * tokens.
143  *
144  * @fn void Logger::tlog_info(struct timeval *t, const char *component, const char *format, ...) = 0
145  * Log informational message for specific time.
146  * @param t time for this message to log
147  * @param component component, used to distuinguish logged messages
148  * @param format format of the message, see man page of sprintf for available
149  * tokens.
150  *
151  * @fn void Logger::tlog_warn(struct timeval *t, const char *component, const char *format, ...) = 0
152  * Log warning message for specific time.
153  * @param t time for this message to log
154  * @param component component, used to distuinguish logged messages
155  * @param format format of the message, see man page of sprintf for available
156  * tokens.
157  *
158  * @fn void Logger::tlog_error(struct timeval *t, const char *component, const char *format, ...) = 0
159  * Log error message for specific time.
160  * @param t time for this message to log
161  * @param component component, used to distuinguish logged messages
162  * @param format format of the message, see man page of sprintf for available
163  * tokens.
164  *
165  * @fn void Logger::tlog_debug(struct timeval *t, const char *component, Exception &e) = 0
166  * Log debug exception for specific time.
167  * @param t time for this message to log
168  * @param component component, used to distuinguish logged messages
169  * @param e exception to log, exception messages will be logged
170  *
171  * @fn void Logger::tlog_info(struct timeval *t, const char *component, Exception &e) = 0
172  * Log informational exception for specific time.
173  * @param t time for this message to log
174  * @param component component, used to distuinguish logged messages
175  * @param e exception to log, exception messages will be logged
176  *
177  * @fn void Logger::tlog_warn(struct timeval *t, const char *component, Exception &e) = 0
178  * Log warning exception for specific time.
179  * @param t time for this message to log
180  * @param component component, used to distuinguish logged messages
181  * @param e exception to log, exception messages will be logged
182  *
183  * @fn void Logger::tlog_error(struct timeval *t, const char *component, Exception &e) = 0
184  * Log error exception for specific time.
185  * @param t time for this message to log
186  * @param component component, used to distuinguish logged messages
187  * @param e exception to log, exception messages will be logged
188  *
189  * @fn void Logger::vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va) = 0
190  * Log debug message for specific time.
191  * @param t time for this message to log
192  * @param component component, used to distuinguish logged messages
193  * @param format format of the message, see man page of sprintf for available
194  * tokens.
195  * @param va variable argument list
196  *
197  * @fn void Logger::vtlog_info(struct timeval *t, const char *component, const char *format, va_list va) = 0
198  * Log informational message for specific time.
199  * @param t time for this message to log
200  * @param component component, used to distuinguish logged messages
201  * @param format format of the message, see man page of sprintf for available
202  * tokens.
203  * @param va variable argument list
204  *
205  * @fn void Logger::vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va) = 0
206  * Log warning message for specific time.
207  * @param t time for this message to log
208  * @param component component, used to distuinguish logged messages
209  * @param format format of the message, see man page of sprintf for available
210  * tokens.
211  * @param va variable argument list
212  *
213  * @fn void Logger::vtlog_error(struct timeval *t, const char *component, const char *format, va_list va) = 0
214  * Log error message for specific time.
215  * @param t time for this message to log
216  * @param component component, used to distuinguish logged messages
217  * @param format format of the message, see man page of sprintf for available
218  * tokens.
219  * @param va variable argument list
220  *
221  */
222 
223 /** Constructor.
224  * @param log_level log level
225  */
227 {
228  this->log_level = log_level;
229 }
230 
231 
232 /** Virtual empty destructor. */
234 {
235 }
236 
237 
238 /** Sets the log level.
239  * The log level determines the minimum log level. If a message is logged that
240  * is below this level the message is ignored.
241  * @param level new log level
242  */
243 void
245 {
246  log_level = level;
247 }
248 
249 
250 /** Get log level.
251  * @return current log level.
252  */
255 {
256  return log_level;
257 }
258 
259 
260 /** Log message for given log level.
261  * @param level log level
262  * @param component component, used to distuinguish logged messages
263  * @param format format of the message, see man page of sprintf for available
264  * tokens.
265  * @param va variadic argument list
266  */
267 void
269  const char *component, const char *format, va_list va)
270 {
271  if ( log_level <= level ) {
272  switch (level) {
273  case LL_DEBUG: vlog_debug(component, format, va); break;
274  case LL_INFO: vlog_info(component, format, va); break;
275  case LL_WARN: vlog_warn(component, format, va); break;
276  case LL_ERROR: vlog_error(component, format, va); break;
277  default: break;
278  }
279  }
280 }
281 
282 
283 
284 /** Log message for given log level and time.
285  * @param level log level
286  * @param t time
287  * @param component component, used to distuinguish logged messages
288  * @param format format of the message, see man page of sprintf for available
289  * tokens.
290  * @param va variadic argument list
291  */
292 void
293 Logger::vtlog(LogLevel level, struct timeval *t,
294  const char *component, const char *format, va_list va)
295 {
296  if ( log_level <= level ) {
297  switch (level) {
298  case LL_DEBUG: vtlog_debug(t, component, format, va); break;
299  case LL_INFO: vtlog_info(t, component, format, va); break;
300  case LL_WARN: vtlog_warn(t, component, format, va); break;
301  case LL_ERROR: vtlog_error(t, component, format, va); break;
302  default: break;
303  }
304  }
305 }
306 
307 
308 /** Log message of given log level.
309  * @param level log level
310  * @param component component, used to distuinguish logged messages
311  * @param format format of the message, see man page of sprintf for available
312  * tokens.
313  */
314 void
315 Logger::log(LogLevel level, const char *component, const char *format, ...)
316 {
317  if ( log_level <= level ) {
318  va_list va;
319  va_start(va, format);
320  vlog(level, component, format, va);
321  va_end(va);
322  }
323 }
324 
325 
326 /** Log exception for given log level.
327  * @param level log level
328  * @param component component, used to distuinguish logged messages
329  * @param e exception to log, exception messages will be logged
330  */
331 void
332 Logger::log(LogLevel level, const char *component, Exception &e)
333 {
334  if ( log_level <= level ) {
335  switch (level) {
336  case LL_DEBUG: log_debug(component, e); break;
337  case LL_INFO: log_info(component, e); break;
338  case LL_WARN: log_warn(component, e); break;
339  case LL_ERROR: log_error(component, e); break;
340  default: break;
341  }
342  }
343 }
344 
345 
346 
347 /** Log message of given log level and time.
348  * @param t time
349  * @param level log level
350  * @param component component, used to distuinguish logged messages
351  * @param format format of the message, see man page of sprintf for available
352  * tokens.
353  */
354 void
355 Logger::tlog(LogLevel level, struct timeval *t,
356  const char *component, const char *format, ...)
357 {
358  if ( log_level <= level ) {
359  va_list va;
360  va_start(va, format);
361  vtlog(level, t, component, format, va);
362  va_end(va);
363  }
364 }
365 
366 
367 /** Log exception for given log level.
368  * @param t time
369  * @param level log level
370  * @param component component, used to distuinguish logged messages
371  * @param e exception to log, exception messages will be logged
372  */
373 void
374 Logger::tlog(LogLevel level, struct timeval *t, const char *component, Exception &e)
375 {
376  if ( log_level <= level ) {
377  switch (level) {
378  case LL_DEBUG: tlog_debug(t, component, e); break;
379  case LL_INFO: tlog_info(t, component, e); break;
380  case LL_WARN: tlog_warn(t, component, e); break;
381  case LL_ERROR: tlog_error(t, component, e); break;
382  default: break;
383  }
384  }
385 }
386 
387 
388 } // end namespace fawkes
LogLevel
Log level.
Definition: logger.h:45
virtual void tlog_error(struct timeval *t, const char *component, const char *format,...)=0
Log error message for specific time.
virtual void tlog_info(struct timeval *t, const char *component, const char *format,...)=0
Log informational message for specific time.
virtual void vlog_warn(const char *component, const char *format, va_list va)=0
Log warning message.
virtual void vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va)=0
Log warning message for specific time.
LogLevel log_level
Minimum log level.
Definition: logger.h:128
informational output about normal procedures
Definition: logger.h:47
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
virtual void vlog_debug(const char *component, const char *format, va_list va)=0
Log debug message.
Fawkes library namespace.
warning, should be investigated but software still functions, an example is that something was reques...
Definition: logger.h:48
virtual void vlog_error(const char *component, const char *format, va_list va)=0
Log error message.
error, may be recoverable (software still running) or not (software has to terminate).
Definition: logger.h:51
virtual ~Logger()
Virtual empty destructor.
Definition: logger.cpp:233
virtual void tlog_debug(struct timeval *t, const char *component, const char *format,...)=0
Log debug message for specific time.
virtual void vtlog(LogLevel level, struct timeval *t, const char *component, const char *format, va_list va)
Log message for given log level and time.
Definition: logger.cpp:293
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void vlog_info(const char *component, const char *format, va_list va)=0
Log informational message.
virtual void vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va)=0
Log debug message for specific time.
virtual void vlog(LogLevel level, const char *component, const char *format, va_list va)
Log message for given log level.
Definition: logger.cpp:268
Logger(LogLevel log_level=LL_DEBUG)
Constructor.
Definition: logger.cpp:226
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
debug output, relevant only when tracking down problems
Definition: logger.h:46
virtual void tlog_warn(struct timeval *t, const char *component, const char *format,...)=0
Log warning message for specific time.
virtual void vtlog_info(struct timeval *t, const char *component, const char *format, va_list va)=0
Log informational message for specific time.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
virtual void tlog(LogLevel level, struct timeval *t, const char *component, const char *format,...)
Log message of given log level and time.
Definition: logger.cpp:355
virtual LogLevel loglevel()
Get log level.
Definition: logger.cpp:254
virtual void log(LogLevel level, const char *component, const char *format,...)
Log message of given log level.
Definition: logger.cpp:315
virtual void vtlog_error(struct timeval *t, const char *component, const char *format, va_list va)=0
Log error message for specific time.
virtual void set_loglevel(LogLevel level)
Sets the log level.
Definition: logger.cpp:244