dato datuashvili dato datuashvili - 5 months ago 68
C++ Question

TRACE system function call in c++

how call TRACE in c++?please explain
for example with this simple code

int x = 1;
int y = 16;
float z = 32.0;
TRACE( "This is a TRACE statement\n" );

TRACE( "The value of x is %d\n", x );

TRACE( "x = %d and y = %d\n", x, y );

TRACE( "x = %d and y = %x and z = %f\n", x, y, z );


If you mean "How can I trace the execution path of my code?" Then you need to use a source-level symbolic debugger.

In Linux this generally means using GDB for which there a number of GUI front ends; using GDB on the command line is arcane and laborious, it can be used through Eclipse or KDevelop for example, or the stand-alone Insight debugger. In Windows, the debugger will be specific to the compiler, but VC++ has about the best debugger available (and for free in the Express Edition).

In response to davit's edit

Define a TRACE macro thus:

#if defined NDEBUG
    #define TRACE( format, ... )
    #define TRACE( format, ... )   printf( "%s::%s(%d)" format, __FILE__, __FUNCTION__,  __LINE__, __VA_ARGS__ )

Note that the lack of a comma between "%s::%s(%d)" and format is deliberate. It prints a formatted string with source location prepended. I work in real-time embedded systems so often I also include a timestamp in the output as well.