Index: lldb/include/lldb/Interpreter/CommandReturnObject.h =================================================================== --- lldb/include/lldb/Interpreter/CommandReturnObject.h +++ lldb/include/lldb/Interpreter/CommandReturnObject.h @@ -16,6 +16,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/Support/FormatVariadic.h" +#include "llvm/Support/WithColor.h" #include @@ -23,7 +24,7 @@ class CommandReturnObject { public: - CommandReturnObject(); + CommandReturnObject(bool colors = false); ~CommandReturnObject(); @@ -150,6 +151,7 @@ StreamTee m_err_stream; lldb::ReturnStatus m_status; + llvm::ColorMode m_colors; bool m_did_change_process_state; bool m_interactive; // If true, then the input handle from the debugger will // be hooked up Index: lldb/include/lldb/Utility/Stream.h =================================================================== --- lldb/include/lldb/Utility/Stream.h +++ lldb/include/lldb/Utility/Stream.h @@ -404,7 +404,9 @@ public: RawOstreamForward(Stream &target) - : llvm::raw_ostream(/*unbuffered*/ true), m_target(target) {} + : llvm::raw_ostream(/*unbuffered*/ true), m_target(target) { + enable_colors(true); + } }; RawOstreamForward m_forwarder; }; Index: lldb/source/Interpreter/CommandInterpreter.cpp =================================================================== --- lldb/source/Interpreter/CommandInterpreter.cpp +++ lldb/source/Interpreter/CommandInterpreter.cpp @@ -209,7 +209,7 @@ static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION); - CommandReturnObject result; + CommandReturnObject result(m_debugger.GetUseColor()); LoadCommandDictionary(); @@ -2792,7 +2792,7 @@ StartHandlingCommand(); - lldb_private::CommandReturnObject result; + lldb_private::CommandReturnObject result(m_debugger.GetUseColor()); HandleCommand(line.c_str(), eLazyBoolCalculate, result); // Now emit the command output text from the command we just executed Index: lldb/source/Interpreter/CommandReturnObject.cpp =================================================================== --- lldb/source/Interpreter/CommandReturnObject.cpp +++ lldb/source/Interpreter/CommandReturnObject.cpp @@ -28,8 +28,9 @@ strm.EOL(); } -CommandReturnObject::CommandReturnObject() +CommandReturnObject::CommandReturnObject(bool colors) : m_out_stream(), m_err_stream(), m_status(eReturnStatusStarted), + m_colors(colors ? llvm::ColorMode::Enable : llvm::ColorMode::Disable), m_did_change_process_state(false), m_interactive(true) {} CommandReturnObject::~CommandReturnObject() {} @@ -45,9 +46,10 @@ const std::string &s = std::string(sstrm.GetString()); if (!s.empty()) { - Stream &error_strm = GetErrorStream(); - error_strm.PutCString("error: "); - DumpStringToStreamWithNewline(error_strm, s); + llvm::WithColor(GetErrorStream().AsRawOstream(), + llvm::HighlightColor::Error, m_colors) + << "error: "; + DumpStringToStreamWithNewline(GetErrorStream(), s); } } @@ -72,7 +74,9 @@ sstrm.PrintfVarArg(format, args); va_end(args); - GetErrorStream() << "warning: " << sstrm.GetString(); + llvm::WithColor(GetErrorStream().AsRawOstream(), + llvm::HighlightColor::Warning, m_colors) + << "warning: " << sstrm.GetString(); } void CommandReturnObject::AppendMessage(llvm::StringRef in_string) { @@ -84,7 +88,9 @@ void CommandReturnObject::AppendWarning(llvm::StringRef in_string) { if (in_string.empty()) return; - GetErrorStream() << "warning: " << in_string << "\n"; + llvm::WithColor(GetErrorStream().AsRawOstream(), + llvm::HighlightColor::Warning, m_colors) + << "warning: " << in_string << '\n'; } // Similar to AppendWarning, but do not prepend 'warning: ' to message, and @@ -99,7 +105,9 @@ void CommandReturnObject::AppendError(llvm::StringRef in_string) { if (in_string.empty()) return; - GetErrorStream() << "error: " << in_string << "\n"; + llvm::WithColor(GetErrorStream().AsRawOstream(), llvm::HighlightColor::Error, + m_colors) + << "error: " << in_string << '\n'; } void CommandReturnObject::SetError(const Status &error, Index: llvm/lib/Support/raw_ostream.cpp =================================================================== --- llvm/lib/Support/raw_ostream.cpp +++ llvm/lib/Support/raw_ostream.cpp @@ -510,10 +510,8 @@ (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg) : sys::Process::OutputColor(static_cast(colors), bold, bg); - if (colorcode) { - size_t len = strlen(colorcode); - write(colorcode, len); - } + if (colorcode) + write(colorcode, strlen(colorcode)); return *this; } @@ -523,11 +521,8 @@ if (sys::Process::ColorNeedsFlush()) flush(); - const char *colorcode = sys::Process::ResetColor(); - if (colorcode) { - size_t len = strlen(colorcode); - write(colorcode, len); - } + if (const char *colorcode = sys::Process::ResetColor()) + write(colorcode, strlen(colorcode)); return *this; } @@ -537,11 +532,8 @@ if (sys::Process::ColorNeedsFlush()) flush(); - const char *colorcode = sys::Process::OutputReverse(); - if (colorcode) { - size_t len = strlen(colorcode); - write(colorcode, len); - } + if (const char *colorcode = sys::Process::OutputReverse()) + write(colorcode, strlen(colorcode)); return *this; } Index: llvm/unittests/Support/raw_ostream_test.cpp =================================================================== --- llvm/unittests/Support/raw_ostream_test.cpp +++ llvm/unittests/Support/raw_ostream_test.cpp @@ -357,12 +357,16 @@ Sos.changeColor(raw_ostream::YELLOW); EXPECT_TRUE(Sos.str().empty()); } + { std::string S; raw_string_ostream Sos(S); Sos.enable_colors(true); Sos.changeColor(raw_ostream::YELLOW); EXPECT_FALSE(Sos.str().empty()); +#ifdef LLVM_ON_UNIX + EXPECT_EQ("\x1B[0;33m", Sos.str()); +#endif } }