[ACCEPTED]-How can I use the TRACE macro in non-MFC projects?-visual-studio-2005

Accepted answer
Score: 30

Build your own.


#ifdef _DEBUG
bool _trace(TCHAR *format, ...)
   TCHAR buffer[1000];

   va_list argptr;
   va_start(argptr, format);
   wvsprintf(buffer, format, argptr);


   return true;


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

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

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

Score: 9

If you use ATL you can try ATLTRACE.

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

// extern ATL::CTrace TRACE;

And 1 ATLTRACE can be found in atltrace.h

Score: 4

You can try the DebugOutputString function. TRACE 1 is only enabled in debug builds.

Score: 2

Thanks to these answers I have fixed my 4 bug :-)

Here I share my TRACE macro in C++ based 3 on ideas from Ferruccio and enthusiasticgeek.

#  ifdef _MSC_VER
#    include <windows.h>
#    include <sstream>
#    define TRACE(x)                           \
     do {  std::stringstream s;  s << (x);     \
           OutputDebugString(s.str().c_str()); \
        } while(0)
#  else
#    include <iostream>
#    define TRACE(x)  std::clog << (x)
#  endif        // or std::cerr << (x) << std::flush
#  define TRACE(x)


#define ENABLE_TRACE  //can depend on _DEBUG or NDEBUG macros
#include "my_above_trace_header.h"

int main (void)
   int     v1 = 123;
   double  v2 = 456.789;
   TRACE ("main() v1="<< v1 <<" v2="<< v2 <<'\n');

Any 2 improvements/suggestions/contributions are 1 welcome ;-)

Score: 1

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

Score: 0

I've seen the following on the 'net: #define TRACE printf

Developing 1 this a little bit, came up with the following:

#ifdef _DEBUG
#define TRACE printf
#define TRACE 

More Related questions