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(); Index: lldb/include/lldb/Utility/Stream.h =================================================================== --- lldb/include/lldb/Utility/Stream.h +++ lldb/include/lldb/Utility/Stream.h @@ -56,7 +56,8 @@ /// /// Construct with dump flags \a flags and the default address size. \a /// flags can be any of the above enumeration logical OR'ed together. - Stream(uint32_t flags, uint32_t addr_size, lldb::ByteOrder byte_order); + Stream(uint32_t flags, uint32_t addr_size, lldb::ByteOrder byte_order, + bool color = false); /// Construct a default Stream, not binary, host byte order and host addr /// size. @@ -71,6 +72,7 @@ m_addr_size = rhs.m_addr_size; m_byte_order = rhs.m_byte_order; m_indent_level = rhs.m_indent_level; + m_color_enabled = rhs.m_color_enabled; return *this; } @@ -279,6 +281,18 @@ /// Increment the current indentation level. void IndentMore(unsigned amount = 2); + /// The color accessor. + /// + /// \return + /// True if colors are enabled. + bool HasColors() const; + + /// The color accessor. + /// + /// \param[in] color + /// The new color value. + void SetColors(bool colors); + /// Output an offset value. /// /// Put an offset \a uval out to the stream using the printf format in \a @@ -365,6 +379,7 @@ m_byte_order; ///< Byte order to use when encoding scalar types. unsigned m_indent_level; ///< Indention level. std::size_t m_bytes_written = 0; ///< Number of bytes written so far. + bool m_color_enabled; void _PutHex8(uint8_t uvalue, bool add_prefix); @@ -398,13 +413,17 @@ m_target.Write(Ptr, Size); } + bool has_colors() const override { return true; } + uint64_t current_pos() const override { return m_target.GetWrittenBytes(); } public: RawOstreamForward(Stream &target) - : llvm::raw_ostream(/*unbuffered*/ true), m_target(target) {} + : llvm::raw_ostream(/*unbuffered*/ true), m_target(target) { + enable_colors(target.HasColors()); + } }; 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,9 +28,12 @@ strm.EOL(); } -CommandReturnObject::CommandReturnObject() +CommandReturnObject::CommandReturnObject(bool colors) : m_out_stream(), m_err_stream(), m_status(eReturnStatusStarted), - m_did_change_process_state(false), m_interactive(true) {} + m_did_change_process_state(false), m_interactive(true) { + m_out_stream.SetColors(colors); + m_err_stream.SetColors(colors); +} CommandReturnObject::~CommandReturnObject() {} @@ -45,9 +48,8 @@ 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::error(GetErrorStream().AsRawOstream()); + DumpStringToStreamWithNewline(GetErrorStream(), s); } } @@ -72,7 +74,8 @@ sstrm.PrintfVarArg(format, args); va_end(args); - GetErrorStream() << "warning: " << sstrm.GetString(); + llvm::WithColor::warning(GetErrorStream().AsRawOstream()) + << sstrm.GetString(); } void CommandReturnObject::AppendMessage(llvm::StringRef in_string) { @@ -84,7 +87,8 @@ void CommandReturnObject::AppendWarning(llvm::StringRef in_string) { if (in_string.empty()) return; - GetErrorStream() << "warning: " << in_string << "\n"; + llvm::WithColor::warning(GetErrorStream().AsRawOstream()) + << in_string << '\n'; } // Similar to AppendWarning, but do not prepend 'warning: ' to message, and @@ -99,7 +103,7 @@ void CommandReturnObject::AppendError(llvm::StringRef in_string) { if (in_string.empty()) return; - GetErrorStream() << "error: " << in_string << "\n"; + llvm::WithColor::error(GetErrorStream().AsRawOstream()) << in_string << '\n'; } void CommandReturnObject::SetError(const Status &error, Index: lldb/source/Utility/Stream.cpp =================================================================== --- lldb/source/Utility/Stream.cpp +++ lldb/source/Utility/Stream.cpp @@ -22,13 +22,14 @@ using namespace lldb; using namespace lldb_private; -Stream::Stream(uint32_t flags, uint32_t addr_size, ByteOrder byte_order) +Stream::Stream(uint32_t flags, uint32_t addr_size, ByteOrder byte_order, + bool color) : m_flags(flags), m_addr_size(addr_size), m_byte_order(byte_order), - m_indent_level(0), m_forwarder(*this) {} + m_indent_level(0), m_color_enabled(color), m_forwarder(*this) {} Stream::Stream() : m_flags(0), m_addr_size(4), m_byte_order(endian::InlHostByteOrder()), - m_indent_level(0), m_forwarder(*this) {} + m_indent_level(0), m_color_enabled(false), m_forwarder(*this) {} // Destructor Stream::~Stream() {} @@ -174,6 +175,13 @@ m_indent_level = 0; } +bool Stream::HasColors() const { return m_color_enabled; } + +void Stream::SetColors(bool colors) { + m_color_enabled = colors; + m_forwarder.enable_colors(colors); +} + // Get the address size in bytes uint32_t Stream::GetAddressByteSize() const { return m_addr_size; }