user170008
user170008

Reputation: 1066

Linking of Method calls

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

Upvotes: 0

Views: 84

Answers (3)

binW
binW

Reputation: 13712

As function wrapperFuncB will have the same signature as funcB, you can simply do this without the need of a new class.

#ifdef __DEBUG__
#define funcB wrapperFuncB
#else
#define funcB actualFuncB
#end

so when you are not debugging or collecting trace info, you can simply turn it off by not defining DEBUG and there will be no overhead

Edit: Updated after getting comments from user170008

I dont think #ifdef __DEBUG__ kind of thing can be used at run time. Most probably you will have to rely on command line params to differentiate between a debug or normal run. But what ever way you use just create a function pointer and set it according to what kind of run you are doing e.g

void (*funcB)(void);
if(debug)  
    funcB=debugFuncB;
else
    funcB=actualFuncB;

after this you can simply use funcB as you are using now i.e funcB();

Upvotes: 0

Praveen S
Praveen S

Reputation: 10393

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.

Upvotes: 1

Alexander Rafferty
Alexander Rafferty

Reputation: 6233

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.

Upvotes: 1

Related Questions