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 <logging/file.h>
00026 #include <utils/system/file.h>
00027 
00028 #include <core/threading/mutex.h>
00029 
00030 #include <cstdlib>
00031 #include <sys/time.h>
00032 #include <sys/stat.h>
00033 #include <time.h>
00034 #include <fcntl.h>
00035 #include <cerrno>
00036 
00037 namespace fawkes {
00038 
00039 /** @class FileLogger <logging/file.h>
00040  * Interface for logging to a specified file.
00041  * The FileLogger will pipe all output into the given file. The
00042  * output will be prepended by a single character which determines the 
00043  * type of output (E for error, W for warning, etc.).
00044  *
00045  */
00046 
00047 /** Constructor. 
00048  * @param filename the name of the log-file
00049  * @param log_level minimum log level
00050  */
00051 FileLogger::FileLogger(const char* filename, LogLevel log_level)
00052   : Logger(log_level)
00053 {
00054   int fd = open(filename, O_RDWR | O_CREAT | O_APPEND,
00055                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
00056   if (fd == -1) {
00057     throw Exception(errno, "Failed to open log file %s", filename);
00058   }
00059   log_file = fdopen(fd, "a");
00060 
00061   now_s = (struct tm *)malloc(sizeof(struct tm));
00062 
00063   mutex = new Mutex();
00064 }
00065 
00066 
00067 /** Destructor. */
00068 FileLogger::~FileLogger()
00069 {
00070   free(now_s);
00071   fclose(log_file);
00072   delete mutex;
00073 }
00074 
00075 
00076 void
00077 FileLogger::log_debug(const char *component, const char *format, ...)
00078 {
00079   va_list arg;
00080   va_start(arg, format);
00081   vlog_debug(component, format, arg);
00082   va_end(arg);
00083 }
00084 
00085 
00086 void
00087 FileLogger::log_info(const char *component, const char *format, ...)
00088 {
00089   va_list arg;
00090   va_start(arg, format);
00091   vlog_info(component, format, arg);
00092   va_end(arg);
00093 }
00094 
00095 
00096 void
00097 FileLogger::log_warn(const char *component, const char *format, ...)
00098 {
00099   va_list arg;
00100   va_start(arg, format);
00101   vlog_warn(component, format, arg);
00102   va_end(arg);
00103 }
00104 
00105 
00106 void
00107 FileLogger::log_error(const char *component, const char *format, ...)
00108 {
00109   va_list arg;
00110   va_start(arg, format);
00111   vlog_error(component, format, arg);
00112   va_end(arg);
00113 }
00114 
00115 
00116 void
00117 FileLogger::log_debug(const char *component, Exception &e)
00118 {
00119   if ( log_level <= LL_DEBUG ) {
00120     struct timeval now;
00121     gettimeofday(&now, NULL);
00122     mutex->lock();
00123     localtime_r(&now.tv_sec, now_s);
00124     for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00125       fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "D", now_s->tm_hour,
00126               now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
00127       fprintf(log_file, "%s", *i);
00128       fprintf(log_file, "\n");
00129     }
00130     fflush(log_file);
00131     mutex->unlock();
00132   }
00133 }
00134 
00135 
00136 void
00137 FileLogger::log_info(const char *component, Exception &e)
00138 {
00139   if ( log_level <= LL_INFO ) {
00140     struct timeval now;
00141     gettimeofday(&now, NULL);
00142     mutex->lock();
00143     localtime_r(&now.tv_sec, now_s);
00144     for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00145       fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "I", now_s->tm_hour,
00146               now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
00147       fprintf(log_file, "%s", *i);
00148       fprintf(log_file, "\n");
00149     }
00150     fflush(log_file);
00151     mutex->unlock();
00152   }
00153 }
00154 
00155 
00156 void
00157 FileLogger::log_warn(const char *component, Exception &e)
00158 {
00159   if ( log_level <= LL_WARN ) {
00160     struct timeval now;
00161     gettimeofday(&now, NULL);
00162     mutex->lock();
00163     localtime_r(&now.tv_sec, now_s);
00164     for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00165       fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "W", now_s->tm_hour,
00166               now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
00167       fprintf(log_file, "%s", *i);
00168       fprintf(log_file, "\n");
00169     }
00170     fflush(log_file);
00171     mutex->unlock();
00172   }
00173 }
00174 
00175 
00176 void
00177 FileLogger::log_error(const char *component, Exception &e)
00178 {
00179   if ( log_level <= LL_ERROR ) {
00180     struct timeval now;
00181     gettimeofday(&now, NULL);
00182     mutex->lock();
00183     localtime_r(&now.tv_sec, now_s);
00184     for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00185       fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "E", now_s->tm_hour,
00186               now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
00187       fprintf(log_file, "%s", *i);
00188       fprintf(log_file, "\n");
00189     }
00190     fflush(log_file);
00191     mutex->unlock();
00192   }
00193 }
00194 
00195 
00196 void
00197 FileLogger::vlog_debug(const char* component, const char* format, va_list va)
00198 {
00199   if (log_level <= LL_DEBUG ) {
00200     struct timeval now;
00201     gettimeofday(&now, NULL);
00202     mutex->lock();
00203     localtime_r(&now.tv_sec, now_s);
00204     fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s: ", "D", now_s->tm_hour,
00205             now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
00206     vfprintf(log_file, format, va);
00207     fprintf(log_file, "\n");
00208     fflush(log_file);
00209     mutex->unlock();
00210   }
00211 }
00212 
00213 
00214 void
00215 FileLogger::vlog_info(const char *component, const char *format, va_list va)
00216 {
00217   if (log_level <= LL_INFO ) {
00218     struct timeval now;
00219     gettimeofday(&now, NULL);
00220     mutex->lock();
00221     localtime_r(&now.tv_sec, now_s);
00222     fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s: ", "I", now_s->tm_hour,
00223             now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
00224     vfprintf(log_file, format, va);
00225     fprintf(log_file, "\n");
00226     fflush(log_file);
00227     mutex->unlock();
00228   }
00229 }
00230 
00231 
00232 void
00233 FileLogger::vlog_warn(const char *component, const char *format, va_list va)
00234 {
00235   if (log_level <= LL_WARN ) {
00236     struct timeval now;
00237     gettimeofday(&now, NULL);
00238     mutex->lock();
00239     localtime_r(&now.tv_sec, now_s);
00240     fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s: ", "W", now_s->tm_hour,
00241             now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
00242     vfprintf(log_file, format, va);
00243     fprintf(log_file, "\n");
00244     fflush(log_file);
00245     mutex->unlock();
00246   }
00247 }
00248 
00249 
00250 void
00251 FileLogger::vlog_error(const char *component, const char *format, va_list va)
00252 {
00253   if (log_level <= LL_ERROR ) {
00254     struct timeval now;
00255     gettimeofday(&now, NULL);
00256     mutex->lock();
00257     localtime_r(&now.tv_sec, now_s);
00258     fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s: ", "E", now_s->tm_hour,
00259             now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
00260     vfprintf(log_file, format, va);
00261     fprintf(log_file, "\n");
00262     fflush(log_file);
00263     mutex->unlock();
00264   }
00265 }
00266 
00267 
00268 void
00269 FileLogger::tlog_debug(struct timeval *t, const char *component, const char *format, ...)
00270 {
00271   va_list arg;
00272   va_start(arg, format);
00273   vtlog_debug(t, component, format, arg);
00274   va_end(arg);
00275 }
00276 
00277 
00278 void
00279 FileLogger::tlog_info(struct timeval *t, const char *component, const char *format, ...)
00280 {
00281   va_list arg;
00282   va_start(arg, format);
00283   vtlog_info(t, component, format, arg);
00284   va_end(arg);
00285 }
00286 
00287 
00288 void
00289 FileLogger::tlog_warn(struct timeval *t, const char *component, const char *format, ...)
00290 {
00291   va_list arg;
00292   va_start(arg, format);
00293   vtlog_warn(t, component, format, arg);
00294   va_end(arg);
00295 }
00296 
00297 
00298 void
00299 FileLogger::tlog_error(struct timeval *t, const char *component, const char *format, ...)
00300 {
00301   va_list arg;
00302   va_start(arg, format);
00303   vtlog_error(t, component, format, arg);
00304   va_end(arg);
00305 }
00306 
00307 
00308 void
00309 FileLogger::tlog_debug(struct timeval *t, const char *component, Exception &e)
00310 {
00311   if ( log_level <= LL_DEBUG ) {
00312     mutex->lock();
00313     localtime_r(&t->tv_sec, now_s);
00314     for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00315       fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "D", now_s->tm_hour,
00316               now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
00317       fprintf(log_file, "%s", *i);
00318       fprintf(log_file, "\n");
00319     }
00320     fflush(log_file);
00321     mutex->unlock();
00322   }
00323 }
00324 
00325 
00326 void
00327 FileLogger::tlog_info(struct timeval *t, const char *component, Exception &e)
00328 {
00329   if ( log_level <= LL_INFO ) {
00330     mutex->lock();
00331     localtime_r(&t->tv_sec, now_s);
00332     for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00333       fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "I", now_s->tm_hour,
00334               now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
00335       fprintf(log_file, "%s", *i);
00336       fprintf(log_file, "\n");
00337     }
00338     fflush(log_file);
00339     mutex->unlock();
00340   }
00341 }
00342 
00343 
00344 void
00345 FileLogger::tlog_warn(struct timeval *t, const char *component, Exception &e)
00346 {
00347   if ( log_level <= LL_WARN ) {
00348     mutex->lock();
00349     localtime_r(&t->tv_sec, now_s);
00350     for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00351       fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "W", now_s->tm_hour,
00352               now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
00353       fprintf(log_file, "%s", *i);
00354       fprintf(log_file, "\n");
00355     }
00356     fflush(log_file);
00357     mutex->unlock();
00358   }
00359 }
00360 
00361 
00362 void
00363 FileLogger::tlog_error(struct timeval *t, const char *component, Exception &e)
00364 {
00365   if ( log_level <= LL_ERROR ) {
00366     mutex->lock();
00367     localtime_r(&t->tv_sec, now_s);
00368     for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00369       fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "E", now_s->tm_hour,
00370               now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
00371       fprintf(log_file, "%s", *i);
00372       fprintf(log_file, "\n");
00373     }
00374     fflush(log_file);
00375     mutex->unlock();
00376   }
00377 }
00378 
00379 
00380 void
00381 FileLogger::vtlog_debug(struct timeval *t, const char* component, const char* format, va_list va)
00382 {
00383   if (log_level <= LL_DEBUG ) {
00384     mutex->lock();
00385     localtime_r(&t->tv_sec, now_s);
00386     fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s: ", "D", now_s->tm_hour,
00387             now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
00388     vfprintf(log_file, format, va);
00389     fprintf(log_file, "\n");
00390     fflush(log_file);
00391     mutex->unlock();
00392   }
00393 }
00394 
00395 
00396 void
00397 FileLogger::vtlog_info(struct timeval *t, const char *component, const char *format, va_list va)
00398 {
00399   if (log_level <= LL_INFO ) {
00400     mutex->lock();
00401     localtime_r(&t->tv_sec, now_s);
00402     fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s: ", "I", now_s->tm_hour,
00403             now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
00404     vfprintf(log_file, format, va);
00405     fprintf(log_file, "\n");
00406     fflush(log_file);
00407     mutex->unlock();
00408   }
00409 }
00410 
00411 
00412 void
00413 FileLogger::vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va)
00414 {
00415   if (log_level <= LL_WARN ) {
00416     mutex->lock();
00417     localtime_r(&t->tv_sec, now_s);
00418     fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s: ", "W", now_s->tm_hour,
00419             now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
00420     vfprintf(log_file, format, va);
00421     fprintf(log_file, "\n");
00422     fflush(log_file);
00423     mutex->unlock();
00424   }
00425 }
00426 
00427 
00428 void
00429 FileLogger::vtlog_error(struct timeval *t, const char *component, const char *format, va_list va)
00430 {
00431   if (log_level <= LL_ERROR ) {
00432     mutex->lock();
00433     localtime_r(&t->tv_sec, now_s);
00434     fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s: ", "E", now_s->tm_hour,
00435             now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
00436     vfprintf(log_file, format, va);
00437     fprintf(log_file, "\n");
00438     fflush(log_file);
00439     mutex->unlock();
00440   }
00441 }
00442 
00443 
00444 } // end namespace fawkes