LLDB  mainline
CommandReturnObject.cpp
Go to the documentation of this file.
1 //===-- CommandReturnObject.cpp ---------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
11 #include "lldb/Utility/Status.h"
13 
14 using namespace lldb;
15 using namespace lldb_private;
16 
17 static void DumpStringToStreamWithNewline(Stream &strm, const std::string &s,
18  bool add_newline_if_empty) {
19  bool add_newline = false;
20  if (s.empty()) {
21  add_newline = add_newline_if_empty;
22  } else {
23  // We already checked for empty above, now make sure there is a newline in
24  // the error, and if there isn't one, add one.
25  strm.Write(s.c_str(), s.size());
26 
27  const char last_char = *s.rbegin();
28  add_newline = last_char != '\n' && last_char != '\r';
29  }
30  if (add_newline)
31  strm.EOL();
32 }
33 
34 CommandReturnObject::CommandReturnObject()
35  : m_out_stream(), m_err_stream(), m_status(eReturnStatusStarted),
36  m_did_change_process_state(false), m_interactive(true),
37  m_abnormal_stop_was_expected(false) {}
38 
40 
41 void CommandReturnObject::AppendErrorWithFormat(const char *format, ...) {
42  if (!format)
43  return;
44  va_list args;
45  va_start(args, format);
46  StreamString sstrm;
47  sstrm.PrintfVarArg(format, args);
48  va_end(args);
49 
50  const std::string &s = sstrm.GetString();
51  if (!s.empty()) {
52  Stream &error_strm = GetErrorStream();
53  error_strm.PutCString("error: ");
54  DumpStringToStreamWithNewline(error_strm, s, false);
55  }
56 }
57 
58 void CommandReturnObject::AppendMessageWithFormat(const char *format, ...) {
59  if (!format)
60  return;
61  va_list args;
62  va_start(args, format);
63  StreamString sstrm;
64  sstrm.PrintfVarArg(format, args);
65  va_end(args);
66 
67  GetOutputStream() << sstrm.GetString();
68 }
69 
70 void CommandReturnObject::AppendWarningWithFormat(const char *format, ...) {
71  if (!format)
72  return;
73  va_list args;
74  va_start(args, format);
75  StreamString sstrm;
76  sstrm.PrintfVarArg(format, args);
77  va_end(args);
78 
79  GetErrorStream() << "warning: " << sstrm.GetString();
80 }
81 
82 void CommandReturnObject::AppendMessage(llvm::StringRef in_string) {
83  if (in_string.empty())
84  return;
85  GetOutputStream() << in_string << "\n";
86 }
87 
88 void CommandReturnObject::AppendWarning(llvm::StringRef in_string) {
89  if (in_string.empty())
90  return;
91  GetErrorStream() << "warning: " << in_string << "\n";
92 }
93 
94 // Similar to AppendWarning, but do not prepend 'warning: ' to message, and
95 // don't append "\n" to the end of it.
96 
97 void CommandReturnObject::AppendRawWarning(llvm::StringRef in_string) {
98  if (in_string.empty())
99  return;
100  GetErrorStream() << in_string;
101 }
102 
103 void CommandReturnObject::AppendError(llvm::StringRef in_string) {
104  if (in_string.empty())
105  return;
106  GetErrorStream() << "error: " << in_string << "\n";
107 }
108 
110  const char *fallback_error_cstr) {
111  const char *error_cstr = error.AsCString();
112  if (error_cstr == nullptr)
113  error_cstr = fallback_error_cstr;
114  SetError(error_cstr);
115 }
116 
117 void CommandReturnObject::SetError(llvm::StringRef error_str) {
118  if (error_str.empty())
119  return;
120 
121  AppendError(error_str);
123 }
124 
125 // Similar to AppendError, but do not prepend 'Status: ' to message, and don't
126 // append "\n" to the end of it.
127 
128 void CommandReturnObject::AppendRawError(llvm::StringRef in_string) {
129  if (in_string.empty())
130  return;
131  GetErrorStream() << in_string;
132 }
133 
134 void CommandReturnObject::SetStatus(ReturnStatus status) { m_status = status; }
135 
137 
139  return m_status <= eReturnStatusSuccessContinuingResult;
140 }
141 
143  return (m_status == eReturnStatusSuccessFinishResult ||
145 }
146 
148  lldb::StreamSP stream_sp;
149  stream_sp = m_out_stream.GetStreamAtIndex(eStreamStringIndex);
150  if (stream_sp)
151  static_cast<StreamString *>(stream_sp.get())->Clear();
152  stream_sp = m_err_stream.GetStreamAtIndex(eStreamStringIndex);
153  if (stream_sp)
154  static_cast<StreamString *>(stream_sp.get())->Clear();
155  m_status = eReturnStatusStarted;
156  m_did_change_process_state = false;
157  m_interactive = true;
158 }
159 
161  return m_did_change_process_state;
162 }
163 
165  m_did_change_process_state = b;
166 }
167 
168 bool CommandReturnObject::GetInteractive() const { return m_interactive; }
169 
170 void CommandReturnObject::SetInteractive(bool b) { m_interactive = b; }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
void void AppendRawWarning(llvm::StringRef in_string)
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
void AppendErrorWithFormat(const char *format,...) __attribute__((format(printf
lldb::StreamSP GetStreamAtIndex(uint32_t idx)
Definition: StreamTee.h:91
void AppendRawError(llvm::StringRef in_string)
size_t Write(const void *src, size_t src_len)
Output character bytes to the stream.
Definition: Stream.h:100
size_t size_t PrintfVarArg(const char *format, va_list args)
Definition: Stream.cpp:115
void AppendWarning(llvm::StringRef in_string)
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:127
void AppendMessageWithFormat(const char *format,...) __attribute__((format(printf
void AppendMessage(llvm::StringRef in_string)
llvm::StringRef GetString() const
void SetError(const Status &error, const char *fallback_error_cstr=nullptr)
void void AppendError(llvm::StringRef in_string)
Definition: SBAddress.h:15
static void DumpStringToStreamWithNewline(Stream &strm, const std::string &s, bool add_newline_if_empty)
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:130
void AppendWarningWithFormat(const char *format,...) __attribute__((format(printf
void SetStatus(lldb::ReturnStatus status)
An error handling class.
Definition: Status.h:44