I sometimes use the class DOut (listed below) for debugging
#include<fstream>
#include<iostream>
#include<sstream>
#define WIN32_LEAN_AND_MEAN
#include<Windows.h>
#ifdef UNICODE
#define tostream wostream
#define tostringstream wostringstream
#define _T(x) L##x
#else
#define tostream ostream
#define tostringstream ostringstream
#define _T(x) x
#endif
class DOut : public std::tostringstream
{
public:
//http://stackoverflow.com/questions/2212776/overload-handling-of-stdendl
DOut& operator << (std::tostream&(*f)(std::tostream&))
{
if (f == std::endl)
{
*this << _T("\n");
OutputDebugString(str().c_str());
str(_T(""));
}
else
{
*this << f;
}
return *this;
}
//https://hbfs.wordpress.com/2010/12/21/c-logging/
template <typename TT>
inline DOut & operator << (const TT& t)
{
(*(std::tostringstream*) this) << t;
return *this;
}
};
int main()
{
DOut dout;
int x = 20;
dout << "x = " << x << std::endl;
dout << "x * x = " << x * x << std::endl;
dout << "hexq=" << x*x << "=" << std::hex << x * x << std::endl;
}
It works quite well except it gets interleaved with all the VS output information. For example
'dout.exe': Loaded 'U:\GCS\test\dout\Debug\dout.exe', Symbols loaded.
'dout.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
'dout.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
'dout.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcp90d.dll'
'dout.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcr90d.dll'
x = 20
x * x = 400
hexq=400=190
The program '[3108] dout.exe: Native' has exited with code 0 (0x0).
The above is not strictly accurate as I don't know how to get stackoverflow to display monochrome text. All the text is just in a single colour. I could always output it to a file but this is convenient as I don't always have a console window and I don't have to worry about chdirs which change where the output file is written to.
I just wondering whether it is possible to output my debug information in a different colour. I've tried ANSI escape sequences but they don't work.