Sunday, March 27, 2011

How can I use TRACE macro in non mfc projects

I want to use the TRACE() macro to get output in the debug window in VS2005 in a non mfc c++ project.

But I don't know which additional header or lib is needed.

I want to know are there any ways of putting messages in the debug output window and how can i do that, thx a lot

From stackoverflow
  • You can try the DebugOutputString function. TRACE is only enabled in debug builds.

  • If you use ATL you can try ATLTRACE.

    TRACE is defined in afx.h as (at least in vs 2008):

    // extern ATL::CTrace TRACE;
    #define TRACE ATLTRACE
    

    And ATLTRACE can be found in atltrace.h

  • Build your own.

    trace.cpp:

    #ifdef _DEBUG
    bool _trace(TCHAR *format, ...)
    {
       TCHAR buffer[1000];
    
       va_list argptr;
       va_start(argptr, format);
       wvsprintf(buffer, format, argptr);
       va_end(argptr);
    
       OutputDebugString(buffer);
    
       return true;
    }
    #endif
    

    trace.h:

    #include <windows.h>
    #ifdef _DEBUG
    bool _trace(TCHAR *format, ...);
    #define TRACE _trace
    #else
    #define TRACE false && _trace
    #endif
    

    then just #include "trace.h" and you're all set.

    Disclaimer: I just copy/pasted this code from a personal project and took out some project specific stuff, but there's no reason it shouldn't work. ;-)

  • In my understanding wvsprintf has problem with formatting. Use _vsnprintf (or thcar version _vsntprintf ) instead

0 comments:

Post a Comment