1 //===-- CommandReturnObject.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Interpreter/CommandReturnObject.h"
14 // Other libraries and framework includes
16 #include "lldb/Utility/Status.h"
17 #include "lldb/Utility/StreamString.h"
20 using namespace lldb_private;
22 static void DumpStringToStreamWithNewline(Stream &strm, const std::string &s,
23 bool add_newline_if_empty) {
24 bool add_newline = false;
26 add_newline = add_newline_if_empty;
28 // We already checked for empty above, now make sure there is a newline
29 // in the error, and if there isn't one, add one.
30 strm.Write(s.c_str(), s.size());
32 const char last_char = *s.rbegin();
33 add_newline = last_char != '\n' && last_char != '\r';
39 CommandReturnObject::CommandReturnObject()
40 : m_out_stream(), m_err_stream(), m_status(eReturnStatusStarted),
41 m_did_change_process_state(false), m_interactive(true),
42 m_abnormal_stop_was_expected(false) {}
44 CommandReturnObject::~CommandReturnObject() {}
46 void CommandReturnObject::AppendErrorWithFormat(const char *format, ...) {
50 va_start(args, format);
52 sstrm.PrintfVarArg(format, args);
55 const std::string &s = sstrm.GetString();
57 Stream &error_strm = GetErrorStream();
58 error_strm.PutCString("error: ");
59 DumpStringToStreamWithNewline(error_strm, s, false);
63 void CommandReturnObject::AppendMessageWithFormat(const char *format, ...) {
67 va_start(args, format);
69 sstrm.PrintfVarArg(format, args);
72 GetOutputStream() << sstrm.GetString();
75 void CommandReturnObject::AppendWarningWithFormat(const char *format, ...) {
79 va_start(args, format);
81 sstrm.PrintfVarArg(format, args);
84 GetErrorStream() << "warning: " << sstrm.GetString();
87 void CommandReturnObject::AppendMessage(llvm::StringRef in_string) {
88 if (in_string.empty())
90 GetOutputStream() << in_string << "\n";
93 void CommandReturnObject::AppendWarning(llvm::StringRef in_string) {
94 if (in_string.empty())
96 GetErrorStream() << "warning: " << in_string << "\n";
99 // Similar to AppendWarning, but do not prepend 'warning: ' to message, and
100 // don't append "\n" to the end of it.
102 void CommandReturnObject::AppendRawWarning(llvm::StringRef in_string) {
103 if (in_string.empty())
105 GetErrorStream() << in_string;
108 void CommandReturnObject::AppendError(llvm::StringRef in_string) {
109 if (in_string.empty())
111 GetErrorStream() << "error: " << in_string << "\n";
114 void CommandReturnObject::SetError(const Status &error,
115 const char *fallback_error_cstr) {
116 const char *error_cstr = error.AsCString();
117 if (error_cstr == nullptr)
118 error_cstr = fallback_error_cstr;
119 SetError(error_cstr);
122 void CommandReturnObject::SetError(llvm::StringRef error_str) {
123 if (error_str.empty())
126 AppendError(error_str);
127 SetStatus(eReturnStatusFailed);
130 // Similar to AppendError, but do not prepend 'Status: ' to message, and
131 // don't append "\n" to the end of it.
133 void CommandReturnObject::AppendRawError(llvm::StringRef in_string) {
134 if (in_string.empty())
136 GetErrorStream() << in_string;
139 void CommandReturnObject::SetStatus(ReturnStatus status) { m_status = status; }
141 ReturnStatus CommandReturnObject::GetStatus() { return m_status; }
143 bool CommandReturnObject::Succeeded() {
144 return m_status <= eReturnStatusSuccessContinuingResult;
147 bool CommandReturnObject::HasResult() {
148 return (m_status == eReturnStatusSuccessFinishResult ||
149 m_status == eReturnStatusSuccessContinuingResult);
152 void CommandReturnObject::Clear() {
153 lldb::StreamSP stream_sp;
154 stream_sp = m_out_stream.GetStreamAtIndex(eStreamStringIndex);
156 static_cast<StreamString *>(stream_sp.get())->Clear();
157 stream_sp = m_err_stream.GetStreamAtIndex(eStreamStringIndex);
159 static_cast<StreamString *>(stream_sp.get())->Clear();
160 m_status = eReturnStatusStarted;
161 m_did_change_process_state = false;
162 m_interactive = true;
165 bool CommandReturnObject::GetDidChangeProcessState() {
166 return m_did_change_process_state;
169 void CommandReturnObject::SetDidChangeProcessState(bool b) {
170 m_did_change_process_state = b;
173 bool CommandReturnObject::GetInteractive() const { return m_interactive; }
175 void CommandReturnObject::SetInteractive(bool b) { m_interactive = b; }