1 //===-- CommandReturnObject.cpp ---------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/Interpreter/CommandReturnObject.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Core/Error.h" 17 #include "lldb/Core/StreamString.h" 18 19 using namespace lldb; 20 using namespace lldb_private; 21 22 static void 23 DumpStringToStreamWithNewline (Stream &strm, const std::string &s, bool add_newline_if_empty) 24 { 25 bool add_newline = false; 26 if (s.empty()) 27 { 28 add_newline = add_newline_if_empty; 29 } 30 else 31 { 32 // We already checked for empty above, now make sure there is a newline 33 // in the error, and if there isn't one, add one. 34 strm.Write(s.c_str(), s.size()); 35 36 const char last_char = *s.rbegin(); 37 add_newline = last_char != '\n' && last_char != '\r'; 38 39 } 40 if (add_newline) 41 strm.EOL(); 42 } 43 44 45 CommandReturnObject::CommandReturnObject () : 46 m_out_stream (), 47 m_err_stream (), 48 m_status (eReturnStatusStarted), 49 m_did_change_process_state (false), 50 m_interactive (true) 51 { 52 } 53 54 CommandReturnObject::~CommandReturnObject () 55 { 56 } 57 58 void 59 CommandReturnObject::AppendErrorWithFormat (const char *format, ...) 60 { 61 if (!format) 62 return; 63 va_list args; 64 va_start (args, format); 65 StreamString sstrm; 66 sstrm.PrintfVarArg(format, args); 67 va_end (args); 68 69 70 const std::string &s = sstrm.GetString(); 71 if (!s.empty()) 72 { 73 Stream &error_strm = GetErrorStream(); 74 error_strm.PutCString ("error: "); 75 DumpStringToStreamWithNewline (error_strm, s, false); 76 } 77 } 78 79 void 80 CommandReturnObject::AppendMessageWithFormat (const char *format, ...) 81 { 82 if (!format) 83 return; 84 va_list args; 85 va_start (args, format); 86 StreamString sstrm; 87 sstrm.PrintfVarArg(format, args); 88 va_end (args); 89 90 GetOutputStream().Printf("%s", sstrm.GetData()); 91 } 92 93 void 94 CommandReturnObject::AppendWarningWithFormat (const char *format, ...) 95 { 96 if (!format) 97 return; 98 va_list args; 99 va_start (args, format); 100 StreamString sstrm; 101 sstrm.PrintfVarArg(format, args); 102 va_end (args); 103 104 GetErrorStream().Printf("warning: %s", sstrm.GetData()); 105 } 106 107 void 108 CommandReturnObject::AppendMessage (const char *in_string) 109 { 110 if (!in_string) 111 return; 112 GetOutputStream().Printf("%s\n", in_string); 113 } 114 115 void 116 CommandReturnObject::AppendWarning (const char *in_string) 117 { 118 if (!in_string || *in_string == '\0') 119 return; 120 GetErrorStream().Printf("warning: %s\n", in_string); 121 } 122 123 // Similar to AppendWarning, but do not prepend 'warning: ' to message, and 124 // don't append "\n" to the end of it. 125 126 void 127 CommandReturnObject::AppendRawWarning (const char *in_string) 128 { 129 if (in_string && in_string[0]) 130 GetErrorStream().PutCString(in_string); 131 } 132 133 void 134 CommandReturnObject::AppendError (const char *in_string) 135 { 136 if (!in_string || *in_string == '\0') 137 return; 138 GetErrorStream().Printf ("error: %s\n", in_string); 139 } 140 141 void 142 CommandReturnObject::SetError (const Error &error, const char *fallback_error_cstr) 143 { 144 const char *error_cstr = error.AsCString(); 145 if (error_cstr == nullptr) 146 error_cstr = fallback_error_cstr; 147 SetError(error_cstr); 148 } 149 150 void 151 CommandReturnObject::SetError (const char *error_cstr) 152 { 153 if (error_cstr) 154 { 155 AppendError (error_cstr); 156 SetStatus (eReturnStatusFailed); 157 } 158 } 159 160 // Similar to AppendError, but do not prepend 'Error: ' to message, and 161 // don't append "\n" to the end of it. 162 163 void 164 CommandReturnObject::AppendRawError (const char *in_string) 165 { 166 if (in_string && in_string[0]) 167 GetErrorStream().PutCString(in_string); 168 } 169 170 void 171 CommandReturnObject::SetStatus (ReturnStatus status) 172 { 173 m_status = status; 174 } 175 176 ReturnStatus 177 CommandReturnObject::GetStatus () 178 { 179 return m_status; 180 } 181 182 bool 183 CommandReturnObject::Succeeded () 184 { 185 return m_status <= eReturnStatusSuccessContinuingResult; 186 } 187 188 bool 189 CommandReturnObject::HasResult () 190 { 191 return (m_status == eReturnStatusSuccessFinishResult || 192 m_status == eReturnStatusSuccessContinuingResult); 193 } 194 195 void 196 CommandReturnObject::Clear() 197 { 198 lldb::StreamSP stream_sp; 199 stream_sp = m_out_stream.GetStreamAtIndex (eStreamStringIndex); 200 if (stream_sp) 201 static_cast<StreamString *>(stream_sp.get())->Clear(); 202 stream_sp = m_err_stream.GetStreamAtIndex (eStreamStringIndex); 203 if (stream_sp) 204 static_cast<StreamString *>(stream_sp.get())->Clear(); 205 m_status = eReturnStatusStarted; 206 m_did_change_process_state = false; 207 m_interactive = true; 208 } 209 210 bool 211 CommandReturnObject::GetDidChangeProcessState () 212 { 213 return m_did_change_process_state; 214 } 215 216 void 217 CommandReturnObject::SetDidChangeProcessState (bool b) 218 { 219 m_did_change_process_state = b; 220 } 221 222 223 bool 224 CommandReturnObject::GetInteractive () const 225 { 226 return m_interactive; 227 } 228 229 void 230 CommandReturnObject::SetInteractive (bool b) 231 { 232 m_interactive = b; 233 } 234 235 236