Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 <cstdio>
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 }
56 
57 
58 /** Destructor. */
60 {
61  free(now_s);
62  delete mutex;
63 }
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(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_lightgray, now_s->tm_hour,
75  now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
76  vfprintf(stderr, format, va);
77  fprintf(stderr, "%s\n", c_normal);
78  mutex->unlock();
79  }
80 }
81 
82 
83 void
84 ConsoleLogger::vlog_info(const char *component, const char *format, va_list va)
85 {
86  if (log_level <= LL_INFO ) {
87  struct timeval now;
88  gettimeofday(&now, NULL);
89  mutex->lock();
90  localtime_r(&now.tv_sec, now_s);
91  fprintf(stderr, "%02d:%02d:%02d.%06ld %s: ", now_s->tm_hour, now_s->tm_min,
92  now_s->tm_sec, (long)now.tv_usec, component);
93  vfprintf(stderr, format, va);
94  fprintf(stderr, "\n");
95  mutex->unlock();
96  }
97 }
98 
99 
100 void
101 ConsoleLogger::vlog_warn(const char *component, const char *format, va_list va)
102 {
103  if ( log_level <= LL_WARN ) {
104  struct timeval now;
105  gettimeofday(&now, NULL);
106  mutex->lock();
107  localtime_r(&now.tv_sec, now_s);
108  fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_brown, now_s->tm_hour,
109  now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
110  vfprintf(stderr, format, va);
111  fprintf(stderr, "%s\n", c_normal);
112  mutex->unlock();
113  }
114 }
115 
116 
117 void
118 ConsoleLogger::vlog_error(const char *component, const char *format, va_list va)
119 {
120  if ( log_level <= LL_ERROR ) {
121  struct timeval now;
122  gettimeofday(&now, NULL);
123  mutex->lock();
124  localtime_r(&now.tv_sec, now_s);
125  fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_red, now_s->tm_hour,
126  now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
127  vfprintf(stderr, format, va);
128  fprintf(stderr, "%s\n", c_normal);
129  mutex->unlock();
130  }
131 }
132 
133 
134 void
135 ConsoleLogger::log_debug(const char *component, const char *format, ...)
136 {
137  va_list arg;
138  va_start(arg, format);
139  vlog_debug(component, format, arg);
140  va_end(arg);
141 }
142 
143 
144 void
145 ConsoleLogger::log_info(const char *component, const char *format, ...)
146 {
147  va_list arg;
148  va_start(arg, format);
149  vlog_info(component, format, arg);
150  va_end(arg);
151 }
152 
153 
154 void
155 ConsoleLogger::log_warn(const char *component, const char *format, ...)
156 {
157  va_list arg;
158  va_start(arg, format);
159  vlog_warn(component, format, arg);
160  va_end(arg);
161 }
162 
163 
164 void
165 ConsoleLogger::log_error(const char *component, const char *format, ...)
166 {
167  va_list arg;
168  va_start(arg, format);
169  vlog_error(component, format, arg);
170  va_end(arg);
171 }
172 
173 
174 void
175 ConsoleLogger::log_debug(const char *component, Exception &e)
176 {
177  if (log_level <= LL_DEBUG ) {
178  struct timeval now;
179  gettimeofday(&now, NULL);
180  mutex->lock();
181  localtime_r(&now.tv_sec, now_s);
182  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
183  fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_lightgray, now_s->tm_hour,
184  now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
185  fprintf(stderr, "%s", *i);
186  fprintf(stderr, "%s\n", c_normal);
187  }
188  mutex->unlock();
189  }
190 }
191 
192 
193 void
194 ConsoleLogger::log_info(const char *component, Exception &e)
195 {
196  if (log_level <= LL_INFO ) {
197  struct timeval now;
198  gettimeofday(&now, NULL);
199  mutex->lock();
200  localtime_r(&now.tv_sec, now_s);
201  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
202  fprintf(stderr, "%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", now_s->tm_hour,
203  now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
204  fprintf(stderr, "%s", *i);
205  fprintf(stderr, "%s\n", c_normal);
206  }
207  mutex->unlock();
208  }
209 }
210 
211 
212 void
213 ConsoleLogger::log_warn(const char *component, Exception &e)
214 {
215  if (log_level <= LL_WARN ) {
216  struct timeval now;
217  gettimeofday(&now, NULL);
218  mutex->lock();
219  localtime_r(&now.tv_sec, now_s);
220  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
221  fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_brown, now_s->tm_hour,
222  now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
223  fprintf(stderr, "%s", *i);
224  fprintf(stderr, "%s\n", c_normal);
225  }
226  mutex->unlock();
227  }
228 }
229 
230 
231 void
232 ConsoleLogger::log_error(const char *component, Exception &e)
233 {
234  if (log_level <= LL_DEBUG ) {
235  struct timeval now;
236  gettimeofday(&now, NULL);
237  mutex->lock();
238  localtime_r(&now.tv_sec, now_s);
239  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
240  fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_red, now_s->tm_hour,
241  now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
242  fprintf(stderr, "%s", *i);
243  fprintf(stderr, "%s\n", c_normal);
244  }
245  mutex->unlock();
246  }
247 }
248 
249 
250 void
251 ConsoleLogger::tlog_debug(struct timeval *t, const char *component, const char *format, ...)
252 {
253  va_list arg;
254  va_start(arg, format);
255  vtlog_debug(t, component, format, arg);
256  va_end(arg);
257 }
258 
259 
260 void
261 ConsoleLogger::tlog_info(struct timeval *t, const char *component, const char *format, ...)
262 {
263  va_list arg;
264  va_start(arg, format);
265  vtlog_info(t, component, format, arg);
266  va_end(arg);
267 }
268 
269 
270 void
271 ConsoleLogger::tlog_warn(struct timeval *t, const char *component, const char *format, ...)
272 {
273  va_list arg;
274  va_start(arg, format);
275  vtlog_warn(t, component, format, arg);
276  va_end(arg);
277 }
278 
279 
280 void
281 ConsoleLogger::tlog_error(struct timeval *t, const char *component, const char *format, ...)
282 {
283  va_list arg;
284  va_start(arg, format);
285  vtlog_error(t, component, format, arg);
286  va_end(arg);
287 }
288 
289 
290 void
291 ConsoleLogger::tlog_debug(struct timeval *t, const char *component, Exception &e)
292 {
293  if (log_level <= LL_DEBUG ) {
294  mutex->lock();
295  localtime_r(&t->tv_sec, now_s);
296  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
297  fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_lightgray, now_s->tm_hour,
298  now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
299  fprintf(stderr, "%s", *i);
300  fprintf(stderr, "%s\n", c_normal);
301  }
302  mutex->unlock();
303  }
304 }
305 
306 
307 void
308 ConsoleLogger::tlog_info(struct timeval *t, const char *component, Exception &e)
309 {
310  if (log_level <= LL_INFO ) {
311  mutex->lock();
312  localtime_r(&t->tv_sec, now_s);
313  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
314  fprintf(stderr, "%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", now_s->tm_hour,
315  now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
316  fprintf(stderr, "%s", *i);
317  fprintf(stderr, "%s\n", c_normal);
318  }
319  mutex->unlock();
320  }
321 }
322 
323 
324 void
325 ConsoleLogger::tlog_warn(struct timeval *t, const char *component, Exception &e)
326 {
327  if (log_level <= LL_WARN ) {
328  mutex->lock();
329  localtime_r(&t->tv_sec, now_s);
330  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
331  fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_brown, now_s->tm_hour,
332  now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
333  fprintf(stderr, "%s", *i);
334  fprintf(stderr, "%s\n", c_normal);
335  }
336  mutex->unlock();
337  }
338 }
339 
340 
341 void
342 ConsoleLogger::tlog_error(struct timeval *t, const char *component, Exception &e)
343 {
344  if (log_level <= LL_DEBUG ) {
345  mutex->lock();
346  localtime_r(&t->tv_sec, now_s);
347  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
348  fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_red, now_s->tm_hour,
349  now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
350  fprintf(stderr, "%s", *i);
351  fprintf(stderr, "%s\n", c_normal);
352  }
353  mutex->unlock();
354  }
355 }
356 
357 
358 
359 
360 void
361 ConsoleLogger::vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va)
362 {
363  if (log_level <= LL_DEBUG ) {
364  mutex->lock();
365  localtime_r(&t->tv_sec, now_s);
366  fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_lightgray, now_s->tm_hour,
367  now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
368  vfprintf(stderr, format, va);
369  fprintf(stderr, "%s\n", c_normal);
370  mutex->unlock();
371  }
372 }
373 
374 
375 void
376 ConsoleLogger::vtlog_info(struct timeval *t, const char *component, const char *format, va_list va)
377 {
378  if (log_level <= LL_INFO ) {
379  mutex->lock();
380  localtime_r(&t->tv_sec, now_s);
381  fprintf(stderr, "%02d:%02d:%02d.%06ld %s: ", now_s->tm_hour, now_s->tm_min,
382  now_s->tm_sec, (long)t->tv_usec, component);
383  vfprintf(stderr, format, va);
384  fprintf(stderr, "\n");
385  mutex->unlock();
386  }
387 }
388 
389 
390 void
391 ConsoleLogger::vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va)
392 {
393  if ( log_level <= LL_WARN ) {
394  mutex->lock();
395  localtime_r(&t->tv_sec, now_s);
396  fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_brown, now_s->tm_hour,
397  now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
398  vfprintf(stderr, format, va);
399  fprintf(stderr, "%s\n", c_normal);
400  mutex->unlock();
401  }
402 }
403 
404 
405 void
406 ConsoleLogger::vtlog_error(struct timeval *t, const char *component, const char *format, va_list va)
407 {
408  if ( log_level <= LL_ERROR ) {
409  mutex->lock();
410  localtime_r(&t->tv_sec, now_s);
411  fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_red, now_s->tm_hour,
412  now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
413  vfprintf(stderr, format, va);
414  fprintf(stderr, "%s\n", c_normal);
415  mutex->unlock();
416  }
417 }
418 
419 
420 } // end namespace fawkes