#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/WithColor.h"
#include <memory>
class CommandReturnObject {
public:
- CommandReturnObject();
+ CommandReturnObject(bool colors);
~CommandReturnObject();
///
/// 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 colors = false);
/// Construct a default Stream, not binary, host byte order and host addr
/// size.
///
- Stream();
+ Stream(bool colors = false);
// FIXME: Streams should not be copyable.
Stream(const Stream &other) : m_forwarder(*this) { (*this) = other; }
}
public:
- RawOstreamForward(Stream &target)
- : llvm::raw_ostream(/*unbuffered*/ true), m_target(target) {}
+ RawOstreamForward(Stream &target, bool colors = false)
+ : llvm::raw_ostream(/*unbuffered*/ true), m_target(target) {
+ enable_colors(colors);
+ }
};
RawOstreamForward m_forwarder;
};
class StreamTee : public Stream {
public:
- StreamTee() : Stream(), m_streams_mutex(), m_streams() {}
+ StreamTee(bool colors = false)
+ : Stream(colors), m_streams_mutex(), m_streams() {}
StreamTee(lldb::StreamSP &stream_sp)
: Stream(), m_streams_mutex(), m_streams() {
class lldb_private::SBCommandReturnObjectImpl {
public:
SBCommandReturnObjectImpl()
- : m_ptr(new CommandReturnObject()), m_owned(true) {}
+ : m_ptr(new CommandReturnObject(false)), m_owned(true) {}
SBCommandReturnObjectImpl(CommandReturnObject &ref)
: m_ptr(&ref), m_owned(false) {}
SBCommandReturnObjectImpl(const SBCommandReturnObjectImpl &rhs)
ExecutionContext exe_ctx(context->exe_ctx_ref);
Target *target = exe_ctx.GetTargetPtr();
if (target) {
- CommandReturnObject result;
Debugger &debugger = target->GetDebugger();
+ CommandReturnObject result(debugger.GetUseColor());
+
// Rig up the results secondary output stream to the debugger's, so the
// output will come out synchronously if the debugger is set up that way.
-
StreamSP output_stream(debugger.GetAsyncOutputStream());
StreamSP error_stream(debugger.GetAsyncErrorStream());
result.SetImmediateOutputStream(output_stream);
StreamFileSP output_sp = io_handler.GetOutputStreamFileSP();
StreamFileSP error_sp = io_handler.GetErrorStreamFileSP();
- CommandReturnObject return_obj;
+ CommandReturnObject return_obj(
+ GetCommandInterpreter().GetDebugger().GetUseColor());
EvaluateExpression(line.c_str(), *output_sp, *error_sp, return_obj);
if (output_sp)
output_sp->Flush();
ExecutionContext exe_ctx(context->exe_ctx_ref);
Target *target = exe_ctx.GetTargetPtr();
if (target) {
- CommandReturnObject result;
Debugger &debugger = target->GetDebugger();
+ CommandReturnObject result(debugger.GetUseColor());
+
// Rig up the results secondary output stream to the debugger's, so the
// output will come out synchronously if the debugger is set up that
// way.
-
StreamSP output_stream(debugger.GetAsyncOutputStream());
StreamSP error_stream(debugger.GetAsyncErrorStream());
result.SetImmediateOutputStream(output_stream);
ci.SetPromptOnQuit(false);
// Execute the command
- CommandReturnObject result;
+ CommandReturnObject result(debugger.GetUseColor());
result.SetImmediateOutputStream(output_sp);
result.SetImmediateErrorStream(error_sp);
ci.HandleCommand(code.c_str(), eLazyBoolNo, result);
std::string options_string(options_args);
// TODO: Find a way to propagate errors in this CommandReturnObject up the
// stack.
- CommandReturnObject result;
+ CommandReturnObject result(false);
// Check to see if the command being aliased can take any command options.
Options *options = cmd_obj_sp->GetOptions();
if (options) {
static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
- CommandReturnObject result;
+ CommandReturnObject result(m_debugger.GetUseColor());
LoadCommandDictionary();
m_debugger.GetPrompt().str().c_str(), cmd);
}
- CommandReturnObject tmp_result;
+ CommandReturnObject tmp_result(m_debugger.GetUseColor());
// If override_context is not NULL, pass no_context_switching = true for
// HandleCommand() since we updated our context already.
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
} else {
// Can we do anything generic with the options?
Options *cur_options = GetOptions();
- CommandReturnObject result;
+ CommandReturnObject result(m_interpreter.GetDebugger().GetUseColor());
OptionElementVector opt_element_vector;
if (cur_options != nullptr) {
using namespace lldb;
using namespace lldb_private;
+static llvm::raw_ostream &error(Stream &strm) {
+ return llvm::WithColor(strm.AsRawOstream(), llvm::HighlightColor::Error,
+ llvm::ColorMode::Enable)
+ << "error: ";
+}
+
+static llvm::raw_ostream &warning(Stream &strm) {
+ return llvm::WithColor(strm.AsRawOstream(), llvm::HighlightColor::Warning,
+ llvm::ColorMode::Enable)
+ << "warning: ";
+}
+
static void DumpStringToStreamWithNewline(Stream &strm, const std::string &s) {
bool add_newline = false;
if (!s.empty()) {
strm.EOL();
}
-CommandReturnObject::CommandReturnObject()
- : m_out_stream(), m_err_stream(), m_status(eReturnStatusStarted),
- m_did_change_process_state(false), m_interactive(true) {}
+CommandReturnObject::CommandReturnObject(bool colors)
+ : m_out_stream(colors), m_err_stream(colors),
+ m_status(eReturnStatusStarted), m_did_change_process_state(false),
+ m_interactive(true) {}
CommandReturnObject::~CommandReturnObject() {}
const std::string &s = std::string(sstrm.GetString());
if (!s.empty()) {
- Stream &error_strm = GetErrorStream();
- error_strm.PutCString("error: ");
- DumpStringToStreamWithNewline(error_strm, s);
+ error(GetErrorStream());
+ DumpStringToStreamWithNewline(GetErrorStream(), s);
}
}
sstrm.PrintfVarArg(format, args);
va_end(args);
- GetErrorStream() << "warning: " << sstrm.GetString();
+ warning(GetErrorStream()) << sstrm.GetString();
}
void CommandReturnObject::AppendMessage(llvm::StringRef in_string) {
void CommandReturnObject::AppendWarning(llvm::StringRef in_string) {
if (in_string.empty())
return;
- GetErrorStream() << "warning: " << in_string << "\n";
+ warning(GetErrorStream()) << in_string << '\n';
}
// Similar to AppendWarning, but do not prepend 'warning: ' to message, and
void CommandReturnObject::AppendError(llvm::StringRef in_string) {
if (in_string.empty())
return;
- GetErrorStream() << "error: " << in_string << "\n";
+ error(GetErrorStream()) << in_string << '\n';
}
void CommandReturnObject::SetError(const Status &error,
EnableOptionsSP options_sp(new EnableOptions());
options_sp->NotifyOptionParsingStarting(&exe_ctx);
- CommandReturnObject result;
+ CommandReturnObject result(debugger.GetUseColor());
// Parse the arguments.
auto options_property_sp =
}
// Run the command.
- CommandReturnObject return_object;
+ CommandReturnObject return_object(interpreter.GetDebugger().GetUseColor());
interpreter.HandleCommand(command_stream.GetData(), eLazyBoolNo,
return_object);
return return_object.Succeeded();
if (!any_active_hooks)
return;
- CommandReturnObject result;
+ CommandReturnObject result(m_debugger.GetUseColor());
std::vector<ExecutionContext> exc_ctx_with_reasons;
std::vector<SymbolContext> sym_ctx_with_reasons;
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 colors)
: 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_forwarder(*this, colors) {}
-Stream::Stream()
+Stream::Stream(bool colors)
: m_flags(0), m_addr_size(4), m_byte_order(endian::InlHostByteOrder()),
- m_indent_level(0), m_forwarder(*this) {}
+ m_indent_level(0), m_forwarder(*this, colors) {}
// Destructor
Stream::~Stream() {}
-# RUN: %lldb --no-use-colors -s %s | FileCheck %s
-settings show use-color
-# CHECK: use-color (boolean) = false
-q
+RUN: not %lldb -b --no-use-colors -o 'settings show use-color' -o 'bogus' 2>&1 | FileCheck %s --check-prefix NOCOLOR
+NOCOLOR: use-color (boolean) = false
+NOCOLOR: error: 'bogus' is not a valid command
--- /dev/null
+UNSUPPORTED: system-windows
+
+RUN: not %lldb -b -o 'settings set use-color true' -o 'settings show use-color' -o 'bogus' > %t 2>&1
+RUN: cat -e %t | FileCheck %s --check-prefix COLOR
+COLOR: use-color (boolean) = true
+# The [[ confuses FileCheck so regex match it.
+COLOR: {{.+}}0;1;31merror: {{.+}}0m'bogus' is not a valid command
std::string Command = substitute(Line);
P.formatLine("Command: {0}", Command);
- CommandReturnObject Result;
+ CommandReturnObject Result(/*colors*/ false);
if (!Dbg.GetCommandInterpreter().HandleCommand(
Command.c_str(), /*add_to_history*/ eLazyBoolNo, Result)) {
P.formatLine("Failed: {0}", Result.GetErrorData());
LanguageSet languages;
if (!Language.empty())
languages.Insert(Language::GetLanguageTypeFromString(Language));
-
+
DenseSet<SymbolFile *> SearchedFiles;
TypeMap Map;
if (!Name.empty())
// Set up a Process. In order to allocate memory within a target, this
// process must be alive and must support JIT'ing.
- CommandReturnObject Result;
+ CommandReturnObject Result(/*colors*/ false);
Dbg.SetAsyncExecution(false);
CommandInterpreter &CI = Dbg.GetCommandInterpreter();
auto IssueCmd = [&](const char *Cmd) -> bool {
auto Dbg = lldb_private::Debugger::CreateInstance();
ModuleList::GetGlobalModuleListProperties().SetEnableExternalLookup(false);
- CommandReturnObject Result;
+ CommandReturnObject Result(/*colors*/ false);
Dbg->GetCommandInterpreter().HandleCommand(
"settings set plugin.process.gdb-remote.packet-timeout 60",
/*add_to_history*/ eLazyBoolNo, Result);
ASSERT_TRUE(debugger_sp);
ScriptInterpreterLua script_interpreter(*debugger_sp);
- CommandReturnObject result;
+ CommandReturnObject result(/*colors*/ false);
EXPECT_TRUE(script_interpreter.ExecuteOneLine("foo = 1", &result));
EXPECT_FALSE(script_interpreter.ExecuteOneLine("nil = foo", &result));
EXPECT_TRUE(result.GetErrorData().startswith(