c++debuggingwinapivisual-c++-6

Is there a TRACE statement for basic win32 C++?


In MFC C++ (Visual Studio 6) I am used to using the TRACE macro for debugging. Is there an equivalent statement for plain win32?


Solution

  • _RPTn works great, though not quite as convenient. Here is some code that recreates the MFC TRACE statement as a function allowing variable number of arguments. Also adds TraceEx macro which prepends source file and line number so you can click back to the location of the statement.

    Update: The original code on CodeGuru wouldn't compile for me in Release mode so I changed the way that TRACE statements are removed for Release mode. Here is my full source that I put into Trace.h. Thanks to Thomas Rizos for the original:

    // TRACE macro for win32
    #ifndef __TRACE_H__850CE873
    #define __TRACE_H__850CE873
    
    #include <crtdbg.h>
    #include <stdarg.h>
    #include <stdio.h>
    #include <string.h>
    
    #ifdef _DEBUG
    #define TRACEMAXSTRING  1024
    
    char szBuffer[TRACEMAXSTRING];
    inline void TRACE(const char* format,...)
    {
        va_list args;
        va_start(args,format);
        int nBuf;
        nBuf = _vsnprintf(szBuffer,
                       TRACEMAXSTRING,
                       format,
                       args);
        va_end(args);
    
        _RPT0(_CRT_WARN,szBuffer);
    }
    #define TRACEF _snprintf(szBuffer,TRACEMAXSTRING,"%s(%d): ", \
                    &strrchr(__FILE__,'\\')[1],__LINE__); \
                    _RPT0(_CRT_WARN,szBuffer); \
                    TRACE
    #else
    // Remove for release mode
    #define TRACE  ((void)0)
    #define TRACEF ((void)0)
    #endif
    
    #endif // __TRACE_H__850CE873