Main Page | Compound List | File List | Compound Members | File Members

Logger.C

Go to the documentation of this file.
00001 
00037 #include <sys/stat.h>
00038 
00039 #include "Logger.h"
00040 
00045 const char *Logger::logFileName = 0;
00047 FILE *Logger::pFile = 0;
00048 
00049 bool Logger::debug = false;
00050 bool Logger::mErrorFound = false;
00051 
00052 
00056 void Logger::setDebug()
00057 {
00058   debug = true;
00059 }
00060 
00065 bool Logger::errorFound()
00066 {
00067   return mErrorFound;
00068 }
00069 
00070 
00074 const char *Logger::getLocalTime(time_t aclock)
00075 {
00076   static char buf[128];
00077   struct tm *newtime;
00078   newtime = localtime( &aclock );  /* Convert time to struct */
00079 
00080   sprintf(buf, "%02d:%02d:%02d %02d/%02d/%04d",
00081           newtime->tm_hour, 
00082           newtime->tm_min, 
00083           newtime->tm_sec, 
00084           newtime->tm_mon, 
00085           newtime->tm_mday, 
00086           newtime->tm_year + 1900 );
00087   return buf;
00088 } // getLocalTime
00089 
00096 const char *Logger::getTimeStamp()
00097 {
00098   time_t aclock;
00099   time( &aclock );                 /* Get time in seconds */
00100   return getLocalTime( aclock );
00101 } // getTimeStamp
00102 
00103 
00104 
00117 bool Logger::openLogFile()
00118 {
00119   bool rslt = true;
00120   if (pFile == 0) {
00121     rslt = false;
00122     if (logFileName != 0) {
00123       const char *mode = "a"; // open for append
00124       if ((pFile = fopen(logFileName, mode)) != 0) {
00125         rslt = true;
00126       }
00127     }
00128   }
00129   return rslt;
00130 } // openLogFile
00131 
00132 
00142 void Logger::log(const LogLevel level,
00143                  const char *methodName,
00144                  const char *message )
00145 {
00146   if ((level == DEBUG && debug) || (level == ERROR)) {
00147     if (openLogFile()) {
00148       char *pLevelName;
00149       if (level == DEBUG) {
00150         pLevelName = "DEBUG";
00151       }
00152       else if (level == ERROR) {
00153         pLevelName = "ERROR";
00154         mErrorFound = true;
00155       }
00156       fprintf(pFile, "%s %s %s::%s: %s\n",
00157               getTimeStamp(),
00158               pLevelName, 
00159               className, 
00160               methodName, 
00161               message );
00162       fflush(pFile);
00163     }
00164   }
00165 } // log

Generated on Sat Mar 27 13:07:37 2004 for Mail Filter by doxygen 1.3.3