mercoledì 2 luglio 2008

Logging enter/exit into/from methods or functions

Just as easy as (and same principle of) Synchronizing in the previous post:

class MyMethodLogger
{

std::string _methodName;

public
:
MyMethodLogger(const char *methodName):
_methodName(methodName)
{

log << "Entering method " << _methodName << std::endl;
}
~
MyMethodLogger()
{

log << "Exiting method " << _methodName << std::endl;
}
};


Very easy to use: just instance a MyMethodLogger as the first instruction in method body:

void method()
{


MyMethodLogger("method()");
...
}


At the end of the method execution (of after an exception throw) the destructor of MyMethodLogger will trace the exit from the method.

Nessun commento: