Wednesday, May 23, 2007

Error Report Example in C++

#ifndef __ERRORREPORT_H__
#define __ERRORREPORT_H__

#define ERRORREPORTFILE "SystemCF\\ErrorLog.txt"

// Add a message to ErrorLog.txt file
// cMsg: the input message
// Return TRUE if it is success, FALSE otherwise
BOOL RecordErrMsg(LPCSTR cMsg)
{
// Open file
FILE* stream =fopen(ERRORREPORTFILE, "a" );
if(NULL==stream) return(FALSE);

// Save the time and data
CTime t = CTime::GetCurrentTime();
int iMonth=t.GetMonth();
int iDay=t.GetDay();
int iHour=t.GetHour();
int iMinute=t.GetMinute();
int iSecond=t.GetSecond();
fprintf( stream, "%d:%d:%d On %d/%d\n", iHour, iMinute, iSecond, iMonth, iDay);

// Save error message
fprintf( stream, "%s\n\n", cMsg);

// Close file
int iReturn=fclose(stream);
if(EOF==iReturn) return(FALSE);

return(TRUE);
}

// Add a message to ErrorLog.txt file
// cMsg: the input message
// Return TRUE if it is success, FALSE otherwise
BOOL RecordErrMsg(LPCTSTR cMsg)
{
// Open file
FILE* stream =fopen(ERRORREPORTFILE, "a" );
if(NULL==stream) return(FALSE);

// Save the time and data
CTime t = CTime::GetCurrentTime();
int iMonth=t.GetMonth();
int iDay=t.GetDay();
int iHour=t.GetHour();
int iMinute=t.GetMinute();
int iSecond=t.GetSecond();
fprintf( stream, "%d:%d:%d On %d/%d\n", iHour, iMinute, iSecond, iMonth, iDay);

// Save error message
_ftprintf( stream, _T("%s\n\n"), cMsg);

// Close file
int iReturn=fclose(stream);
if(EOF==iReturn) return(FALSE);

return(TRUE);
}

// Add a message to a file with the current time mask
// cMsg: the input message
// Return TRUE if it is success, FALSE otherwise
BOOL RecordMsg(LPCSTR filePath, LPCTSTR cMsg)
{
// Open file
FILE* stream =fopen(filePath, "a" );
if(NULL==stream) return(FALSE);

// Save the time and data
CTime t = CTime::GetCurrentTime();
int iMonth=t.GetMonth();
int iDay=t.GetDay();
int iHour=t.GetHour();
int iMinute=t.GetMinute();
int iSecond=t.GetSecond();
fprintf( stream, "%d:%d:%d On %d/%d\n", iHour, iMinute, iSecond, iMonth, iDay);

// Save error message
_ftprintf( stream, _T("%s\n\n"), cMsg);

// Close file
int iReturn=fclose(stream);
if(EOF==iReturn) return(FALSE);

return(TRUE);
}

// Add a message to a file with the input time mask
// cMsg: the input message
// CTime: the time record
// Return TRUE if it is success, FALSE otherwise
BOOL RecordMsg(LPCSTR filePath, LPCTSTR cMsg, CTime t)
{
// Open file
FILE* stream =fopen(filePath, "a" );
if(NULL==stream) return(FALSE);

// Save the time and data
int iMonth=t.GetMonth();
int iDay=t.GetDay();
int iHour=t.GetHour();
int iMinute=t.GetMinute();
int iSecond=t.GetSecond();
fprintf( stream, "%d:%d:%d On %d/%d\n", iHour, iMinute, iSecond, iMonth, iDay);

// Save error message
_ftprintf( stream, _T("%s\n\n"), cMsg);

// Close file
int iReturn=fclose(stream);
if(EOF==iReturn) return(FALSE);

return(TRUE);
}

// Add a message to a file without a time mask
// cMsg: the input message
// Return TRUE if it is success, FALSE otherwise
BOOL RecordMsg_NoTime(LPCSTR filePath, LPCTSTR cMsg)
{
// Open file
FILE* stream =fopen(filePath, "a" );
if(NULL==stream) return(FALSE);

// Save error message
_ftprintf( stream, _T("%s\n\n"), cMsg);

// Close file
int iReturn=fclose(stream);
if(EOF==iReturn) return(FALSE);

return(TRUE);
}

// Record the current memory
BOOL PrintMem(LPCTSTR cMsg, BOOL bEnd)
{
MEMORYSTATUS stat;
GlobalMemoryStatus (&stat);

// Open file
FILE* stream = fopen("\\SystemCF\\testMessage.txt", "a" );
if(NULL==stream) return(FALSE);

//print the message
_ftprintf(stream, _T("%s\n"), cMsg);

// fprintf(stream, "There are %12.5f total MB of physical memory\n", stat.dwTotalPhys/(1024.0 * 1024.0));

fprintf(stream, "There are %12.5f free MB of physical memory.\n", stat.dwAvailPhys/(1024.0 * 1024.0));

// fprintf(stream, "There are %12.5f total MB of virtual memory.\n", stat.dwTotalVirtual/(1024.0 * 1024.0));

if(TRUE==bEnd)
{
fprintf(stream, "There are %12.5f free MB of virtual memory.\n\n",
stat.dwAvailVirtual/(1024.0 * 1024.0));
}
else
{
fprintf(stream, "There are %12.5f free MB of virtual memory.\n",
stat.dwAvailVirtual/(1024.0 * 1024.0));
}

Sleep(100);
// Close file
int iReturn=fclose(stream);
if(EOF==iReturn) return(FALSE);

return(TRUE);
}

#endif //__ERRORREPORT_H__

No comments: