If you don't mind inserting any code manually, you can create a class that:
- writes a record to a method in the constructor
- provides a method to reset arbitrary parameters
- provides a way to record status
- Log output with recorded status in the destructor
Usage will look something like this:
unsigned long long factorial(unsigned long long n) { Inspector inspect("factorial", __FILE__, __LINE__); inspect.parameter("n", n); if (n < 2) { return inspect.result(1); } return inspect.result(n * fact(n-1)); }
Of course, you can write macros to declare the inspector and check the parameters. If you work with a compiler that supports macros of variable variables, then you can get a result similar to:
unsigned long long factorial(unsigned long long n) { INJECT_INSPECTOR(n); if (n < 2) { return INSPECT_RETURN(1); } return INSPECT_RETURN(n * fact(n-1)); }
I'm not sure that you can get a cleaner solution without resorting to the AOP environment or some special code generation tool.
D.Shawley
source share