2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2004-04-14 23:52:59 +02:00
|
|
|
#include "CLogger.h"
|
2004-08-15 22:57:04 +02:00
|
|
|
#include "ConfigDB.h"
|
2004-04-14 23:52:59 +02:00
|
|
|
#include "lib.h"
|
|
|
|
|
2004-08-15 22:57:04 +02:00
|
|
|
#include "CConsole.h"
|
|
|
|
extern CConsole* g_Console;
|
2004-07-11 18:03:26 +02:00
|
|
|
|
2004-04-14 23:52:59 +02:00
|
|
|
using namespace std;
|
|
|
|
|
2004-08-15 22:57:04 +02:00
|
|
|
const char* html_header0 =
|
|
|
|
"<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>";
|
2004-04-14 23:52:59 +02:00
|
|
|
|
2004-08-15 22:57:04 +02:00
|
|
|
const char* html_header1 = "</H1></P>\n";
|
2004-04-14 23:52:59 +02:00
|
|
|
|
2004-08-15 22:57:04 +02:00
|
|
|
const char* html_footer = "</BODY>\n</HTML>\n";
|
2004-04-14 23:52:59 +02:00
|
|
|
|
2004-08-18 03:32:05 +02:00
|
|
|
#define MEMORY_BUFFER_SIZE 240
|
2004-04-14 23:52:59 +02:00
|
|
|
|
|
|
|
CLogger * CLogger::GetInstance()
|
|
|
|
{
|
|
|
|
static CLogger m_Instance;
|
|
|
|
|
|
|
|
return &m_Instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
CLogger::CLogger()
|
|
|
|
{
|
|
|
|
|
|
|
|
m_NumberOfMessages = 0;
|
|
|
|
m_NumberOfErrors = 0;
|
|
|
|
m_NumberOfWarnings = 0;
|
|
|
|
|
2004-08-18 03:51:23 +02:00
|
|
|
m_MemoryLogBuffer = (char *)calloc(MEMORY_BUFFER_SIZE, 1);
|
2004-08-15 22:57:04 +02:00
|
|
|
m_CurrentPosition = m_MemoryLogBuffer;
|
2004-04-14 23:52:59 +02:00
|
|
|
|
2004-05-29 18:36:28 +02:00
|
|
|
// current directory is $install_dir/data, we want $install_dir/logs.
|
|
|
|
// TODO: make sure we are called after file_rel_chdir,
|
|
|
|
// or else cur dir may be anywhere
|
2004-08-15 22:57:04 +02:00
|
|
|
m_MainLog.open ("../logs/mainlog.html", ofstream::out | ofstream::trunc);
|
|
|
|
m_InterestingLog.open ("../logs/interestinglog.html", ofstream::out | ofstream::trunc);
|
|
|
|
m_MemoryLog.open ("../logs/memorylog.html", ofstream::out | ofstream::trunc);
|
2004-04-14 23:52:59 +02:00
|
|
|
|
|
|
|
//Write Headers for the HTML documents
|
2004-08-15 22:57:04 +02:00
|
|
|
m_MainLog << html_header0 << "Main log" << html_header1;
|
2004-04-14 23:52:59 +02:00
|
|
|
|
2004-08-15 22:57:04 +02:00
|
|
|
//Write Headers for the HTML documents
|
|
|
|
m_InterestingLog << html_header0 << "Main log (interesting items only, as specified in system.cfg)" << html_header1;
|
|
|
|
|
2004-04-14 23:52:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
2004-08-15 22:57:04 +02:00
|
|
|
m_MainLog << html_footer;
|
|
|
|
m_MainLog.close ();
|
2004-04-14 23:52:59 +02:00
|
|
|
|
2004-08-15 22:57:04 +02:00
|
|
|
m_InterestingLog << "<P>Engine exited successfully on " << __DATE__;
|
|
|
|
m_InterestingLog << " at " << __TIME__ << buffer << "</P>\n";
|
|
|
|
m_InterestingLog << html_footer;
|
|
|
|
m_InterestingLog.close ();
|
|
|
|
|
|
|
|
|
2004-04-14 23:52:59 +02:00
|
|
|
//Add end marker to logs in memory
|
|
|
|
m_CurrentPosition = '\0';
|
|
|
|
|
2004-08-15 22:57:04 +02:00
|
|
|
m_MemoryLog << html_header0 << "Memory log" << html_header1;
|
|
|
|
m_MemoryLog << "<P>Memory Log started with capacity of " << \
|
2004-04-14 23:52:59 +02:00
|
|
|
MEMORY_BUFFER_SIZE << " characters.</P>\n";
|
2004-08-15 22:57:04 +02:00
|
|
|
m_MemoryLog << m_MemoryLogBuffer;
|
|
|
|
m_MemoryLog << html_footer;
|
2004-04-14 23:52:59 +02:00
|
|
|
|
2004-08-15 22:57:04 +02:00
|
|
|
m_MemoryLog.close ();
|
2004-04-14 23:52:59 +02:00
|
|
|
|
2004-08-15 22:57:04 +02:00
|
|
|
free(m_MemoryLogBuffer);
|
2004-04-14 23:52:59 +02:00
|
|
|
}
|
|
|
|
|
2004-08-15 22:57:04 +02:00
|
|
|
void CLogger::WriteMessage(const char *message, int interestedness)
|
2004-04-14 23:52:59 +02:00
|
|
|
{
|
|
|
|
m_NumberOfMessages++;
|
2004-08-15 22:57:04 +02:00
|
|
|
|
|
|
|
if (interestedness >= 2)
|
|
|
|
{
|
|
|
|
if (g_Console) g_Console->InsertMessage(L"LOG: %hs", message);
|
|
|
|
m_InterestingLog << "<P>" << message << "</P>\n";
|
|
|
|
m_InterestingLog.flush();
|
|
|
|
}
|
2004-07-19 21:05:34 +02:00
|
|
|
m_MainLog << "<P>" << message << "</P>\n";
|
2004-04-14 23:52:59 +02:00
|
|
|
m_MainLog.flush();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-08-15 22:57:04 +02:00
|
|
|
void CLogger::WriteError(const char *message, int interestedness)
|
2004-04-14 23:52:59 +02:00
|
|
|
{
|
|
|
|
m_NumberOfErrors++;
|
2004-08-15 22:57:04 +02:00
|
|
|
debug_out("ERROR: %s\n", message);
|
|
|
|
if (interestedness >= 1)
|
|
|
|
{
|
|
|
|
if (g_Console) g_Console->InsertMessage(L"ERROR: %hs", message);
|
|
|
|
m_InterestingLog << "<P class=\"error\">ERROR: "<< message << "</P>\n";
|
|
|
|
m_InterestingLog.flush();
|
|
|
|
}
|
2004-04-14 23:52:59 +02:00
|
|
|
m_MainLog << "<P class=\"error\">ERROR: "<< message << "</P>\n";
|
|
|
|
m_MainLog.flush();
|
|
|
|
}
|
|
|
|
|
2004-08-15 22:57:04 +02:00
|
|
|
void CLogger::WriteWarning(const char *message, int interestedness)
|
2004-04-14 23:52:59 +02:00
|
|
|
{
|
|
|
|
m_NumberOfWarnings++;
|
2004-08-15 22:57:04 +02:00
|
|
|
if (interestedness >= 1)
|
|
|
|
{
|
|
|
|
if (g_Console) g_Console->InsertMessage(L"WARNING: %hs", message);
|
|
|
|
m_InterestingLog << "<P class=\"warning\">WARNING: "<< message << "</P>\n";
|
|
|
|
m_InterestingLog.flush();
|
|
|
|
}
|
2004-04-14 23:52:59 +02:00
|
|
|
m_MainLog << "<P class=\"warning\">WARNING: "<< message << "</P>\n";
|
|
|
|
m_MainLog.flush();
|
|
|
|
}
|
|
|
|
|
2004-08-15 22:57:04 +02:00
|
|
|
void CLogger::Log(ELogMethod method, const char* category, const char *fmt, ...)
|
2004-04-14 23:52:59 +02:00
|
|
|
{
|
|
|
|
va_list argp;
|
|
|
|
char buffer[512];
|
|
|
|
|
|
|
|
memset(buffer,0,sizeof(buffer));
|
|
|
|
|
|
|
|
va_start(argp, fmt);
|
2004-08-09 17:44:35 +02:00
|
|
|
vsnprintf2(buffer, sizeof(buffer), fmt, argp);
|
2004-04-14 23:52:59 +02:00
|
|
|
va_end(argp);
|
|
|
|
|
|
|
|
if(method == NORMAL)
|
2004-08-15 22:57:04 +02:00
|
|
|
WriteMessage(buffer, Interestedness(category));
|
2004-04-14 23:52:59 +02:00
|
|
|
else if(method == ERROR)
|
2004-08-15 22:57:04 +02:00
|
|
|
WriteError(buffer, Interestedness(category));
|
2004-04-14 23:52:59 +02:00
|
|
|
else if(method == WARNING)
|
2004-08-15 22:57:04 +02:00
|
|
|
WriteWarning(buffer, Interestedness(category));
|
2004-04-14 23:52:59 +02:00
|
|
|
else
|
2004-08-15 22:57:04 +02:00
|
|
|
WriteMessage(buffer, Interestedness(category));
|
2004-04-14 23:52:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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>");
|
|
|
|
|
2004-08-15 22:57:04 +02:00
|
|
|
if((m_CurrentPosition - m_MemoryLogBuffer + strlen(buffer) + 1) >= MEMORY_BUFFER_SIZE)
|
2004-04-14 23:52:59 +02:00
|
|
|
{
|
|
|
|
//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);
|
|
|
|
}
|
|
|
|
|
2004-08-15 22:57:04 +02:00
|
|
|
int CLogger::Interestedness(const char* category)
|
|
|
|
{
|
|
|
|
// This could be cached, but reading from the config DB every time allows
|
|
|
|
// easy run-time alteration of interest levels (and shouldn't be particularly
|
|
|
|
// slow)
|
|
|
|
|
2004-08-15 23:10:49 +02:00
|
|
|
// Category unspecified: use a high interest level to encourage
|
|
|
|
// people to categorise their errors
|
|
|
|
if (category == NULL)
|
|
|
|
return 2;
|
|
|
|
|
2004-08-15 22:57:04 +02:00
|
|
|
// If the config DB hasn't been loaded, assume the default
|
2004-08-24 13:05:41 +02:00
|
|
|
if (! CConfigDB::IsInitialised())
|
2004-08-15 22:57:04 +02:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
CConfigValue* v = g_ConfigDB.GetValue(CFG_SYSTEM, CStr("loginterest.")+category);
|
2004-08-15 23:10:49 +02:00
|
|
|
// If the value is unspecified, also use the default
|
2004-08-15 22:57:04 +02:00
|
|
|
if (!v)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
int level;
|
2004-08-15 23:10:49 +02:00
|
|
|
// Something failed, so the default value might be a good alternative
|
2004-08-15 22:57:04 +02:00
|
|
|
if (! v->GetInt(level))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return level;
|
|
|
|
}
|