26

I want to use the TRACE() macro to get output in the debug window in Visual Studio 2005 in a non-MFC C++ project, but which additional header or library is needed?

Is there a way of putting messages in the debug output window and how can I do that?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jagttt
  • 1,020
  • 1
  • 12
  • 25

6 Answers6

30

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. ;-)

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
  • 1
    wvsprintf does not handle floating point numbers (%f). Instead, vsprintf can be used. – JcMaco Nov 29 '11 at 18:51
  • 3
    For Linux C++, I modified your code with `#include `, `#include `, replaced `wvsprinf` with `vsnprinf`. Then replaced `OutputDebugString` with `std::clog << buffer << std::flush;`. – enthusiasticgeek Aug 01 '12 at 14:27
  • 4
    In release builds, Visual Studio generates a warning C4013 (warning level 3) and also warnings C4555 (warning level all) for each TRACE occurrence. To prevent the warnings, I changed the `false && _trace` part into `__noop`. This is also the way MFC does it. – GOTO 0 Aug 30 '12 at 02:21
9

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

Ulf Lindback
  • 13,974
  • 3
  • 40
  • 31
  • I wanted to use this this approach however there is some additional ATL baggage that comes along with this for a project that is not ATL. I am working with C++/CLI with Window Forms and adding the `atltrace.h` include caused compiler errors so I took the approach of just using `OuputDebugString()` instead since I just have a few text strings to output at various places. – Richard Chambers Jun 22 '15 at 23:44
4

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

Fredrik Jansson
  • 3,764
  • 3
  • 30
  • 33
2

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

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

#ifdef ENABLE_TRACE
#  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
#else
#  define TRACE(x)
#endif

example:

#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 improvements/suggestions/contributions are welcome ;-)

oHo
  • 51,447
  • 27
  • 165
  • 200
  • I got error when tried to use it: Error (active) E2140 expression must have integral or unscoped enum type – Moti Hamo May 29 '19 at 06:25
1

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

sarat
  • 10,512
  • 7
  • 43
  • 74
0

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

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

#ifdef _DEBUG
#define TRACE printf
#else
#define TRACE 
#endif
user214810
  • 11
  • 2