Fawkes API  Fawkes Development Version
liblogger.cpp
1 
2 /***************************************************************************
3  * liblogger.h - Fawkes lib logger
4  *
5  * Created: Mon May 07 15:22:18 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/liblogger.h>
25 #include <logging/multi.h>
26 #include <logging/console.h>
27 
28 #include <core/exceptions/software.h>
29 #include <core/threading/mutex.h>
30 
31 namespace fawkes {
32 
33 /** @class LibLogger <logging/liblogger.h>
34  * Library logger.
35  * This logger is meant to be used in libraries that depend on utils anyway
36  * and in utils itself. This logger is completely static so it only has to be
37  * initialized once per process. If the logger is used before it has been
38  * initialized it is automatically initialized with an empty MultiLogger.
39  * If you want to see output you have to make sure that you add loggers
40  * like the ConsoleLogger.
41  *
42  * Make sure that you call finalize() at the end of the surrounding process
43  * to free all the loggers associcated with the internal multi logger and
44  * the multi logger itself.
45  *
46  * @see MultiLogger
47  * @author Tim Niemueller
48  */
49 
50 
51 /** The internal multi logger. */
52 MultiLogger * LibLogger::logger = NULL;
53 /** Internal mutex */
54 Mutex * LibLogger::mutex = NULL;
55 
56 
57 /** Initialize logger.
58  * @param multi_logger Logger to use in this multi logger. If NULL a new
59  * logger is created. Note that LibLogger takes over ownership of the
60  * multi logger and will destroy it if finalize() is called.
61  */
62 void
64 {
65  if ( logger != NULL ) {
66  throw AccessViolationException("LibLogger already initialized");
67  }
68  mutex = new Mutex();
69  if ( multi_logger == NULL ) {
70  logger = new MultiLogger(new ConsoleLogger());
71  } else {
72  logger = multi_logger;
73  }
74 
75 }
76 
77 
78 /** Delete internal logger.
79  * Note that the multi logger took over ownership of the loggers.
80  * @see MultiLogger
81  */
82 void
84 {
85  delete logger;
86  delete mutex;
87  logger = NULL;
88  mutex = NULL;
89 }
90 
91 
92 /** Add logger.
93  * @param l sub-logger to add
94  * @see MultiLogger::add_logger()
95  */
96 void
98 {
99  if ( logger == NULL ) init();
100  mutex->lock();
101  logger->add_logger(l);
102  mutex->unlock();
103 }
104 
105 
106 /** Remove logger.
107  * @param l sub-logger to remove
108  * @see MultiLogger::remove_logger()
109  */
110 void
112 {
113  if ( logger == NULL ) init();
114  mutex->lock();
115  logger->remove_logger(l);
116  mutex->unlock();
117 }
118 
119 
120 /** Log debug 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  */
125 void
126 LibLogger::log_debug(const char *component, const char *format, ...)
127 {
128  if ( logger == NULL ) init();
129  mutex->lock();
130  va_list va;
131  va_start(va, format);
132  logger->vlog_debug(component, format, va);
133  va_end(va);
134  mutex->unlock();
135 }
136 
137 
138 /** Log informational message.
139  * @param component component, used to distuinguish logged messages
140  * @param format format of the message, see man page of sprintf for available
141  * tokens.
142  */
143 void
144 LibLogger::log_info(const char *component, const char *format, ...)
145 {
146  if ( logger == NULL ) init();
147  mutex->lock();
148  va_list va;
149  va_start(va, format);
150  logger->vlog_info(component, format, va);
151  va_end(va);
152  mutex->unlock();
153 }
154 
155 
156 /** Log warning message.
157  * @param component component, used to distuinguish logged messages
158  * @param format format of the message, see man page of sprintf for available
159  * tokens.
160  */
161 void
162 LibLogger::log_warn(const char *component, const char *format, ...)
163 {
164  if ( logger == NULL ) init();
165  mutex->lock();
166  va_list va;
167  va_start(va, format);
168  logger->vlog_warn(component, format, va);
169  va_end(va);
170  mutex->unlock();
171 }
172 
173 
174 /** Log error message.
175  * @param component component, used to distuinguish logged messages
176  * @param format format of the message, see man page of sprintf for available
177  * tokens.
178  */
179 void
180 LibLogger::log_error(const char *component, const char *format, ...)
181 {
182  if ( logger == NULL ) init();
183  mutex->lock();
184  va_list va;
185  va_start(va, format);
186  logger->vlog_error(component, format, va);
187  va_end(va);
188  mutex->unlock();
189 }
190 
191 
192 /** Log debug message.
193  * @param component component, used to distuinguish logged messages
194  * @param format format of the message, see man page of sprintf for available
195  * tokens.
196  * @param va variadic argument list
197  */
198 void
199 LibLogger::vlog_debug(const char *component, const char *format, va_list va)
200 {
201  if ( logger == NULL ) init();
202  mutex->lock();
203  logger->vlog_debug(component, format, va);
204  mutex->unlock();
205 }
206 
207 
208 /** Log informational message.
209  * @param component component, used to distuinguish logged messages
210  * @param format format of the message, see man page of sprintf for available
211  * tokens.
212  * @param va variadic argument list
213  */
214 void
215 LibLogger::vlog_info(const char *component, const char *format, va_list va)
216 {
217  if ( logger == NULL ) init();
218  mutex->lock();
219  logger->vlog_info(component, format, va);
220  mutex->unlock();
221 }
222 
223 
224 /** Log warning message.
225  * @param component component, used to distuinguish logged messages
226  * @param format format of the message, see man page of sprintf for available
227  * tokens.
228  * @param va variadic argument list
229  */
230 void
231 LibLogger::vlog_warn(const char *component, const char *format, va_list va)
232 {
233  if ( logger == NULL ) init();
234  mutex->lock();
235  logger->vlog_warn(component, format, va);
236  mutex->unlock();
237 }
238 
239 
240 /** Log error message.
241  * @param component component, used to distuinguish logged messages
242  * @param format format of the message, see man page of sprintf for available
243  * tokens.
244  * @param va variadic argument list
245  */
246 void
247 LibLogger::vlog_error(const char *component, const char *format, va_list va)
248 {
249  if ( logger == NULL ) init();
250  mutex->lock();
251  logger->vlog_error(component, format, va);
252  mutex->unlock();
253 }
254 
255 
256 
257 /** Log debug message.
258  * @param component component, used to distuinguish logged messages
259  * @param e exception to log, exception messages will be logged
260  */
261 void
262 LibLogger::log_debug(const char *component, Exception &e)
263 {
264  if ( logger == NULL ) init();
265  mutex->lock();
266  logger->log_debug(component, e);
267  mutex->unlock();
268 }
269 
270 /** Log informational message.
271  * @param component component, used to distuinguish logged messages
272  * @param e exception to log, exception messages will be logged
273  */
274 void
275 LibLogger::log_info(const char *component, Exception &e)
276 {
277  if ( logger == NULL ) init();
278  mutex->lock();
279  logger->log_info(component, e);
280  mutex->unlock();
281 }
282 
283 
284 /** Log warning message.
285  * @param component component, used to distuinguish logged messages
286  * @param e exception to log, exception messages will be logged
287  */
288 void
289 LibLogger::log_warn(const char *component, Exception &e)
290 {
291  if ( logger == NULL ) init();
292  mutex->lock();
293  logger->log_warn(component, e);
294  mutex->unlock();
295 }
296 
297 
298 /** Log error message.
299  * @param component component, used to distuinguish logged messages
300  * @param e exception to log, exception messages will be logged
301  */
302 void
303 LibLogger::log_error(const char *component, Exception &e)
304 {
305  if ( logger == NULL ) init();
306  mutex->lock();
307  logger->log_error(component, e);
308  mutex->unlock();
309 }
310 
311 
312 } // end namespace fawkes
static void finalize()
Delete internal logger.
Definition: liblogger.cpp:83
static void log_info(const char *component, const char *format,...)
Log informational message.
Definition: liblogger.cpp:144
static void log_debug(const char *component, const char *format,...)
Log debug message.
Definition: liblogger.cpp:126
virtual void log_error(const char *component, const char *format,...)
Log error message.
Definition: multi.cpp:249
Interface for logging to stderr.
Definition: console.h:36
Fawkes library namespace.
void unlock()
Unlock the mutex.
Definition: mutex.cpp:135
static void vlog_warn(const char *component, const char *format, va_list va)
Log warning message.
Definition: liblogger.cpp:231
static void vlog_info(const char *component, const char *format, va_list va)
Log informational message.
Definition: liblogger.cpp:215
static void vlog_debug(const char *component, const char *format, va_list va)
Log debug message.
Definition: liblogger.cpp:199
Log through multiple loggers.
Definition: multi.h:35
static void log_error(const char *component, const char *format,...)
Log error message.
Definition: liblogger.cpp:180
static void vlog_error(const char *component, const char *format, va_list va)
Log error message.
Definition: liblogger.cpp:247
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void vlog_error(const char *component, const char *format, va_list va)
Log error message.
Definition: multi.cpp:428
virtual void vlog_debug(const char *component, const char *format, va_list va)
Log debug message.
Definition: multi.cpp:371
virtual void log_warn(const char *component, const char *format,...)
Log warning message.
Definition: multi.cpp:227
virtual void vlog_info(const char *component, const char *format, va_list va)
Log informational message.
Definition: multi.cpp:390
void add_logger(Logger *logger)
Add a logger.
Definition: multi.cpp:115
virtual void vlog_warn(const char *component, const char *format, va_list va)
Log warning message.
Definition: multi.cpp:409
static void log_warn(const char *component, const char *format,...)
Log warning message.
Definition: liblogger.cpp:162
virtual void log_info(const char *component, const char *format,...)
Log informational message.
Definition: multi.cpp:205
static void init(MultiLogger *multi_logger=NULL)
Initialize logger.
Definition: liblogger.cpp:63
static void add_logger(Logger *logger)
Add logger.
Definition: liblogger.cpp:97
static void remove_logger(Logger *logger)
Remove logger.
Definition: liblogger.cpp:111
void lock()
Lock this mutex.
Definition: mutex.cpp:89
Mutex mutual exclusion lock.
Definition: mutex.h:32
Access violates policy.
Definition: software.h:96
virtual void log_debug(const char *component, const char *format,...)
Log debug message.
Definition: multi.cpp:183
void remove_logger(Logger *logger)
Remove logger.
Definition: multi.cpp:134
Interface for logging.
Definition: logger.h:34