Fawkes API Fawkes Development Version
console.cpp
1
2/***************************************************************************
3 * console.cpp - Fawkes console logger
4 *
5 * Created: Tue Jan 16 21:08:25 2007
6 * Copyright 2006-2011 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/threading/mutex.h>
25#include <logging/console.h>
26#include <sys/time.h>
27#include <utils/system/console_colors.h>
28
29#include <cstdlib>
30#include <ctime>
31#include <unistd.h>
32
33namespace fawkes {
34
35/** @class ConsoleLogger <logging/console.h>
36 * Interface for logging to stderr.
37 * The ConsoleLogger will pipe all output to stderr on the console. The
38 * output will be color coded due to the importance of the output.
39 *
40 * Debug output will be drawn in grey font, informational output in console
41 * default color, warnings will be printed in brown/orange and errors in red.
42 *
43 * @author Tim Niemueller
44 */
45
46/** Constructor.
47 * @param log_level minimum level to log
48 */
50{
51 now_s = (struct ::tm *)malloc(sizeof(struct ::tm));
52 mutex = new Mutex();
53 outf_ = fdopen(dup(STDERR_FILENO), "a");
54 // make buffer line-buffered
55 setvbuf(outf_, NULL, _IOLBF, 0);
56}
57
58/** Destructor. */
60{
61 free(now_s);
62 delete mutex;
63 fclose(outf_);
64}
65
66void
67ConsoleLogger::vlog_debug(const char *component, const char *format, va_list va)
68{
69 if (log_level <= LL_DEBUG) {
70 struct timeval now;
71 gettimeofday(&now, NULL);
72 mutex->lock();
73 localtime_r(&now.tv_sec, now_s);
74 fprintf(outf_,
75 "%s%02d:%02d:%02d.%06ld %s: ",
77 now_s->tm_hour,
78 now_s->tm_min,
79 now_s->tm_sec,
80 (long)now.tv_usec,
81 component);
82 vfprintf(outf_, format, va);
83 fprintf(outf_, "%s\n", c_normal);
84 mutex->unlock();
85 }
86}
87
88void
89ConsoleLogger::vlog_info(const char *component, const char *format, va_list va)
90{
91 if (log_level <= LL_INFO) {
92 struct timeval now;
93 gettimeofday(&now, NULL);
94 mutex->lock();
95 localtime_r(&now.tv_sec, now_s);
96 fprintf(outf_,
97 "%02d:%02d:%02d.%06ld %s: ",
98 now_s->tm_hour,
99 now_s->tm_min,
100 now_s->tm_sec,
101 (long)now.tv_usec,
102 component);
103 vfprintf(outf_, format, va);
104 fprintf(outf_, "\n");
105 mutex->unlock();
106 }
107}
108
109void
110ConsoleLogger::vlog_warn(const char *component, const char *format, va_list va)
111{
112 if (log_level <= LL_WARN) {
113 struct timeval now;
114 gettimeofday(&now, NULL);
115 mutex->lock();
116 localtime_r(&now.tv_sec, now_s);
117 fprintf(outf_,
118 "%s%02d:%02d:%02d.%06ld %s: ",
119 c_brown,
120 now_s->tm_hour,
121 now_s->tm_min,
122 now_s->tm_sec,
123 (long)now.tv_usec,
124 component);
125 vfprintf(outf_, format, va);
126 fprintf(outf_, "%s\n", c_normal);
127 mutex->unlock();
128 }
129}
130
131void
132ConsoleLogger::vlog_error(const char *component, const char *format, va_list va)
133{
134 if (log_level <= LL_ERROR) {
135 struct timeval now;
136 gettimeofday(&now, NULL);
137 mutex->lock();
138 localtime_r(&now.tv_sec, now_s);
139 fprintf(outf_,
140 "%s%02d:%02d:%02d.%06ld %s: ",
141 c_red,
142 now_s->tm_hour,
143 now_s->tm_min,
144 now_s->tm_sec,
145 (long)now.tv_usec,
146 component);
147 vfprintf(outf_, format, va);
148 fprintf(outf_, "%s\n", c_normal);
149 mutex->unlock();
150 }
151}
152
153void
154ConsoleLogger::log_debug(const char *component, const char *format, ...)
155{
156 va_list arg;
157 va_start(arg, format);
158 vlog_debug(component, format, arg);
159 va_end(arg);
160}
161
162void
163ConsoleLogger::log_info(const char *component, const char *format, ...)
164{
165 va_list arg;
166 va_start(arg, format);
167 vlog_info(component, format, arg);
168 va_end(arg);
169}
170
171void
172ConsoleLogger::log_warn(const char *component, const char *format, ...)
173{
174 va_list arg;
175 va_start(arg, format);
176 vlog_warn(component, format, arg);
177 va_end(arg);
178}
179
180void
181ConsoleLogger::log_error(const char *component, const char *format, ...)
182{
183 va_list arg;
184 va_start(arg, format);
185 vlog_error(component, format, arg);
186 va_end(arg);
187}
188
189void
190ConsoleLogger::log_debug(const char *component, Exception &e)
191{
192 if (log_level <= LL_DEBUG) {
193 struct timeval now;
194 gettimeofday(&now, NULL);
195 mutex->lock();
196 localtime_r(&now.tv_sec, now_s);
197 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
198 fprintf(outf_,
199 "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ",
201 now_s->tm_hour,
202 now_s->tm_min,
203 now_s->tm_sec,
204 (long)now.tv_usec,
205 component);
206 fprintf(outf_, "%s", *i);
207 fprintf(outf_, "%s\n", c_normal);
208 }
209 mutex->unlock();
210 }
211}
212
213void
214ConsoleLogger::log_info(const char *component, Exception &e)
215{
216 if (log_level <= LL_INFO) {
217 struct timeval now;
218 gettimeofday(&now, NULL);
219 mutex->lock();
220 localtime_r(&now.tv_sec, now_s);
221 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
222 fprintf(outf_,
223 "%02d:%02d:%02d.%06ld %s: [EXCEPTION] ",
224 now_s->tm_hour,
225 now_s->tm_min,
226 now_s->tm_sec,
227 (long)now.tv_usec,
228 component);
229 fprintf(outf_, "%s", *i);
230 fprintf(outf_, "%s\n", c_normal);
231 }
232 mutex->unlock();
233 }
234}
235
236void
237ConsoleLogger::log_warn(const char *component, Exception &e)
238{
239 if (log_level <= LL_WARN) {
240 struct timeval now;
241 gettimeofday(&now, NULL);
242 mutex->lock();
243 localtime_r(&now.tv_sec, now_s);
244 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
245 fprintf(outf_,
246 "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ",
247 c_brown,
248 now_s->tm_hour,
249 now_s->tm_min,
250 now_s->tm_sec,
251 (long)now.tv_usec,
252 component);
253 fprintf(outf_, "%s", *i);
254 fprintf(outf_, "%s\n", c_normal);
255 }
256 mutex->unlock();
257 }
258}
259
260void
261ConsoleLogger::log_error(const char *component, Exception &e)
262{
263 if (log_level <= LL_ERROR) {
264 struct timeval now;
265 gettimeofday(&now, NULL);
266 mutex->lock();
267 localtime_r(&now.tv_sec, now_s);
268 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
269 fprintf(outf_,
270 "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ",
271 c_red,
272 now_s->tm_hour,
273 now_s->tm_min,
274 now_s->tm_sec,
275 (long)now.tv_usec,
276 component);
277 fprintf(outf_, "%s", *i);
278 fprintf(outf_, "%s\n", c_normal);
279 }
280 mutex->unlock();
281 }
282}
283
284void
285ConsoleLogger::tlog_debug(struct timeval *t, const char *component, const char *format, ...)
286{
287 va_list arg;
288 va_start(arg, format);
289 vtlog_debug(t, component, format, arg);
290 va_end(arg);
291}
292
293void
294ConsoleLogger::tlog_info(struct timeval *t, const char *component, const char *format, ...)
295{
296 va_list arg;
297 va_start(arg, format);
298 vtlog_info(t, component, format, arg);
299 va_end(arg);
300}
301
302void
303ConsoleLogger::tlog_warn(struct timeval *t, const char *component, const char *format, ...)
304{
305 va_list arg;
306 va_start(arg, format);
307 vtlog_warn(t, component, format, arg);
308 va_end(arg);
309}
310
311void
312ConsoleLogger::tlog_error(struct timeval *t, const char *component, const char *format, ...)
313{
314 va_list arg;
315 va_start(arg, format);
316 vtlog_error(t, component, format, arg);
317 va_end(arg);
318}
319
320void
321ConsoleLogger::tlog_debug(struct timeval *t, const char *component, Exception &e)
322{
323 if (log_level <= LL_DEBUG) {
324 mutex->lock();
325 localtime_r(&t->tv_sec, now_s);
326 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
327 fprintf(outf_,
328 "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ",
330 now_s->tm_hour,
331 now_s->tm_min,
332 now_s->tm_sec,
333 (long)t->tv_usec,
334 component);
335 fprintf(outf_, "%s", *i);
336 fprintf(outf_, "%s\n", c_normal);
337 }
338 mutex->unlock();
339 }
340}
341
342void
343ConsoleLogger::tlog_info(struct timeval *t, const char *component, Exception &e)
344{
345 if (log_level <= LL_INFO) {
346 mutex->lock();
347 localtime_r(&t->tv_sec, now_s);
348 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
349 fprintf(outf_,
350 "%02d:%02d:%02d.%06ld %s: [EXCEPTION] ",
351 now_s->tm_hour,
352 now_s->tm_min,
353 now_s->tm_sec,
354 (long)t->tv_usec,
355 component);
356 fprintf(outf_, "%s", *i);
357 fprintf(outf_, "%s\n", c_normal);
358 }
359 mutex->unlock();
360 }
361}
362
363void
364ConsoleLogger::tlog_warn(struct timeval *t, const char *component, Exception &e)
365{
366 if (log_level <= LL_WARN) {
367 mutex->lock();
368 localtime_r(&t->tv_sec, now_s);
369 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
370 fprintf(outf_,
371 "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ",
372 c_brown,
373 now_s->tm_hour,
374 now_s->tm_min,
375 now_s->tm_sec,
376 (long)t->tv_usec,
377 component);
378 fprintf(outf_, "%s", *i);
379 fprintf(outf_, "%s\n", c_normal);
380 }
381 mutex->unlock();
382 }
383}
384
385void
386ConsoleLogger::tlog_error(struct timeval *t, const char *component, Exception &e)
387{
388 if (log_level <= LL_ERROR) {
389 mutex->lock();
390 localtime_r(&t->tv_sec, now_s);
391 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
392 fprintf(outf_,
393 "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ",
394 c_red,
395 now_s->tm_hour,
396 now_s->tm_min,
397 now_s->tm_sec,
398 (long)t->tv_usec,
399 component);
400 fprintf(outf_, "%s", *i);
401 fprintf(outf_, "%s\n", c_normal);
402 }
403 mutex->unlock();
404 }
405}
406
407void
408ConsoleLogger::vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va)
409{
410 if (log_level <= LL_DEBUG) {
411 mutex->lock();
412 localtime_r(&t->tv_sec, now_s);
413 fprintf(outf_,
414 "%s%02d:%02d:%02d.%06ld %s: ",
416 now_s->tm_hour,
417 now_s->tm_min,
418 now_s->tm_sec,
419 (long)t->tv_usec,
420 component);
421 vfprintf(outf_, format, va);
422 fprintf(outf_, "%s\n", c_normal);
423 mutex->unlock();
424 }
425}
426
427void
428ConsoleLogger::vtlog_info(struct timeval *t, const char *component, const char *format, va_list va)
429{
430 if (log_level <= LL_INFO) {
431 mutex->lock();
432 localtime_r(&t->tv_sec, now_s);
433 fprintf(outf_,
434 "%02d:%02d:%02d.%06ld %s: ",
435 now_s->tm_hour,
436 now_s->tm_min,
437 now_s->tm_sec,
438 (long)t->tv_usec,
439 component);
440 vfprintf(outf_, format, va);
441 fprintf(outf_, "\n");
442 mutex->unlock();
443 }
444}
445
446void
447ConsoleLogger::vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va)
448{
449 if (log_level <= LL_WARN) {
450 mutex->lock();
451 localtime_r(&t->tv_sec, now_s);
452 fprintf(outf_,
453 "%s%02d:%02d:%02d.%06ld %s: ",
454 c_brown,
455 now_s->tm_hour,
456 now_s->tm_min,
457 now_s->tm_sec,
458 (long)t->tv_usec,
459 component);
460 vfprintf(outf_, format, va);
461 fprintf(outf_, "%s\n", c_normal);
462 mutex->unlock();
463 }
464}
465
466void
467ConsoleLogger::vtlog_error(struct timeval *t, const char *component, const char *format, va_list va)
468{
469 if (log_level <= LL_ERROR) {
470 mutex->lock();
471 localtime_r(&t->tv_sec, now_s);
472 fprintf(outf_,
473 "%s%02d:%02d:%02d.%06ld %s: ",
474 c_red,
475 now_s->tm_hour,
476 now_s->tm_min,
477 now_s->tm_sec,
478 (long)t->tv_usec,
479 component);
480 vfprintf(outf_, format, va);
481 fprintf(outf_, "%s\n", c_normal);
482 mutex->unlock();
483 }
484}
485
486} // end namespace fawkes
virtual void tlog_error(struct timeval *t, const char *component, const char *format,...)
Log error message for specific time.
Definition: console.cpp:312
virtual void vlog_warn(const char *component, const char *format, va_list va)
Log warning message.
Definition: console.cpp:110
virtual void log_error(const char *component, const char *format,...)
Log error message.
Definition: console.cpp:181
virtual void log_warn(const char *component, const char *format,...)
Log warning message.
Definition: console.cpp:172
virtual void vlog_info(const char *component, const char *format, va_list va)
Log informational message.
Definition: console.cpp:89
virtual void vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va)
Log debug message for specific time.
Definition: console.cpp:408
virtual void tlog_info(struct timeval *t, const char *component, const char *format,...)
Log informational message for specific time.
Definition: console.cpp:294
virtual void tlog_debug(struct timeval *t, const char *component, const char *format,...)
Log debug message for specific time.
Definition: console.cpp:285
virtual void log_info(const char *component, const char *format,...)
Log informational message.
Definition: console.cpp:163
virtual void vlog_error(const char *component, const char *format, va_list va)
Log error message.
Definition: console.cpp:132
virtual void vlog_debug(const char *component, const char *format, va_list va)
Log debug message.
Definition: console.cpp:67
virtual void vtlog_info(struct timeval *t, const char *component, const char *format, va_list va)
Log informational message for specific time.
Definition: console.cpp:428
virtual void vtlog_error(struct timeval *t, const char *component, const char *format, va_list va)
Log error message for specific time.
Definition: console.cpp:467
virtual void vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va)
Log warning message for specific time.
Definition: console.cpp:447
virtual void log_debug(const char *component, const char *format,...)
Log debug message.
Definition: console.cpp:154
virtual ~ConsoleLogger()
Destructor.
Definition: console.cpp:59
virtual void tlog_warn(struct timeval *t, const char *component, const char *format,...)
Log warning message for specific time.
Definition: console.cpp:303
ConsoleLogger(LogLevel log_level=LL_DEBUG)
Constructor.
Definition: console.cpp:49
Message iterator for exceptions.
Definition: exception.h:73
Base class for exceptions in Fawkes.
Definition: exception.h:36
iterator end() noexcept
Get end iterator for messages.
Definition: exception.cpp:692
iterator begin() noexcept
Get iterator for messages.
Definition: exception.cpp:676
Interface for logging.
Definition: logger.h:42
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
LogLevel log_level
Minimum log level.
Definition: logger.h:126
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.
static const char * c_normal
Print normal on console, without colors, depends on console settings.
static const char * c_lightgray
Print light gray on console.
static const char * c_brown
Print brown on console.
static const char * c_red
Print red on console.