1 //===-- SBStream.cpp ------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBStream.h"
11 #include "lldb/API/SBFile.h"
12 #include "lldb/Host/FileSystem.h"
13 #include "lldb/Host/StreamFile.h"
14 #include "lldb/Utility/Instrumentation.h"
15 #include "lldb/Utility/LLDBLog.h"
16 #include "lldb/Utility/Status.h"
17 #include "lldb/Utility/Stream.h"
18 #include "lldb/Utility/StreamString.h"
21 using namespace lldb_private
;
23 SBStream::SBStream() : m_opaque_up(new StreamString()) {
24 LLDB_INSTRUMENT_VA(this);
27 SBStream::SBStream(SBStream
&&rhs
)
28 : m_opaque_up(std::move(rhs
.m_opaque_up
)), m_is_file(rhs
.m_is_file
) {}
30 SBStream::~SBStream() = default;
32 bool SBStream::IsValid() const {
33 LLDB_INSTRUMENT_VA(this);
34 return this->operator bool();
36 SBStream::operator bool() const {
37 LLDB_INSTRUMENT_VA(this);
39 return (m_opaque_up
!= nullptr);
42 // If this stream is not redirected to a file, it will maintain a local cache
43 // for the stream data which can be accessed using this accessor.
44 const char *SBStream::GetData() {
45 LLDB_INSTRUMENT_VA(this);
47 if (m_is_file
|| m_opaque_up
== nullptr)
50 return ConstString(static_cast<StreamString
*>(m_opaque_up
.get())->GetData())
54 // If this stream is not redirected to a file, it will maintain a local cache
55 // for the stream output whose length can be accessed using this accessor.
56 size_t SBStream::GetSize() {
57 LLDB_INSTRUMENT_VA(this);
59 if (m_is_file
|| m_opaque_up
== nullptr)
62 return static_cast<StreamString
*>(m_opaque_up
.get())->GetSize();
65 void SBStream::Print(const char *str
) {
66 LLDB_INSTRUMENT_VA(this, str
);
71 void SBStream::Printf(const char *format
, ...) {
75 va_start(args
, format
);
76 ref().PrintfVarArg(format
, args
);
80 void SBStream::RedirectToFile(const char *path
, bool append
) {
81 LLDB_INSTRUMENT_VA(this, path
, append
);
86 std::string local_data
;
88 // See if we have any locally backed data. If so, copy it so we can then
89 // redirect it to the file so we don't lose the data
91 local_data
= std::string(
92 static_cast<StreamString
*>(m_opaque_up
.get())->GetString());
94 auto open_options
= File::eOpenOptionWriteOnly
| File::eOpenOptionCanCreate
;
96 open_options
|= File::eOpenOptionAppend
;
98 open_options
|= File::eOpenOptionTruncate
;
100 llvm::Expected
<FileUP
> file
=
101 FileSystem::Instance().Open(FileSpec(path
), open_options
);
103 LLDB_LOG_ERROR(GetLog(LLDBLog::API
), file
.takeError(),
104 "Cannot open {1}: {0}", path
);
108 m_opaque_up
= std::make_unique
<StreamFile
>(std::move(file
.get()));
111 // If we had any data locally in our StreamString, then pass that along to
112 // the to new file we are redirecting to.
113 if (!local_data
.empty())
114 m_opaque_up
->Write(&local_data
[0], local_data
.size());
117 void SBStream::RedirectToFileHandle(FILE *fh
, bool transfer_fh_ownership
) {
118 LLDB_INSTRUMENT_VA(this, fh
, transfer_fh_ownership
);
119 FileSP file
= std::make_unique
<NativeFile
>(fh
, transfer_fh_ownership
);
120 return RedirectToFile(file
);
123 void SBStream::RedirectToFile(SBFile file
) {
124 LLDB_INSTRUMENT_VA(this, file
)
125 RedirectToFile(file
.GetFile());
128 void SBStream::RedirectToFile(FileSP file_sp
) {
129 LLDB_INSTRUMENT_VA(this, file_sp
);
131 if (!file_sp
|| !file_sp
->IsValid())
134 std::string local_data
;
136 // See if we have any locally backed data. If so, copy it so we can then
137 // redirect it to the file so we don't lose the data
139 local_data
= std::string(
140 static_cast<StreamString
*>(m_opaque_up
.get())->GetString());
143 m_opaque_up
= std::make_unique
<StreamFile
>(file_sp
);
146 // If we had any data locally in our StreamString, then pass that along to
147 // the to new file we are redirecting to.
148 if (!local_data
.empty())
149 m_opaque_up
->Write(&local_data
[0], local_data
.size());
152 void SBStream::RedirectToFileDescriptor(int fd
, bool transfer_fh_ownership
) {
153 LLDB_INSTRUMENT_VA(this, fd
, transfer_fh_ownership
);
155 std::string local_data
;
157 // See if we have any locally backed data. If so, copy it so we can then
158 // redirect it to the file so we don't lose the data
160 local_data
= std::string(
161 static_cast<StreamString
*>(m_opaque_up
.get())->GetString());
164 m_opaque_up
= std::make_unique
<StreamFile
>(fd
, transfer_fh_ownership
);
167 // If we had any data locally in our StreamString, then pass that along to
168 // the to new file we are redirecting to.
169 if (!local_data
.empty())
170 m_opaque_up
->Write(&local_data
[0], local_data
.size());
173 lldb_private::Stream
*SBStream::operator->() { return m_opaque_up
.get(); }
175 lldb_private::Stream
*SBStream::get() { return m_opaque_up
.get(); }
177 lldb_private::Stream
&SBStream::ref() {
178 if (m_opaque_up
== nullptr)
179 m_opaque_up
= std::make_unique
<StreamString
>();
183 void SBStream::Clear() {
184 LLDB_INSTRUMENT_VA(this);
187 // See if we have any locally backed data. If so, copy it so we can then
188 // redirect it to the file so we don't lose the data
192 static_cast<StreamString
*>(m_opaque_up
.get())->Clear();