0

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.

KompjoeFriek
  • 3,572
  • 1
  • 22
  • 35
cup
  • 7,589
  • 4
  • 19
  • 42
  • Defining a `_T` macro is an especially bad idea because it already exists in tchar.h. `_T` is a [reserved identifier](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) anyway. – chris Sep 12 '15 at 16:24
  • Since you're using `OutputDebugString` you'll likely have to download or create your own debug capture tool that recognizes whatever terminal escape codes you want to support. It's actually not that difficult. – Captain Obvlious Sep 12 '15 at 16:27

2 Answers2

2

If I understand you right, you can do such things with help of extensions: VSColorOutput or VSCommands for Visual Studio

They can change appearance in the output window. You can use out of the box behavior or add some special characters when outputting with DOut class and create own rule how to color it.

VSColorOutput hooks into the the classifier chain of Visual Studio. This allows VSColorOutput to monitor every line sent to the output window. A list of classifiers, consisting of regular expressions and classifications is checked. The first matching expression determines the classification the line of text. If no patterns match, then line is classified as a common build text.

Walking.In.The.Air
  • 682
  • 2
  • 5
  • 13
  • What do those tools do? What part of the post do you think they address? Why is this information missing from your link only answer? – Captain Obvlious Sep 12 '15 at 16:29
  • They can change appearance in the output window. You can use out of the box behavior or add some special characters when outputting with DOut class and create own rule how to color it. – Walking.In.The.Air Sep 12 '15 at 16:33
  • This extensions should be downloaded to use. So I added links in order to save your time by googling =) Thank you for feedback - I've update my answer a bit. – Walking.In.The.Air Sep 12 '15 at 16:55
  • I had a look at VSColorOutput - their **More Information** link goes to some Thai webpage. I think I'll try these out on a VM first. I don't really trust the links when they go to somewhere obscure. – cup Sep 12 '15 at 20:54
0

Actually, the answer is easier than I thought. Just right click in the output window and select Program Output. It just shows you the program output without all the other bits. No need for colours.

Also, it is available from VS2005 up to the current version without any need for installing different Visual Studio extensions for the different versions of VS.

cup
  • 7,589
  • 4
  • 19
  • 42