Original Post
I have been experimenting a lot with errors but I have to admit that I have not checked C++ throw, try and catch so I might be totally out in deep water.
Goal: An error handling class that records errors in a logfile. The reporting should be such that I know in which class the error occured and specifically in which function && statement.
My simple solution: I have a class CError that is a Singleton, globally accessible. Each class has #inludes "CError.h" and has a private member:
CError *cErr; that is initiated at startup like so:
CError *cErr=CError::getInstance();
I only want to know errors here so the logging will only be done when a call does not result in what I want. That is for example
Now it also takes care of repeated messages and records how many times the error occured like so:
**********Above message was repeated 218 times***************
The handler can also be turned off and will then only return a value. I know it is an extra function call but hey, something went wrong so who cares.
Now give me your devastating comments on why this is not good, cause I have the feeling it is not. I´ll use this during development cause it is easy to remove if needed.
I am actually a Swede, living in paradise.
EDIT: sigh, why do I always mess this up...
Edited by - clabinsky on December 30, 2001 1:01:46 AM
Edited by - clabinsky on December 30, 2001 1:03:28 AM
Edited by - clabinsky on December 30, 2001 1:05:41 AM
HRESULT hr;
hr=call some directx function;
if(FAILED(hr))
//I usually have two choices return NULL or bool
//For bool
return cErr->Log("<classname>:<functionname>:<description>",<hr optional1>,<bool: return value>);
//For NULL
{
cErr->Log("<classname>:<functionname>:<description>",<hr optional1>);
return NULL;
}
The Error class overloads the Log function many times and can be called with different results.
1. It returns the value that is specified in <bool: return value>
2. If no parameter is specified it returns nothing
3. If hr is included, it looks up the hr message in a table and adds it to the logfile.
Here is a real function using the Errorhandler:
//Logs Error and HRESULT and returns nothing
hr=m_lpDirectDraw->CreateSurface(ddDesc,&lpPrimary,NULL);
if(FAILED(hr))
{
cErr->Log("CSurfBase:CreateSurface: Call to CreateSurface failed",hr);
return NULL;
}
//Logs Error and hr and returns false
hr=m_lpDirectDraw->RestoreAllSurfaces();
if(FAILED(hr))
return cErr->Log("CSurfBase:RestoreSurfaces: call to RestoreAllSurf..failed",hr,false);
|