src/Logger.cpp

00001 #include "Logger.h"
00002 
00003 #include <time.h>
00004 
00005 
00006 Logger* Logger::pUniqueInstance = NULL; // Reference to the unique instance.
00007 SDL_mutex* Logger::mxInstance = SDL_CreateMutex();      // Reference to instance mutex.
00008 
00009 Logger* Logger::getInstance(){
00010         SDL_LockMutex(mxInstance);      // Locks the mutex...
00011         if (pUniqueInstance == NULL){ // No instance, create one.
00012         pUniqueInstance = new Logger;
00013     }
00014     SDL_UnlockMutex(mxInstance);        // Locks the mutex...
00015     return pUniqueInstance;
00016 }
00017 
00018 void Logger::freeInstance(){
00019         SDL_LockMutex(mxInstance);      // Locks the mutex...
00020         if (pUniqueInstance != NULL) {
00021         delete pUniqueInstance;
00022         pUniqueInstance = NULL;
00023     }
00024         SDL_LockMutex(mxInstance);      // Locks the mutex...
00025 }
00026 
00027 Logger::Logger()
00028         :LogFile(LOGFILE_NORMAL, std::ios::trunc),
00029         ErrLogFile(LOGFILE_ERROR, std::ios::app),
00030         AuthLogFile(LOGFILE_AUTH, std::ios::app),
00031         DBLogFile(LOGFILE_DB, std::ios::app),
00032         mxStdLogger(SDL_CreateMutex()),
00033         mxErrLogger(SDL_CreateMutex()),
00034         mxAuthLogger(SDL_CreateMutex()),
00035         mxDBLogger(SDL_CreateMutex()),
00036         mxLoggerProp(SDL_CreateMutex()),
00037         debugMode(false)
00038 {
00039 }
00040 
00041 Logger::~Logger()
00042 {
00043 // std::ofstream destructors take care of closing file descriptors
00044         SDL_DestroyMutex(mxStdLogger);
00045         SDL_DestroyMutex(mxErrLogger);
00046         SDL_DestroyMutex(mxAuthLogger);
00047         SDL_DestroyMutex(mxDBLogger);
00048         SDL_DestroyMutex(mxLoggerProp);
00049 }
00050 
00051 std::string Logger::getCurrentTime(){
00052         struct tm  timeinfo;
00053         time_t rawtime;
00054 
00055         time(&rawtime);
00056     timeinfo = *(localtime(&rawtime));
00057 
00058     std::ostringstream os;
00059         os  << std::setw(2) << std::setfill('0') << timeinfo.tm_mday
00060                 << "-" << std::setw(2) << std::setfill('0') << (timeinfo.tm_mon)+1
00061                 << " "
00062                 << "[" << std::setw(2) << std::setfill('0') << timeinfo.tm_hour
00063         << ":" << std::setw(2) << std::setfill('0') << timeinfo.tm_min
00064         << ":" << std::setw(2) << std::setfill('0') << timeinfo.tm_sec
00065         << "] ";
00066 
00067     return os.str();
00068 
00069 }
00070 
00071 void Logger::setDebugMode(bool flag){
00072         // Locks the mutex...
00073         SDL_LockMutex(this->mxLoggerProp);      // Locks the mutex...
00074         this->debugMode = flag;
00075         SDL_UnlockMutex(this->mxLoggerProp);// ...unlocks the mutex
00076 }
00077 
00078 void Logger::log(const std::string &msg, Uint16 mode){
00079         if(mode == LOGMODE_NORMAL && this->LogFile.is_open()){// Normal log.
00080                 SDL_LockMutex(this->mxStdLogger);
00081                 this->LogFile << getCurrentTime() << msg << std::endl;
00082 
00083                 if(this->debugMode){
00084                         std::cout << "<Log> " << msg << std::endl;
00085                 }
00086 
00087                 SDL_UnlockMutex(this->mxStdLogger);
00088         }
00089         else if(mode == LOGMODE_ERROR && this->ErrLogFile.is_open()) {// Log a server error.
00090                 SDL_LockMutex(this->mxErrLogger);
00091                 this->ErrLogFile << getCurrentTime() << msg << std::endl;
00092 
00093                 if(this->debugMode){
00094                         std::cerr << "<ERR Log> " << msg << std::endl;
00095                 }
00096 
00097                 SDL_UnlockMutex(this->mxErrLogger);
00098         }
00099         else if(mode == LOGMODE_AUTH && this->AuthLogFile.is_open()){// Log an authentication or an authentication attempt.
00100                 SDL_LockMutex(this->mxAuthLogger);
00101                 this->AuthLogFile << getCurrentTime() << msg << std::endl;
00102 
00103                 if(this->debugMode){
00104                         std::cout << "<Auth Log> " << msg << std::endl;
00105                 }
00106 
00107                 SDL_UnlockMutex(this->mxAuthLogger);
00108         }
00109         else if(mode == LOGMODE_DB && this->DBLogFile.is_open()){// Log a database-related information or problem.
00110         SDL_LockMutex(this->mxDBLogger);
00111                 this->DBLogFile << getCurrentTime() << msg << std::endl;
00112 
00113                 if(this->debugMode){
00114                         std::cout << "<DB Log> " << msg << std::endl;
00115                 }
00116 
00117                 SDL_UnlockMutex(this->mxDBLogger);
00118         }
00119 
00120 }

Generated on Mon Dec 3 04:30:13 2007 for lo-testserver by  doxygen 1.3.9.1