6

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 !

CDT
  • 10,165
  • 18
  • 66
  • 97
  • Does this answer your question? [Simplest way to write output message to 'output window' in Visual Studio 2010?](https://stackoverflow.com/questions/3179199/simplest-way-to-write-output-message-to-output-window-in-visual-studio-2010) – Josh Correia Oct 29 '20 at 23:23

5 Answers5

6

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.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • VS reported incomplete type error at `std::stringstream ss` ? – CDT May 22 '13 at 08:59
  • You need to `#include ` then. – Mats Petersson May 22 '13 at 09:00
  • 1
    Also reported incompatible type error at OutputDebugString(ss.str().c_str()); ? `const char *` not compatible with `LPCWSTR`. – CDT May 22 '13 at 09:03
  • After a minute or two the incompatible type error disappears automatically. – CDT May 22 '13 at 09:09
  • 3
    @CDT It still won't compile, though. Use `std::wstringstream` instead (note the `w`), since all Windows code should be written using Unicode strings. – Cody Gray - on strike May 22 '13 at 09:12
  • The error appeared again. It is hard to tell when VS will give the right pre-compile error detection. – CDT May 22 '13 at 09:12
  • Still no matched `<<` operator occurred at `ss << i << ':' << itemVal << std::endl;`... – CDT May 22 '13 at 09:14
  • @CDT Because `':'` is a narrow character literal (of type `char`). You need it to be a wide character literal of type `wchar_t`. To do that, declare it like this: `L':'`. Surely you've run into this before if you're written *any* Windows code. – Cody Gray - on strike May 22 '13 at 09:22
2

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());
3Dave
  • 28,657
  • 18
  • 88
  • 151
1

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 );
stijn
  • 34,664
  • 13
  • 111
  • 163
  • Thanks, but it seems I got some stackoverflow with this method. – CDT May 23 '13 at 00:15
  • that's probably beacuse the OutputDebugString warpper is in the global namespace, it shouldn't be there, edited. Anyway the principle in this answer is exactly the same as Mats Petersson's, just written in a reusable way. – stijn May 23 '13 at 07:10
1

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");
Oneiros
  • 4,328
  • 6
  • 40
  • 69
0

In addition, if you use MFC then you can use TRACE TRACE1 TRACE2 ... macros that work like printf to the debug output.

Balog Pal
  • 16,195
  • 2
  • 23
  • 37