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