 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tojo ...Thats Me Guest
|
Posted: Thu May 18, 2006 11:21 pm Post subject: Help needed for Trace mechanism for function calls |
|
|
Hello,
I have a requiremnt to put a trace in all my functions when
execution enter in the function and also when execution exist from it.
I saw some samples to do the same
for eg. Hereis the current method I am using.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
//#define TRACE_ENABLED //Commenting and uncommenting this line can
disable/enable logging
#ifdef TRACE_ENABLED
#define FUNC_TRACE(funcName) FuncHeader obj_##funcName(#funcName)
#else
#define FUNC_TRACE(funcName) void(0)
#endif
class FuncHeader
{
char* myTraceStr;
public:
FuncHeader(char* TraceString )
{
myTraceStr = TraceString;
std::cout<<std::endl<<"Entering :"<<myTraceStr<<std::endl;
}
~FuncHeader( )
{
std::cout<<std::endl<<"Exiting :"<<myTraceStr<<std::endl;
}
};
int main(int argc, char* argv[])
{
FUNC_TRACE(main);
printf("Hello World!\n");
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Could you tell me a better mechanism to implement tracing when
entering and exiting a function.
My Target is to achive the same but without putting some macro with
function name in every function call.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
kanze Guest
|
Posted: Sat May 20, 2006 4:21 am Post subject: Re: Help needed for Trace mechanism for function calls |
|
|
Tojo ...Thats Me wrote:
| Quote: | I have a requiremnt to put a trace in all my functions when
execution enter in the function and also when execution exist
from it.
I saw some samples to do the same
for eg. Hereis the current method I am using.
|
Just one small comment, and a meta-comment...
[...]
| Quote: | #define FUNC_TRACE(funcName) FuncHeader obj_##funcName
(#funcName)
|
Since this line should only be used within a function, and only
once in any given function, you don't really need ## to make it
unique. More important is to use a name so strange no
reasonable programmer is likely to use it. Something along the
lines of "dummy_for_trace_in_functions", for example. (Of
course, whatever you use, the coding guidelines should specify
that a user cannot use it. If, for example, the coding
guidelines insist on camelCase, just about any name with a _ in
it is OK.)
If you really do want distinct names, I would concatenate avec
__LINE__. Something like:
#define paste2( a, b ) a ## b
#define paste( a, b ) paste2( a, b )
#define FUNC_TRACE( funcName ) \
FuncHeader paste( dummy, __LINE__ )( \
#funcName, __FILE__, __LINE__ )
More generally, I would design the tracing mechanism so that it
can be turned on and off dynamically, with a configuration file
or a command line option. And I'd try to make it as cheap as
possible when turned off, so that I could leave it in production
code. (But I'd also try to make it possible to remove entirely,
as you do here. Mainly for political reasons -- I've never
actually had to remove it in delivered code, but the fact that
it could be removed if necessary has been a necessary argument
to get it accepted in the first place.)
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Sat May 20, 2006 10:21 pm Post subject: Re: Help needed for Trace mechanism for function calls |
|
|
Tojo ...Thats Me wrote:
| Quote: | Hello,
I have a requiremnt to put a trace in all my functions when
execution enter in the function and also when execution exist from it.
I saw some samples to do the same
for eg. Hereis the current method I am using.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream
//#define TRACE_ENABLED //Commenting and uncommenting this line can
disable/enable logging
#ifdef TRACE_ENABLED
#define FUNC_TRACE(funcName) FuncHeader obj_##funcName(#funcName)
#else
#define FUNC_TRACE(funcName) void(0)
#endif
class FuncHeader
{
char* myTraceStr;
public:
FuncHeader(char* TraceString )
{
myTraceStr = TraceString;
std::cout<<std::endl<<"Entering :"<<myTraceStr<<std::endl;
}
~FuncHeader( )
{
std::cout<<std::endl<<"Exiting :"<<myTraceStr<<std::endl;
}
};
int main(int argc, char* argv[])
{
FUNC_TRACE(main);
printf("Hello World!\n");
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Could you tell me a better mechanism to implement tracing when
entering and exiting a function.
My Target is to achive the same but without putting some macro with
function name in every function call.
|
A better mechanism would to direct the C++ compiler to insert such
function calls (one at the entry and another at the exit of each
compiled routine). I know that "-finstrument-functions" is the switch
to pass to the gcc compiler to have that compiler generate a function
"prologue" and an "epilogue" for each compiled routine. And I would
expect that most - if not all - other C++ compilers also have this
capability. To find out for certain, I would consult the compiler's
documentation under execution profiling or code instrumentation.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Axter Guest
|
Posted: Mon May 22, 2006 2:21 pm Post subject: Re: Help needed for Trace mechanism for function calls |
|
|
Tojo ...Thats Me wrote:
| Quote: | Hello,
I have a requiremnt to put a trace in all my functions when
execution enter in the function and also when execution exist from it.
I saw some samples to do the same
for eg. Hereis the current method I am using.
//////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////
#include <iostream
//#define TRACE_ENABLED //Commenting and uncommenting this line can
disable/enable logging
#ifdef TRACE_ENABLED
#define FUNC_TRACE(funcName) FuncHeader obj_##funcName
(#funcName)
#else
#define FUNC_TRACE(funcName) void(0)
#endif
class FuncHeader
{
char* myTraceStr;
public:
FuncHeader(char* TraceString )
{
myTraceStr = TraceString;
std::cout<<std::endl<<"Entering :"<<myTraceStr<<std::endl;
}
~FuncHeader( )
{
std::cout<<std::endl<<"Exiting :"<<myTraceStr<<std::endl;
}
};
int main(int argc, char* argv[])
{
FUNC_TRACE(main);
printf("Hello World!\n");
return 0;
}
//////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////
Could you tell me a better mechanism to implement tracing when
entering and exiting a function.
My Target is to achive the same but without putting some macro with
function name in every function call.
|
Some compilers, like GNU 3.x/4.x and VC++ 7.x/8.x will support the C
standard function macro within C++ code.
Check out example usage in the following link:
http://axter.com/ezlogger/
In paticular, look at the __FUNCTION__ macro usage in related link:
http://axter.com/ezlogger/ezlogger__macros_8hpp.htm
The __FUNCTION__ macro is supported by both GNU and VC++ compilers, so
if you use this macro, you don't need to specify the function name.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Arndt Muehlenfeld Guest
|
Posted: Tue May 23, 2006 10:21 pm Post subject: Re: Help needed for Trace mechanism for function calls |
|
|
Tojo ...Thats Me wrote:
| Quote: | Hello,
I have a requiremnt to put a trace in all my functions when
execution enter in the function and also when execution exist from it.
I saw some samples to do the same
for eg. Hereis the current method I am using.
|
[code using macros snipped]
| Quote: | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Could you tell me a better mechanism to implement tracing when
entering and exiting a function.
My Target is to achive the same but without putting some macro with
function name in every function call.
|
You could take a look at Aspect-C++ (wwww.aspectc.org).
With aspects you could insert tracing calls without direct modifications
to your source code. If you want to remove the tracing code again,
just compile without the aspect.
The only drawback I encountered is that the aspect weaver choked on
some C++ code like BOOST, but it was improved since I used it.
You should give it a try.
Cheers,
Arndt
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|