Fawkes API  Fawkes Development Version
console.cpp
00001 
00002 /***************************************************************************
00003  *  console.cpp - Fawkes console logger
00004  *
00005  *  Created: Tue Jan 16 21:08:25 2007
00006  *  Copyright  2006-2011  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <core/threading/mutex.h>
00025 #include <logging/console.h>
00026 
00027 #include <utils/system/console_colors.h>
00028 
00029 #include <cstdlib>
00030 #include <sys/time.h>
00031 #include <ctime>
00032 #include <cstdio>
00033 
00034 namespace fawkes {
00035 
00036 /** @class ConsoleLogger <logging/console.h>
00037  * Interface for logging to stderr.
00038  * The ConsoleLogger will pipe all output to stderr on the console. The
00039  * output will be color coded due to the importance of the output.
00040  *
00041  * Debug output will be drawn in grey font, informational output in console
00042  * default color, warnings will be printed in brown/orange and errors in red.
00043  *
00044  * @author Tim Niemueller
00045  */
00046 
00047 /** Constructor.
00048  * @param log_level minimum level to log
00049  */
00050 ConsoleLogger::ConsoleLogger(LogLevel log_level)
00051   : Logger(log_level)
00052 {
00053   now_s = (struct ::tm *)malloc(sizeof(struct ::tm));
00054   mutex = new Mutex();
00055 }
00056 
00057 
00058 /** Destructor. */
00059 ConsoleLogger::~ConsoleLogger()
00060 {
00061   free(now_s);
00062   delete mutex;
00063 }
00064 
00065 
00066 void
00067 ConsoleLogger::vlog_debug(const char *component, const char *format, va_list va)
00068 {
00069   if (log_level <= LL_DEBUG ) {
00070     struct timeval now;
00071     gettimeofday(&now, NULL);
00072     mutex->lock();
00073     localtime_r(&now.tv_sec, now_s);
00074     fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_lightgray, now_s->tm_hour,
00075             now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
00076     vfprintf(stderr, format, va);
00077     fprintf(stderr, "%s\n", c_normal);
00078     mutex->unlock();
00079   }
00080 }
00081 
00082 
00083 void
00084 ConsoleLogger::vlog_info(const char *component, const char *format, va_list va)
00085 {
00086   if (log_level <= LL_INFO ) {
00087     struct timeval now;
00088     gettimeofday(&now, NULL);
00089     mutex->lock();
00090     localtime_r(&now.tv_sec, now_s);
00091     fprintf(stderr, "%02d:%02d:%02d.%06ld %s: ", now_s->tm_hour, now_s->tm_min,
00092             now_s->tm_sec, (long)now.tv_usec, component);
00093     vfprintf(stderr, format, va);
00094     fprintf(stderr, "\n");
00095     mutex->unlock();
00096   }
00097 }
00098 
00099 
00100 void
00101 ConsoleLogger::vlog_warn(const char *component, const char *format, va_list va)
00102 {
00103   if ( log_level <= LL_WARN ) {
00104     struct timeval now;
00105     gettimeofday(&now, NULL);
00106     mutex->lock();
00107     localtime_r(&now.tv_sec, now_s);
00108     fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_brown, now_s->tm_hour,
00109             now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
00110     vfprintf(stderr, format, va);
00111     fprintf(stderr, "%s\n", c_normal);
00112     mutex->unlock();
00113   }
00114 }
00115 
00116 
00117 void
00118 ConsoleLogger::vlog_error(const char *component, const char *format, va_list va)
00119 {
00120   if ( log_level <= LL_ERROR ) {
00121     struct timeval now;
00122     gettimeofday(&now, NULL);
00123     mutex->lock();
00124     localtime_r(&now.tv_sec, now_s);
00125     fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_red, now_s->tm_hour,
00126             now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
00127     vfprintf(stderr, format, va);
00128     fprintf(stderr, "%s\n", c_normal);
00129     mutex->unlock();
00130   }
00131 }
00132 
00133 
00134 void
00135 ConsoleLogger::log_debug(const char *component, const char *format, ...)
00136 {
00137   va_list arg;
00138   va_start(arg, format);
00139   vlog_debug(component, format, arg);
00140   va_end(arg);
00141 }
00142 
00143 
00144 void
00145 ConsoleLogger::log_info(const char *component, const char *format, ...)
00146 {
00147   va_list arg;
00148   va_start(arg, format);
00149   vlog_info(component, format, arg);
00150   va_end(arg);
00151 }
00152 
00153 
00154 void
00155 ConsoleLogger::log_warn(const char *component, const char *format, ...)
00156 {
00157   va_list arg;
00158   va_start(arg, format);
00159   vlog_warn(component, format, arg);
00160   va_end(arg);
00161 }
00162 
00163 
00164 void
00165 ConsoleLogger::log_error(const char *component, const char *format, ...)
00166 {
00167   va_list arg;
00168   va_start(arg, format);
00169   vlog_error(component, format, arg);
00170   va_end(arg);
00171 }
00172 
00173 
00174 void
00175 ConsoleLogger::log_debug(const char *component, Exception &e)
00176 {
00177   if (log_level <= LL_DEBUG ) {
00178     struct timeval now;
00179     gettimeofday(&now, NULL);
00180     mutex->lock();
00181     localtime_r(&now.tv_sec, now_s);
00182     for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00183       fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_lightgray, now_s->tm_hour,
00184             now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
00185       fprintf(stderr, "%s", *i);
00186       fprintf(stderr, "%s\n", c_normal);
00187     }
00188     mutex->unlock();
00189   }
00190 }
00191 
00192 
00193 void
00194 ConsoleLogger::log_info(const char *component, Exception &e)
00195 {
00196   if (log_level <= LL_INFO ) {
00197     struct timeval now;
00198     gettimeofday(&now, NULL);
00199     mutex->lock();
00200     localtime_r(&now.tv_sec, now_s);
00201     for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00202       fprintf(stderr, "%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", now_s->tm_hour,
00203               now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
00204       fprintf(stderr, "%s", *i);
00205       fprintf(stderr, "%s\n", c_normal);
00206     }
00207     mutex->unlock();
00208   }
00209 }
00210 
00211 
00212 void
00213 ConsoleLogger::log_warn(const char *component, Exception &e)
00214 {
00215   if (log_level <= LL_WARN ) {
00216     struct timeval now;
00217     gettimeofday(&now, NULL);
00218     mutex->lock();
00219     localtime_r(&now.tv_sec, now_s);
00220     for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00221       fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_brown, now_s->tm_hour,
00222               now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
00223       fprintf(stderr, "%s", *i);
00224       fprintf(stderr, "%s\n", c_normal);
00225     }
00226     mutex->unlock();
00227   }
00228 }
00229 
00230 
00231 void
00232 ConsoleLogger::log_error(const char *component, Exception &e)
00233 {
00234   if (log_level <= LL_DEBUG ) {
00235     struct timeval now;
00236     gettimeofday(&now, NULL);
00237     mutex->lock();
00238     localtime_r(&now.tv_sec, now_s);
00239     for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00240       fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_red, now_s->tm_hour,
00241               now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
00242       fprintf(stderr, "%s", *i);
00243       fprintf(stderr, "%s\n", c_normal);
00244     }
00245     mutex->unlock();
00246   }
00247 }
00248 
00249 
00250 void
00251 ConsoleLogger::tlog_debug(struct timeval *t, const char *component, const char *format, ...)
00252 {
00253   va_list arg;
00254   va_start(arg, format);
00255   vtlog_debug(t, component, format, arg);
00256   va_end(arg);
00257 }
00258 
00259 
00260 void
00261 ConsoleLogger::tlog_info(struct timeval *t, const char *component, const char *format, ...)
00262 {
00263   va_list arg;
00264   va_start(arg, format);
00265   vtlog_info(t, component, format, arg);
00266   va_end(arg);
00267 }
00268 
00269 
00270 void
00271 ConsoleLogger::tlog_warn(struct timeval *t, const char *component, const char *format, ...)
00272 {
00273   va_list arg;
00274   va_start(arg, format);
00275   vtlog_warn(t, component, format, arg);
00276   va_end(arg);
00277 }
00278 
00279 
00280 void
00281 ConsoleLogger::tlog_error(struct timeval *t, const char *component, const char *format, ...)
00282 {
00283   va_list arg;
00284   va_start(arg, format);
00285   vtlog_error(t, component, format, arg);
00286   va_end(arg);
00287 }
00288 
00289 
00290 void
00291 ConsoleLogger::tlog_debug(struct timeval *t, const char *component, Exception &e)
00292 {
00293   if (log_level <= LL_DEBUG ) {
00294     mutex->lock();
00295     localtime_r(&t->tv_sec, now_s);
00296     for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00297       fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_lightgray, now_s->tm_hour,
00298             now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
00299       fprintf(stderr, "%s", *i);
00300       fprintf(stderr, "%s\n", c_normal);
00301     }
00302     mutex->unlock();
00303   }
00304 }
00305 
00306 
00307 void
00308 ConsoleLogger::tlog_info(struct timeval *t, const char *component, Exception &e)
00309 {
00310   if (log_level <= LL_INFO ) {
00311     mutex->lock();
00312     localtime_r(&t->tv_sec, now_s);
00313     for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00314       fprintf(stderr, "%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", now_s->tm_hour,
00315               now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
00316       fprintf(stderr, "%s", *i);
00317       fprintf(stderr, "%s\n", c_normal);
00318     }
00319     mutex->unlock();
00320   }
00321 }
00322 
00323 
00324 void
00325 ConsoleLogger::tlog_warn(struct timeval *t, const char *component, Exception &e)
00326 {
00327   if (log_level <= LL_WARN ) {
00328     mutex->lock();
00329     localtime_r(&t->tv_sec, now_s);
00330     for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00331       fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_brown, now_s->tm_hour,
00332               now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
00333       fprintf(stderr, "%s", *i);
00334       fprintf(stderr, "%s\n", c_normal);
00335     }
00336     mutex->unlock();
00337   }
00338 }
00339 
00340 
00341 void
00342 ConsoleLogger::tlog_error(struct timeval *t, const char *component, Exception &e)
00343 {
00344   if (log_level <= LL_DEBUG ) {
00345     mutex->lock();
00346     localtime_r(&t->tv_sec, now_s);
00347     for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00348       fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_red, now_s->tm_hour,
00349               now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
00350       fprintf(stderr, "%s", *i);
00351       fprintf(stderr, "%s\n", c_normal);
00352     }
00353     mutex->unlock();
00354   }
00355 }
00356 
00357 
00358 
00359 
00360 void
00361 ConsoleLogger::vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va)
00362 {
00363   if (log_level <= LL_DEBUG ) {
00364     mutex->lock();
00365     localtime_r(&t->tv_sec, now_s);
00366     fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_lightgray, now_s->tm_hour,
00367             now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
00368     vfprintf(stderr, format, va);
00369     fprintf(stderr, "%s\n", c_normal);
00370     mutex->unlock();
00371   }
00372 }
00373 
00374 
00375 void
00376 ConsoleLogger::vtlog_info(struct timeval *t, const char *component, const char *format, va_list va)
00377 {
00378   if (log_level <= LL_INFO ) {
00379     mutex->lock();
00380     localtime_r(&t->tv_sec, now_s);
00381     fprintf(stderr, "%02d:%02d:%02d.%06ld %s: ", now_s->tm_hour, now_s->tm_min,
00382             now_s->tm_sec, (long)t->tv_usec, component);
00383     vfprintf(stderr, format, va);
00384     fprintf(stderr, "\n");
00385     mutex->unlock();
00386   }
00387 }
00388 
00389 
00390 void
00391 ConsoleLogger::vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va)
00392 {
00393   if ( log_level <= LL_WARN ) {
00394     mutex->lock();
00395     localtime_r(&t->tv_sec, now_s);
00396     fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_brown, now_s->tm_hour,
00397             now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
00398     vfprintf(stderr, format, va);
00399     fprintf(stderr, "%s\n", c_normal);
00400     mutex->unlock();
00401   }
00402 }
00403 
00404 
00405 void
00406 ConsoleLogger::vtlog_error(struct timeval *t, const char *component, const char *format, va_list va)
00407 {
00408   if ( log_level <= LL_ERROR ) {
00409     mutex->lock();
00410     localtime_r(&t->tv_sec, now_s);
00411     fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_red, now_s->tm_hour,
00412             now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
00413     vfprintf(stderr, format, va);
00414     fprintf(stderr, "%s\n", c_normal);
00415     mutex->unlock();
00416   }
00417 }
00418 
00419 
00420 } // end namespace fawkes