Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio - How can I output debug information to debug window?

OutputDebugString method seems rather tedious and seems only limited to string and not polymorphic. What should I do if I want to output some integer or other variable type ?

Hope some function like std::cout exists !

like image 433
CDT Avatar asked May 22 '13 08:05

CDT


People also ask

How do I write to the Output window in Visual Studio?

You can write run-time messages to the Output window using the Debug class or the Trace class, which are part of the System. Diagnostics class library. Use the Debug class if you only want output in the Debug version of your program. Use the Trace class if you want output in both the Debug and Release versions.

How do I show a Debug window?

You can open most debugger windows while you are debugging your program. To see a list of debugger windows, set a breakpoint and start debugging. When you hit the breakpoint and execution stops, click Debug > Windows.

How do I forward a Debug in Visual Studio?

Begin code stepping by selecting F10 or F11. Doing so allows you to quickly find the entry point of your app. You can then continue to press step commands to navigate through the code. Run to a specific location or function, for example, by setting a breakpoint and starting your app.


4 Answers

I'm pretty sure you could write a streambuf implementation that outputs via OutputDebugString. It's not entirely straight forward, but possible.

It would certainly be possible to use something like this:

std::stringstream ss;
ss << something << another << variable << here << endl;
OutputDebugString(ss.str().c_str()); 

You may need to use MultiByteToWideChar to convert the c_str() to a wide string, if you have "UNICODE" enabled in your project.

like image 178
Mats Petersson Avatar answered Sep 27 '22 21:09

Mats Petersson


Since the accepted answer doesn't really provide a working version:

If you're not concerned with unicode - though you probably should be if you're shipping anything, I'll assume you won't be shipping it with OutputDebugString included - you can use one of the other versions, such as OutputDebugStringA:

stringstream ss;

ss << "Hello World\n";

OutputDebugStringA(ss.str().c_str());
like image 32
3Dave Avatar answered Sep 27 '22 19:09

3Dave


Use a class like this:

class stringbuilder
{
public:
  stringbuilder()
  {
  }

  template< class T >
  stringbuilder& operator << ( const T& val )
  {
    os << val;
    return *this;
  }

  operator std::string () const
  {
    return os.str();
  }

private:
  std::ostringstream os;
};

And pass the output to a wrapper around OutputDebugString (or anything else that logs strings only):

void MyOutputDebugString( const std::string& s )
{
  ::OutputDebugString( s.c_str() );
}

  //usage:
MyOutputDebugString( stringbuilder() << "integer " << 5 );
like image 37
stijn Avatar answered Sep 27 '22 20:09

stijn


A macro for Mats Petersson's answer, with unicode support:

#define odslog(msg) { std::wstringstream ss; ss << msg; OutputDebugStringW(ss.str().c_str()); }

Usage:

odslog("A string " << 123123 << L"A wide string" << "\n");
like image 42
Oneiros Avatar answered Sep 27 '22 21:09

Oneiros