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