Visual Studio - How can I output debugging information for debugging a window?

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

We hope that there is some kind of function like std::cout !

+4
source share
5 answers

I am sure that you can write an implementation of streambuf that is output via OutputDebugString . This is not entirely straightforward, but possible.

Of course, one could 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 c_str () to a wide string if the UNICODE function is enabled in your project.

+5
source

Since the accepted answer does not really provide a working version:

If you are not interested in Unicode - although you probably should if you are sending something, I assume that you will not send it with OutputDebugString enabled - you can use one of the other versions, for example OutputDebugStringA :

 stringstream ss; ss << "Hello World\n"; OutputDebugStringA(ss.str().c_str()); 
+2
source

Use the class as follows:

 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 the wrapper around the OutputDebugString (or anything else that only writes lines):

 void MyOutputDebugString( const std::string& s ) { ::OutputDebugString( s.c_str() ); } //usage: MyOutputDebugString( stringbuilder() << "integer " << 5 ); 
+1
source

Macro for Mac Petsson's answer with unicode support:

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

Using:

 odslog("A string " << 123123 << L"A wide string" << "\n"); 
+1
source

Also, if you use MFC, you can use TRACE TRACE1 TRACE2 ... macros that work like printf to output debugging.

0
source

Source: https://habr.com/ru/post/1482112/


All Articles