Logger Class Reference

#include <Logger.h>

List of all members.

Public Member Functions

void setDebugMode (bool flag=true)
void log (const std::string &msg, Uint16 mode)
std::string getCurrentTime ()

Static Public Member Functions

LoggergetInstance ()
void freeInstance ()

Protected Member Functions

 Logger ()
virtual ~Logger ()

Private Attributes

std::ofstream LogFile
std::ofstream ErrLogFile
std::ofstream AuthLogFile
std::ofstream DBLogFile
SDL_mutex * mxStdLogger
SDL_mutex * mxErrLogger
SDL_mutex * mxAuthLogger
SDL_mutex * mxDBLogger
SDL_mutex * mxLoggerProp
bool debugMode

Static Private Attributes

LoggerpUniqueInstance = NULL
SDL_mutex * mxInstance = SDL_CreateMutex()


Detailed Description

A simple logging system, implements Singleton pattern. Threadsafe.

Todo:
TODO Porting existing code in order to use logging system.

Definition at line 29 of file Logger.h.


Constructor & Destructor Documentation

Logger::Logger  )  [protected]
 

Constructor.

Definition at line 27 of file Logger.cpp.

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 }

Logger::~Logger  )  [protected, virtual]
 

Virtual destructor.

Definition at line 41 of file Logger.cpp.

References mxAuthLogger, mxDBLogger, mxErrLogger, mxLoggerProp, and mxStdLogger.

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 }


Member Function Documentation

void Logger::freeInstance  )  [static]
 

Frees the Logger instance and its memory.

Author:
stonedz
Since:
pre-alpha

Definition at line 18 of file Logger.cpp.

References mxInstance, and pUniqueInstance.

00018                          {
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 }

std::string Logger::getCurrentTime  ) 
 

Returns a pre-formatted string with localtime.

Author:
sotnedz
Since:
pre-alpha
Returns:
Current time.

Definition at line 51 of file Logger.cpp.

Referenced by log().

00051                                 {
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 }

Logger * Logger::getInstance  )  [static]
 

Gets the logger instance. Since this class implements the Singleton pattern, this method is the one to be called to get an usable instance.

Author:
stonedz
Since:
pre-alpha
Returns:
The Logger instance.

Definition at line 9 of file Logger.cpp.

References mxInstance, and pUniqueInstance.

Referenced by Cmd_accounts::addAccount(), Cmd_accounts::attachChar(), Char::Char(), and CommandScheduler::queue().

00009                            {
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 }

void Logger::log const std::string &  msg,
Uint16  mode
 

Logs a message, adding a timestamp and possibly more informations.

Author:
stonedz
Since:
pre-alpha
Parameters:
msg Constant reference to the message to log.
mode Logging mode, For available modes please check Logger.h define.

Definition at line 78 of file Logger.cpp.

References AuthLogFile, DBLogFile, ErrLogFile, getCurrentTime(), and LogFile.

Referenced by Cmd_accounts::addAccount(), Cmd_accounts::attachChar(), ClientFSM::authenticate(), Char::Char(), Gamefsm::ExecTrans(), ClientFSM::move(), CommandScheduler::queue(), ClientFSM::Ready(), and Gamefsm::Start().

00078                                                  {
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 }

void Logger::setDebugMode bool  flag = true  ) 
 

Sets or unsets the debug mode. Default behaviour of the Logger object is to start with debugMode off.

Author:
stonedz
Since:
pre-alpha
Parameters:
flag Sets or unsets debug mode.

Definition at line 71 of file Logger.cpp.

References debugMode.

00071                                   {
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 }


Member Data Documentation

std::ofstream Logger::AuthLogFile [private]
 

Authentication log file.

Definition at line 104 of file Logger.h.

Referenced by log().

std::ofstream Logger::DBLogFile [private]
 

Database log file.

Definition at line 105 of file Logger.h.

Referenced by log().

bool Logger::debugMode [private]
 

When true, the output will be printed to the logfile AND to the standard output stream (the error log will be printed to the standard error stream).

Deafult value is false.

Definition at line 120 of file Logger.h.

Referenced by setDebugMode().

std::ofstream Logger::ErrLogFile [private]
 

Error log file.

Definition at line 103 of file Logger.h.

Referenced by log().

std::ofstream Logger::LogFile [private]
 

Standard log file.

Definition at line 102 of file Logger.h.

Referenced by log().

SDL_mutex* Logger::mxAuthLogger [private]
 

Mutex for authentication logfile.

Definition at line 109 of file Logger.h.

Referenced by ~Logger().

SDL_mutex* Logger::mxDBLogger [private]
 

Mutex for the Database logfile.

Definition at line 110 of file Logger.h.

Referenced by ~Logger().

SDL_mutex* Logger::mxErrLogger [private]
 

Mutex for error logfile.

Definition at line 108 of file Logger.h.

Referenced by ~Logger().

SDL_mutex * Logger::mxInstance = SDL_CreateMutex() [static, private]
 

Instance mutex, for thread safetyness.

Definition at line 7 of file Logger.cpp.

Referenced by freeInstance(), and getInstance().

SDL_mutex* Logger::mxLoggerProp [private]
 

Mutex fo general Logger's properties.

Definition at line 112 of file Logger.h.

Referenced by ~Logger().

SDL_mutex* Logger::mxStdLogger [private]
 

Mutex for standard logfile.

Definition at line 107 of file Logger.h.

Referenced by ~Logger().

Logger * Logger::pUniqueInstance = NULL [static, private]
 

Logger instance.

Definition at line 6 of file Logger.cpp.

Referenced by freeInstance(), and getInstance().


The documentation for this class was generated from the following files:
Generated on Mon Dec 3 04:30:14 2007 for lo-testserver by  doxygen 1.3.9.1