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 
33 namespace 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  */
49 ConsoleLogger::ConsoleLogger(LogLevel log_level) : Logger(log_level)
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 
66 void
67 ConsoleLogger::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 
88 void
89 ConsoleLogger::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 
109 void
110 ConsoleLogger::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 
131 void
132 ConsoleLogger::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 
153 void
154 ConsoleLogger::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 
162 void
163 ConsoleLogger::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 
171 void
172 ConsoleLogger::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 
180 void
181 ConsoleLogger::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 
189 void
190 ConsoleLogger::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] ",
200  c_lightgray,
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 
213 void
214 ConsoleLogger::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 
236 void
237 ConsoleLogger::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 
260 void
261 ConsoleLogger::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 
284 void
285 ConsoleLogger::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 
293 void
294 ConsoleLogger::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 
302 void
303 ConsoleLogger::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 
311 void
312 ConsoleLogger::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 
320 void
321 ConsoleLogger::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] ",
329  c_lightgray,
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 
342 void
343 ConsoleLogger::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 
363 void
364 ConsoleLogger::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 
385 void
386 ConsoleLogger::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 
407 void
408 ConsoleLogger::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: ",
415  c_lightgray,
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 
427 void
428 ConsoleLogger::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 
446 void
447 ConsoleLogger::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 
466 void
467 ConsoleLogger::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
fawkes::Mutex::lock
void lock()
Lock this mutex.
Definition: mutex.cpp:91
fawkes::ConsoleLogger::log_warn
virtual void log_warn(const char *component, const char *format,...)
Definition: console.cpp:176
fawkes::ConsoleLogger::ConsoleLogger
ConsoleLogger(LogLevel log_level=LL_DEBUG)
Constructor.
Definition: console.cpp:53
fawkes::Exception::end
iterator end()
Get end iterator for messages.
Definition: exception.cpp:691
fawkes::Logger::log_level
LogLevel log_level
Minimum log level.
Definition: logger.h:125
fawkes::ConsoleLogger::tlog_info
virtual void tlog_info(struct timeval *t, const char *component, const char *format,...)
Definition: console.cpp:298
fawkes::ConsoleLogger::log_error
virtual void log_error(const char *component, const char *format,...)
Definition: console.cpp:185
fawkes::ConsoleLogger::log_debug
virtual void log_debug(const char *component, const char *format,...)
Definition: console.cpp:158
fawkes::ConsoleLogger::log_info
virtual void log_info(const char *component, const char *format,...)
Definition: console.cpp:167
fawkes::Mutex::unlock
void unlock()
Unlock the mutex.
Definition: mutex.cpp:135
fawkes::ConsoleLogger::tlog_warn
virtual void tlog_warn(struct timeval *t, const char *component, const char *format,...)
Definition: console.cpp:307
fawkes::Logger::LL_DEBUG
debug output, relevant only when tracking down problems
Definition: logger.h:51
fawkes::c_lightgray
static const char * c_lightgray
Print light gray on console.
Definition: console_colors.h:108
fawkes::ConsoleLogger::vtlog_info
virtual void vtlog_info(struct timeval *t, const char *component, const char *format, va_list va)
Definition: console.cpp:432
fawkes::c_red
static const char * c_red
Print red on console.
Definition: console_colors.h:48
fawkes::Logger::LL_INFO
informational output about normal procedures
Definition: logger.h:52
fawkes
fawkes::c_normal
static const char * c_normal
Print normal on console, without colors, depends on console settings.
Definition: console_colors.h:118
fawkes::ConsoleLogger::tlog_debug
virtual void tlog_debug(struct timeval *t, const char *component, const char *format,...)
Definition: console.cpp:289
fawkes::Logger::LL_ERROR
error, may be recoverable (software still running) or not (software has to terminate).
Definition: logger.h:56
fawkes::ConsoleLogger::vtlog_error
virtual void vtlog_error(struct timeval *t, const char *component, const char *format, va_list va)
Definition: console.cpp:471
fawkes::ConsoleLogger::vtlog_debug
virtual void vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va)
Definition: console.cpp:412
fawkes::ConsoleLogger::vlog_error
virtual void vlog_error(const char *component, const char *format, va_list va)
Definition: console.cpp:136
fawkes::Exception::begin
iterator begin()
Get iterator for messages.
Definition: exception.cpp:675
fawkes::c_brown
static const char * c_brown
Print brown on console.
Definition: console_colors.h:68
fawkes::ConsoleLogger::vtlog_warn
virtual void vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va)
Definition: console.cpp:451
fawkes::ConsoleLogger::vlog_debug
virtual void vlog_debug(const char *component, const char *format, va_list va)
Definition: console.cpp:71
fawkes::ConsoleLogger::vlog_warn
virtual void vlog_warn(const char *component, const char *format, va_list va)
Definition: console.cpp:114
fawkes::Logger::LL_WARN
warning, should be investigated but software still functions, an example is that something was reques...
Definition: logger.h:53
fawkes::ConsoleLogger::~ConsoleLogger
virtual ~ConsoleLogger()
Destructor.
Definition: console.cpp:63
fawkes::Exception::iterator
Definition: exception.h:76
fawkes::ConsoleLogger::tlog_error
virtual void tlog_error(struct timeval *t, const char *component, const char *format,...)
Definition: console.cpp:316
fawkes::ConsoleLogger::vlog_info
virtual void vlog_info(const char *component, const char *format, va_list va)
Definition: console.cpp:93
fawkes::Exception
Definition: exception.h:39