CLogger Initial Commit

This was SVN commit r206.
This commit is contained in:
Simon Brenner 2004-04-14 21:52:59 +00:00
parent 28bd6e0510
commit d736ac7026
2 changed files with 214 additions and 0 deletions

156
source/ps/CLogger.cpp Executable file
View File

@ -0,0 +1,156 @@
#include "CLogger.h"
#include "lib.h"
using namespace std;
#define MAIN_HEADER "<HTML>\n<HEAD>\n<LINK REL=StyleSheet HREF=" \
"\"style.css\" TYPE=\"text/css\">\n" \
"</HEAD>\n<BODY>\n<P align=\"center\"><IMG src=" \
"\"0adlogo.jpg\"/></P>\n" \
"<P><H1>Main Log</H1></P>\n"
#define MEMORY_HEADER "<HTML>\n<HEAD>\n<LINK REL=StyleSheet HREF=" \
"\"style.css\" TYPE=\"text/css\">\n" \
"</HEAD>\n<BODY>\n<P align=\"center\"><IMG src=" \
"\"0adlogo.jpg\"/></P>\n" \
"<P><H1>Memory Log</H1></P>\n"
#define FOOTER "</BODY>\n</HTML>\n"
#define MEMORY_BUFFER_SIZE 35
CLogger * CLogger::GetInstance()
{
static CLogger m_Instance;
return &m_Instance;
}
CLogger::CLogger()
{
m_NumberOfMessages = 0;
m_NumberOfErrors = 0;
m_NumberOfWarnings = 0;
m_MemoryLog = (char *) malloc(MEMORY_BUFFER_SIZE);
m_CurrentPosition = m_MemoryLog;
memset(m_MemoryLog,0,MEMORY_BUFFER_SIZE);
m_MainLog.open ("logs/mainlog.html",ofstream::out | ofstream::trunc);
m_DetailedLog.open ("logs/detailedlog.html",ofstream::out | ofstream::trunc );
//Write Headers for the HTML documents
m_MainLog << MAIN_HEADER;
}
CLogger::~CLogger ()
{
char buffer[60];
sprintf(buffer," with %d message(s), %d error(s) and %d warning(s).", \
m_NumberOfMessages,m_NumberOfErrors,m_NumberOfWarnings);
//Write closing text
m_MainLog << "<P>Engine exited successfully on " << __DATE__;
m_MainLog << " at " << __TIME__ << buffer << "</P>\n";
m_MainLog << FOOTER;
//Add end marker to logs in memory
m_CurrentPosition = '\0';
m_DetailedLog << MEMORY_HEADER;
m_DetailedLog << "<P>Memory Log started with capacity of " << \
MEMORY_BUFFER_SIZE << " characters.</P>\n";
m_DetailedLog << m_MemoryLog;
m_DetailedLog << FOOTER;
m_DetailedLog.close ();
m_MainLog.close ();
free(m_MemoryLog);
}
void CLogger::WriteMessage(const char *message)
{
m_NumberOfMessages++;
m_MainLog << "<P>" << message << "</P>";
m_MainLog.flush();
}
void CLogger::WriteError(const char *message)
{
m_NumberOfErrors++;
m_MainLog << "<P class=\"error\">ERROR: "<< message << "</P>\n";
m_MainLog.flush();
}
void CLogger::WriteWarning(const char *message)
{
m_NumberOfWarnings++;
m_MainLog << "<P class=\"warning\">WARNING: "<< message << "</P>\n";
m_MainLog.flush();
}
void CLogger::Log(ELogMethod method, const char *fmt, ...)
{
va_list argp;
char buffer[512];
memset(buffer,0,sizeof(buffer));
va_start(argp, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, argp);
va_end(argp);
if(method == NORMAL)
WriteMessage(buffer);
else if(method == ERROR)
WriteError(buffer);
else if(method == WARNING)
WriteWarning(buffer);
else
WriteMessage(buffer);
}
void CLogger::QuickLog(const char *fmt, ...)
{
va_list argp;
char buffer[512];
int count = 0;
//Start a new paragraph in HTML
strcpy(buffer,"<P>");
va_start(argp, fmt);
vsnprintf(strchr(buffer, 0), sizeof(buffer), fmt, argp);
va_end(argp);
//add some html formatting
strcat(buffer,"</P>");
if((m_CurrentPosition - m_MemoryLog + strlen(buffer) + 1) >= MEMORY_BUFFER_SIZE)
{
//not enough room in the buffer so don't log.
return;
}
while(buffer[count] != '\0')
{
*m_CurrentPosition = buffer[count];
*m_CurrentPosition++;
count++;
}
*m_CurrentPosition = '\n';
*m_CurrentPosition++;
free(buffer);
}

58
source/ps/CLogger.h Executable file
View File

@ -0,0 +1,58 @@
#ifndef _ps_CLogger_H
#define _ps_CLogger_H
#include <fstream>
#include <stdio.h>
#include <stdarg.h>
#include <string>
#include <vector>
#define LOG (CLogger::GetInstance()->Log)
enum ELogMethod
{
NORMAL,
MESSAGE=NORMAL,
ERROR,
WARNING
};
class CLogger
{
public:
//Function used to get instance of CLogger
static CLogger *GetInstance();
virtual ~CLogger();
//Functions to write different message types
void WriteMessage(const char *message);
void WriteError(const char *message);
void WriteWarning(const char *message);
//Function to log stuff to file
void Log(ELogMethod method, const char* fmt, ...);
//Function to log stuff to memory buffer
void QuickLog(const char *fmt, ...);
private:
CLogger();
//the two filestreams
std::ofstream m_MainLog;
std::ofstream m_DetailedLog;
//vars to hold message counts
int m_NumberOfMessages;
int m_NumberOfErrors;
int m_NumberOfWarnings;
//this holds the start of the memory buffer.
char *m_MemoryLog;
//this holds the next available place to write to.
char *m_CurrentPosition;
};
#endif