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