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 !
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 !
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.
std::stringstream ss ?#include <sstream> then.const char * not compatible with LPCWSTR.std::wstringstream instead (note the w), since all Windows code should be written using Unicode strings.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()); 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 );