public class CreateErrorLog
{
#region Variables
private string m_sLogFormat;
private string m_sErrorTime;
#endregion
#region Properties
//get and set - public access of private members
public string LogFormat
{
get
{
return m_sLogFormat;
}
set
{
m_sLogFormat = value;
}
}
public string ErrorTime
{
get
{
return m_sErrorTime;
}
set
{
m_sErrorTime = value;
}
}
#endregion
#region Constructor
public CreateErrorLog()
{
// TODO: Add constructor logic here
m_sLogFormat = "[2T]"+ DateTime.Now.ToShortDateString().ToString()+" "+DateTime.Now.ToLongTimeString().ToString()+" ==> ";
string sYear = DateTime.Now.Year.ToString();
string sMonth = DateTime.Now.Month.ToString();
string sDay = DateTime.Now.Day.ToString();
m_sErrorTime = sYear+sMonth+sDay;
}
~CreateErrorLog(){}
#endregion
#region Functions
public void ErrorLog(string sPathName, string sErrMsg)
{
StreamWriter sw = File.AppendText(sPathName+m_sErrorTime+".txt");
sw.WriteLine(m_sLogFormat + sErrMsg);
sw.Flush();
sw.Close();
}
#endregion
}
Showing posts with label Error Report Example. Show all posts
Showing posts with label Error Report Example. Show all posts
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__
#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__
Subscribe to:
Posts (Atom)