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