tags:

views:

56

answers:

3

I had to define a new set of wrappers for existing methods. These new wrappers intend to help in debugging by including certain tracing information.

Original Source Code:

Sample.c ::

Caller{

void functionA(){
    funcB();
}

}

Callee{

void funcB(){
}
}

Modified Code with Traditional Wrapper Functionality:

Sample.h ::

#define funcB wrapperFuncB //not visible to Callee

Caller{

void functionA(){ //this gets redirected to wrapperFuncB cos of #define
    funcB();
}
}
Callee{

void wrapperFuncB(){
    if(DEBUG){ //value of DEBUG is set at runtime 
        //COLLECT Trace data


    }else{
        funcB();
    }
}
void funcB(){
}
}

This mechanism has the additional overhead of : 1] All calls to funcB is routed to wrapperFuncB, irrespecitve of DEBUG enabled or not 2] An additional method frame [wrapperFuncB] has to be created, irrespecitve of DEBUG enabled or not 3] An overhead of a conditional check

+1  A: 

If the methods are identical, you could use something like this:

#ifdef __DEBUGING
#define myClass debug_class
#else
#define myClass actual_class
#endif

In this way, you can selectively choose which class you are using in your code, the actual one or the wrapper one. There may be a few problems though, because this was just the first idea that came to mind.

Alexander Rafferty
+1  A: 

Alternatively you could put your debug statements like this

#ifdef DEBUG_ENABLE
DEBUG_LOG(X)  printf(x)
#else
DEBUG_LOG(X) do{}while(0)

void FunctionA()
{
DEBUG_LOG("\n Debugging function",__func__);
}

You can use the same routine without wrappers. Advantage would be the logs would make the code little bit easier to understand and you can enable and disable them using the compiler options.

Praveen S
You should use a do/while block for the #ifdef case as well as the #else. You might have responded while the C tag was still on the question, so fair enough using printf() though with one string arg it's difficult to use. In C++, can use `std::cerr << __FILE__ << ':' << __LINE<< ' ' << X << '\n';` or similar, then `DEBUG_LOG("x " << x);` etc.
Tony
But the issue here is "DEBUG_ENABLE" is set at runtime , during the actual program execution. At compile time, we do not know whether the program will be executed in debug mode or not .
In that case you will know if debug_enable is set or not, the macro can have a conditional if(debug_enable) statement and perform the action. This avoids using wrapper functions.
Praveen S
A: 
binW
But the issue here is "__DEBUG__" is set at runtime , during the actual program execution. At compile time, we do not know whether the program will be executed in debug mode or not .
then i guess you can use function pointers. I am going to add to my answer
binW