Fawkes API
Fawkes Development Version
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
All
Classes
Namespaces
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Groups
Pages
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
63
LibLogger::init
(
MultiLogger
*multi_logger)
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
83
LibLogger::finalize
()
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
95
LibLogger::add_logger
(
Logger
*l)
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
109
LibLogger::remove_logger
(
Logger
*l)
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
src
libs
logging
liblogger.cpp
Generated by
1.8.1.2