更改调试输出的颜色

时间:2015-09-12 16:18:07

标签: c++ visual-studio

我有时会使用类DOut(下面列出的)进行调试

  #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;
  }

它工作得很好,除了它与所有VS输出信息交错。例如

'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).

以上内容并不严格准确,因为我不知道如何让stackoverflow显示单色文本。所有文字都只是一种颜色。我总是可以将它输出到一个文件,但这很方便,因为我不总是有一个控制台窗口,我不必担心改变输出文件写入位置的chdirs。

我只是想知道是否可以以不同的颜色输出我的调试信息。我已经尝试过ANSI转义序列,但它们无法正常工作。

2 个答案:

答案 0 :(得分:2)

如果我理解正确,您可以借助扩展程序执行此类操作:VSColorOutputVSCommands for Visual Studio

他们可以在输出窗口中更改外观。您可以使用开箱即用的行为或在使用DOut类输出时添加一些特殊字符,并创建自己的规则如何为其着色。

VSColorOutput挂钩到Visual Studio的分类器链。这允许VSColorOutput监视发送到输出窗口的每一行。检查由正则表达式和分类组成的分类器列表。第一个匹配表达式确定文本行的分类。如果没有模式匹配,则将行分类为公共构建文本。

答案 1 :(得分:0)

实际上,答案比我想象的要容易。只需右键单击输出窗口并选择Program Output。它只显示程序输出而没有所有其他位。不需要颜色。

此外,它可以从VS2005一直到当前版本,而无需为不同版本的VS安装不同的Visual Studio扩展。

相关问题