Hi,
I would like to use a generic error msg handler so I can GetLastError
and SetError
easily for any class. I came up with this scheme. But I have a couple of questions. Please note this implementation is just for testing. But I want to get the basic design right.
#include <iostream>
#include <stdarg.h>
class ErrorHandler
{
public:
virtual void SetError(const char* zFormat, ...)
{
va_list args;
va_start (args, zFormat);
vsnprintf (z_ErrorBuf, sz_MaxBufSize, zFormat, args);
va_end (args);
}
const char* GetError() const
{
return z_ErrorBuf;
}
virtual ~ErrorHandler(){}
explicit ErrorHandler(const size_t szMaxBufSize = 1024)
{
z_ErrorBuf = malloc(sizeof(char)*szMaxBufSize);
sz_MaxBufSize = szMaxBufSize;
}
void ResizeBuffer(const size_t szMaxBufSize)
{
z_ErrorBuf = realloc(z_ErrorBuf, szMaxBufSize);
sz_MaxBufSize = szMaxBufSize;
}
protected:
char* z_ErrorBuf;
size_t sz_MaxBufSize;
};
class MyClass;
//Worker can be just an interface if needed. So, can't use friend.
class Worker
{
public:
void Work(MyClass& oGod);
};
void Worker::Work(MyClass& oGod)
{
//Work
//OnError
oGod.GetErrorHandler().SetError("Save me %s", "not");
}
class RecordingErrors
{
public:
const char* GetLastError() const
{
return oErrorHandler.GetError();
}
//GetErrorHandler is public
ErrorHandler& GetErrorHandler()
{
return oErrorHandler;
}
private:
ErrorHandler oErrorHandler;
};
class MyClass : public RecordingErrors
{
public:
bool GetThingsDone(Worker& me)
{
me.Work(*this);
//on Error
return false;
}
};
int main()
{
MyClass oL;
Worker w;
if(!oL.GetThingsDone(w))
{
std::cout << oL.GetLastError() << std::endl;
}
}
- Can I override this function in a child class?
virtual void SetError(const char* zFormat, ...)
. - Can I do away with the
RecordingErrors
class? I feel that thenMyClass
would have to inherit fromErrorHandler
which I don't think is good. Am I right or wrong? Is this another question about composition over inheritence. EDIT: I don't mean the name, but the idea? - Any possible scenarios this approach would fail?
- is there a better way to implement error logging?(logging is not the right word here. What is it)