Chitika

Thursday, October 20, 2011

C++: Logging Completeness

The CLog class was created by considering completeness in mind. When ever any message is logged, along with message, its actual location should be printed somewhat as the case with printing exception objects in very high level languages - Java/C#.

CLog class provide static methods to log messages to file descriptor contained in static variable of CLogFile (see previous blogs for CLogFile), keep in mind the requirement was single thread logging.
Preprocessor Macros were used to log messages for avoiding logging file name, function name and line number and to simplify things. Preprocessor constants __FILE__ gives current file, __FUNCTION__ gives current function and __LINE__ gives current line.

The Macro is as follows:

#define Log(Msg,LogLevel) CLog::LogMessage(Msg,LogLevel,__FILE__,__FUNCTION__,__LINE__);

The class is as follows:

class CLog
{

private:
static CLogFile LogFile;

public:
static void LogInit(char* fileName);
static void LogMessage(char *msg, int logLevel, char *fileName, char *funcName, int lineNum);
static void LogMessageToFD(FILE *fd, char *msg, int logLevel, char *fileName, char *funcName, int lineNum);

};

void CLog::LogInit(char* fileName) {
LogFile.SetFileDescriptorToFile(filename);
}

void CLog::LogMessage(char *msg, int logLevel, char *fileName, char *funcName, int lineNum) {
LogMessageToFD(LogFile.GetFD(), msg, logLevel, fileName, funcName, lineNum);
}

void CLog::LogMessageToFD(FILE *fd, char *msg, int logLevel, char *fileName,
char *funcName, int lineNum,) {

..........................
// Print Log Message with time stamp
..........................

}

No comments:

Post a Comment