Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the TRACE macro in non-MFC projects?

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?

like image 421
jagttt Avatar asked Jan 30 '09 06:01

jagttt


People also ask

What is trace macro?

The TRACE macroTo display messages from your program in the debugger Output window, you can use the ATLTRACE macro or the MFC TRACE macro. Like assertions, the trace macros are active only in the Debug version of your program and disappear when compiled in the Release version.

What is Trace in Visual Studio?

To trace partially trusted code that is running in a sandbox in Visual Studio, do not add trace listeners. Instead, view the Trace and Debug messages in the Output window. The Trace class provides properties to get or set the level of Indent and the IndentSize, and whether to AutoFlush after each write.


1 Answers

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

like image 109
Ferruccio Avatar answered Sep 20 '22 10:09

Ferruccio