LLDB  mainline
CommandReturnObject.cpp
Go to the documentation of this file.
1 //===-- CommandReturnObject.cpp -------------------------------------------===//
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 llvm::raw_ostream &error(Stream &strm) {
18  return llvm::WithColor(strm.AsRawOstream(), llvm::HighlightColor::Error,
19  llvm::ColorMode::Enable)
20  << "error: ";
21 }
22 
23 static llvm::raw_ostream &warning(Stream &strm) {
24  return llvm::WithColor(strm.AsRawOstream(), llvm::HighlightColor::Warning,
25  llvm::ColorMode::Enable)
26  << "warning: ";
27 }
28 
29 static void DumpStringToStreamWithNewline(Stream &strm, const std::string &s) {
30  bool add_newline = false;
31  if (!s.empty()) {
32  // We already checked for empty above, now make sure there is a newline in
33  // 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  if (add_newline)
40  strm.EOL();
41 }
42 
43 CommandReturnObject::CommandReturnObject(bool colors)
44  : m_out_stream(colors), m_err_stream(colors) {}
45 
46 void CommandReturnObject::AppendErrorWithFormat(const char *format, ...) {
48 
49  if (!format)
50  return;
51  va_list args;
52  va_start(args, format);
53  StreamString sstrm;
54  sstrm.PrintfVarArg(format, args);
55  va_end(args);
56 
57  const std::string &s = std::string(sstrm.GetString());
58  if (!s.empty()) {
61  }
62 }
63 
64 void CommandReturnObject::AppendMessageWithFormat(const char *format, ...) {
65  if (!format)
66  return;
67  va_list args;
68  va_start(args, format);
69  StreamString sstrm;
70  sstrm.PrintfVarArg(format, args);
71  va_end(args);
72 
73  GetOutputStream() << sstrm.GetString();
74 }
75 
76 void CommandReturnObject::AppendWarningWithFormat(const char *format, ...) {
77  if (!format)
78  return;
79  va_list args;
80  va_start(args, format);
81  StreamString sstrm;
82  sstrm.PrintfVarArg(format, args);
83  va_end(args);
84 
85  warning(GetErrorStream()) << sstrm.GetString();
86 }
87 
88 void CommandReturnObject::AppendMessage(llvm::StringRef in_string) {
89  if (in_string.empty())
90  return;
91  GetOutputStream() << in_string.rtrim() << '\n';
92 }
93 
94 void CommandReturnObject::AppendWarning(llvm::StringRef in_string) {
95  if (in_string.empty())
96  return;
97  warning(GetErrorStream()) << in_string.rtrim() << '\n';
98 }
99 
100 void CommandReturnObject::AppendError(llvm::StringRef in_string) {
102  if (in_string.empty())
103  return;
104  error(GetErrorStream()) << in_string.rtrim() << '\n';
105 }
106 
108  const char *fallback_error_cstr) {
109  AppendError(error.AsCString(fallback_error_cstr));
110 }
111 
112 // Similar to AppendError, but do not prepend 'Status: ' to message, and don't
113 // append "\n" to the end of it.
114 
115 void CommandReturnObject::AppendRawError(llvm::StringRef in_string) {
117  assert(!in_string.empty() && "Expected a non-empty error message");
118  GetErrorStream() << in_string;
119 }
120 
122 
124 
127 }
128 
132 }
133 
135  lldb::StreamSP stream_sp;
137  if (stream_sp)
138  static_cast<StreamString *>(stream_sp.get())->Clear();
140  if (stream_sp)
141  static_cast<StreamString *>(stream_sp.get())->Clear();
145  m_interactive = true;
146 }
147 
150 }
151 
154 }
155 
157 
159 
162 }
163 
166 }
lldb_private::CommandReturnObject::HasResult
bool HasResult()
Definition: CommandReturnObject.cpp:129
lldb_private::CommandReturnObject::GetStatus
lldb::ReturnStatus GetStatus()
Definition: CommandReturnObject.cpp:123
lldb::eReturnStatusSuccessContinuingResult
@ eReturnStatusSuccessContinuingResult
Definition: lldb-enumerations.h:263
lldb_private::CommandReturnObject::SetError
void SetError(const Status &error, const char *fallback_error_cstr=nullptr)
Definition: CommandReturnObject.cpp:107
lldb_private::CommandReturnObject::m_status
lldb::ReturnStatus m_status
Definition: CommandReturnObject.h:161
lldb_private::CommandReturnObject::AppendRawError
void AppendRawError(llvm::StringRef in_string)
Definition: CommandReturnObject.cpp:115
lldb_private::Stream
Definition: Stream.h:28
CommandReturnObject.h
lldb_private::CommandReturnObject::Clear
void Clear()
Definition: CommandReturnObject.cpp:134
lldb_private::StreamString::GetString
llvm::StringRef GetString() const
Definition: StreamString.cpp:51
lldb_private::CommandReturnObject::m_err_stream
StreamTee m_err_stream
Definition: CommandReturnObject.h:159
lldb_private::CommandReturnObject::GetSuppressImmediateOutput
bool GetSuppressImmediateOutput() const
Definition: CommandReturnObject.cpp:160
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::CommandReturnObject::GetInteractive
bool GetInteractive() const
Definition: CommandReturnObject.cpp:156
lldb_private::CommandReturnObject::m_out_stream
StreamTee m_out_stream
Definition: CommandReturnObject.h:158
lldb::ReturnStatus
ReturnStatus
Command Return Status Types.
Definition: lldb-enumerations.h:258
lldb_private::CommandReturnObject::eStreamStringIndex
@ eStreamStringIndex
Definition: CommandReturnObject.h:156
lldb_private::CommandReturnObject::SetStatus
void SetStatus(lldb::ReturnStatus status)
Definition: CommandReturnObject.cpp:121
lldb_private::CommandReturnObject::m_suppress_immediate_output
bool m_suppress_immediate_output
Definition: CommandReturnObject.h:164
lldb::eReturnStatusStarted
@ eReturnStatusStarted
Definition: lldb-enumerations.h:264
StreamString.h
lldb_private::CommandReturnObject::GetOutputStream
Stream & GetOutputStream()
Definition: CommandReturnObject.h:45
DumpStringToStreamWithNewline
static void DumpStringToStreamWithNewline(Stream &strm, const std::string &s)
Definition: CommandReturnObject.cpp:29
lldb_private::StreamString
Definition: StreamString.h:23
lldb_private::CommandReturnObject::SetSuppressImmediateOutput
void SetSuppressImmediateOutput(bool b)
Definition: CommandReturnObject.cpp:164
lldb_private::StreamTee::GetStreamAtIndex
lldb::StreamSP GetStreamAtIndex(uint32_t idx)
Definition: StreamTee.h:92
lldb_private::Stream::PrintfVarArg
size_t size_t PrintfVarArg(const char *format, va_list args)
Definition: Stream.cpp:116
lldb::eReturnStatusFailed
@ eReturnStatusFailed
Definition: lldb-enumerations.h:265
lldb_private::CommandReturnObject::GetErrorStream
Stream & GetErrorStream()
Definition: CommandReturnObject.h:55
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::CommandReturnObject::m_interactive
bool m_interactive
If true, then the input handle from the debugger will be hooked up.
Definition: CommandReturnObject.h:167
lldb_private::Status
Definition: Status.h:44
warning
static llvm::raw_ostream & warning(Stream &strm)
Definition: CommandReturnObject.cpp:23
lldb::eReturnStatusSuccessFinishResult
@ eReturnStatusSuccessFinishResult
Definition: lldb-enumerations.h:261
lldb_private::CommandReturnObject::AppendWarning
void void AppendWarning(llvm::StringRef in_string)
Definition: CommandReturnObject.cpp:94
lldb_private::Stream::EOL
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:128
lldb_private::CommandReturnObject::AppendMessageWithFormat
void AppendMessageWithFormat(const char *format,...) __attribute__((format(printf
Definition: CommandReturnObject.cpp:64
lldb_private::Stream::AsRawOstream
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:357
lldb_private::CommandReturnObject::SetInteractive
void SetInteractive(bool b)
Definition: CommandReturnObject.cpp:158
lldb_private::CommandReturnObject::AppendWarningWithFormat
void AppendWarningWithFormat(const char *format,...) __attribute__((format(printf
Definition: CommandReturnObject.cpp:76
lldb_private::CommandReturnObject::Succeeded
bool Succeeded()
Definition: CommandReturnObject.cpp:125
lldb_private::CommandReturnObject::SetDidChangeProcessState
void SetDidChangeProcessState(bool b)
Definition: CommandReturnObject.cpp:152
lldb_private::CommandReturnObject::AppendErrorWithFormat
void AppendErrorWithFormat(const char *format,...) __attribute__((format(printf
Definition: CommandReturnObject.cpp:46
Status.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::CommandReturnObject::m_did_change_process_state
bool m_did_change_process_state
Definition: CommandReturnObject.h:163
Error
llvm::Error Error
Definition: UdtRecordCompleter.cpp:29
lldb_private::Stream::Write
size_t Write(const void *src, size_t src_len)
Output character bytes to the stream.
Definition: Stream.h:101
lldb_private::CommandReturnObject::AppendError
void void AppendError(llvm::StringRef in_string)
Definition: CommandReturnObject.cpp:100
lldb
Definition: SBAddress.h:15
lldb_private::CommandReturnObject::AppendMessage
void AppendMessage(llvm::StringRef in_string)
Definition: CommandReturnObject.cpp:88
lldb_private::CommandReturnObject::GetDidChangeProcessState
bool GetDidChangeProcessState()
Definition: CommandReturnObject.cpp:148