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 }
88 
89 
90 /** Add logger.
91  * @param l sub-logger to add
92  * @see MultiLogger::add_logger()
93  */
94 void
96 {
97  if ( logger == NULL ) init();
98  mutex->lock();
99  logger->add_logger(l);
100  mutex->unlock();
101 }
102 
103 
104 /** Remove logger.
105  * @param l sub-logger to remove
106  * @see MultiLogger::remove_logger()
107  */
108 void
110 {
111  if ( logger == NULL ) init();
112  mutex->lock();
113  logger->remove_logger(l);
114  mutex->unlock();
115 }
116 
117 
118 /** Log debug message.
119  * @param component component, used to distuinguish logged messages
120  * @param format format of the message, see man page of sprintf for available
121  * tokens.
122  */
123 void
124 LibLogger::log_debug(const char *component, const char *format, ...)
125 {
126  if ( logger == NULL ) init();
127  mutex->lock();
128  va_list va;
129  va_start(va, format);
130  logger->vlog_debug(component, format, va);
131  va_end(va);
132  mutex->unlock();
133 }
134 
135 
136 /** Log informational message.
137  * @param component component, used to distuinguish logged messages
138  * @param format format of the message, see man page of sprintf for available
139  * tokens.
140  */
141 void
142 LibLogger::log_info(const char *component, const char *format, ...)
143 {
144  if ( logger == NULL ) init();
145  mutex->lock();
146  va_list va;
147  va_start(va, format);
148  logger->vlog_info(component, format, va);
149  va_end(va);
150  mutex->unlock();
151 }
152 
153 
154 /** Log warning message.
155  * @param component component, used to distuinguish logged messages
156  * @param format format of the message, see man page of sprintf for available
157  * tokens.
158  */
159 void
160 LibLogger::log_warn(const char *component, const char *format, ...)
161 {
162  if ( logger == NULL ) init();
163  mutex->lock();
164  va_list va;
165  va_start(va, format);
166  logger->vlog_warn(component, format, va);
167  va_end(va);
168  mutex->unlock();
169 }
170 
171 
172 /** Log error message.
173  * @param component component, used to distuinguish logged messages
174  * @param format format of the message, see man page of sprintf for available
175  * tokens.
176  */
177 void
178 LibLogger::log_error(const char *component, const char *format, ...)
179 {
180  if ( logger == NULL ) init();
181  mutex->lock();
182  va_list va;
183  va_start(va, format);
184  logger->vlog_error(component, format, va);
185  va_end(va);
186  mutex->unlock();
187 }
188 
189 
190 /** Log debug message.
191  * @param component component, used to distuinguish logged messages
192  * @param format format of the message, see man page of sprintf for available
193  * tokens.
194  * @param va variadic argument list
195  */
196 void
197 LibLogger::vlog_debug(const char *component, const char *format, va_list va)
198 {
199  if ( logger == NULL ) init();
200  mutex->lock();
201  logger->vlog_debug(component, format, va);
202  mutex->unlock();
203 }
204 
205 
206 /** Log informational message.
207  * @param component component, used to distuinguish logged messages
208  * @param format format of the message, see man page of sprintf for available
209  * tokens.
210  * @param va variadic argument list
211  */
212 void
213 LibLogger::vlog_info(const char *component, const char *format, va_list va)
214 {
215  if ( logger == NULL ) init();
216  mutex->lock();
217  logger->vlog_info(component, format, va);
218  mutex->unlock();
219 }
220 
221 
222 /** Log warning message.
223  * @param component component, used to distuinguish logged messages
224  * @param format format of the message, see man page of sprintf for available
225  * tokens.
226  * @param va variadic argument list
227  */
228 void
229 LibLogger::vlog_warn(const char *component, const char *format, va_list va)
230 {
231  if ( logger == NULL ) init();
232  mutex->lock();
233  logger->vlog_warn(component, format, va);
234  mutex->unlock();
235 }
236 
237 
238 /** Log error message.
239  * @param component component, used to distuinguish logged messages
240  * @param format format of the message, see man page of sprintf for available
241  * tokens.
242  * @param va variadic argument list
243  */
244 void
245 LibLogger::vlog_error(const char *component, const char *format, va_list va)
246 {
247  if ( logger == NULL ) init();
248  mutex->lock();
249  logger->vlog_error(component, format, va);
250  mutex->unlock();
251 }
252 
253 
254 
255 /** Log debug message.
256  * @param component component, used to distuinguish logged messages
257  * @param e exception to log, exception messages will be logged
258  */
259 void
260 LibLogger::log_debug(const char *component, Exception &e)
261 {
262  if ( logger == NULL ) init();
263  mutex->lock();
264  logger->log_debug(component, e);
265  mutex->unlock();
266 }
267 
268 /** Log informational message.
269  * @param component component, used to distuinguish logged messages
270  * @param e exception to log, exception messages will be logged
271  */
272 void
273 LibLogger::log_info(const char *component, Exception &e)
274 {
275  if ( logger == NULL ) init();
276  mutex->lock();
277  logger->log_info(component, e);
278  mutex->unlock();
279 }
280 
281 
282 /** Log warning message.
283  * @param component component, used to distuinguish logged messages
284  * @param e exception to log, exception messages will be logged
285  */
286 void
287 LibLogger::log_warn(const char *component, Exception &e)
288 {
289  if ( logger == NULL ) init();
290  mutex->lock();
291  logger->log_warn(component, e);
292  mutex->unlock();
293 }
294 
295 
296 /** Log error message.
297  * @param component component, used to distuinguish logged messages
298  * @param e exception to log, exception messages will be logged
299  */
300 void
301 LibLogger::log_error(const char *component, Exception &e)
302 {
303  if ( logger == NULL ) init();
304  mutex->lock();
305  logger->log_error(component, e);
306  mutex->unlock();
307 }
308 
309 
310 } // 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:142
static void log_debug(const char *component, const char *format,...)
Log debug message.
Definition: liblogger.cpp:124
virtual void log_error(const char *component, const char *format,...)
Log error message.
Definition: multi.cpp:247
Interface for logging to stderr.
Definition: console.h:35
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:229
static void vlog_info(const char *component, const char *format, va_list va)
Log informational message.
Definition: liblogger.cpp:213
static void vlog_debug(const char *component, const char *format, va_list va)
Log debug message.
Definition: liblogger.cpp:197
Log through multiple loggers.
Definition: multi.h:35
static void log_error(const char *component, const char *format,...)
Log error message.
Definition: liblogger.cpp:178
static void vlog_error(const char *component, const char *format, va_list va)
Log error message.
Definition: liblogger.cpp:245
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:426
virtual void vlog_debug(const char *component, const char *format, va_list va)
Log debug message.
Definition: multi.cpp:369
virtual void log_warn(const char *component, const char *format,...)
Log warning message.
Definition: multi.cpp:225
virtual void vlog_info(const char *component, const char *format, va_list va)
Log informational message.
Definition: multi.cpp:388
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:407
static void log_warn(const char *component, const char *format,...)
Log warning message.
Definition: liblogger.cpp:160
virtual void log_info(const char *component, const char *format,...)
Log informational message.
Definition: multi.cpp:203
static void init(MultiLogger *multi_logger=NULL)
Initialize logger.
Definition: liblogger.cpp:63
static void add_logger(Logger *logger)
Add logger.
Definition: liblogger.cpp:95
static void remove_logger(Logger *logger)
Remove logger.
Definition: liblogger.cpp:109
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:181
void remove_logger(Logger *logger)
Remove logger.
Definition: multi.cpp:133
Interface for logging.
Definition: logger.h:34