Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to send debug information to the Visual Studio 'Output' window

I started a blank project in Visual Studio 2010 to write a C application. How can I send debug information to the Output window (menu Debug -> Windows -> Output )? Is there a relatively simple way to implement TRACE or OutputDebugString or something similar?

like image 904
Jim Fell Avatar asked Mar 02 '12 21:03

Jim Fell


2 Answers

If you use C++, you may be interested on my portable TRACE macro.

#ifdef ENABLE_TRACE
#  ifdef _MSC_VER
#    include <windows.h>
#    include <sstream>
#    define TRACE(x)                           \
     do {  std::ostringstream s;  s << x;      \
           OutputDebugString(s.str().c_str()); \
        } while(0)
#  else
#    include <iostream>
#    define TRACE(x)  std::cerr << x << std::flush
#  endif
#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     i = 123;
   double  d = 456.789;
   TRACE ("main() i="<< i <<" d="<< d <<'\n');
}

Any improvements/suggestions/contributions are welcome ;-)

like image 152
oHo Avatar answered Oct 05 '22 16:10

oHo


You can use OutputDebugString from a VS C program.

#include <windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
    OutputDebugString(_T("Hello World\n"));
    return 0;
}

The output will only be visible if you run with debugging (Debug > Start Debugging)

In the Output window, select "Debug" for "Show output from:"

like image 45
Mike Clark Avatar answered Oct 05 '22 16:10

Mike Clark