1 //===-- SBStream.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/API/SBStream.h"
12 #include "lldb/Core/StreamFile.h"
13 #include "lldb/Utility/Status.h"
14 #include "lldb/Utility/Stream.h"
15 #include "lldb/Utility/StreamString.h"
18 using namespace lldb_private;
20 SBStream::SBStream() : m_opaque_ap(new StreamString()), m_is_file(false) {}
22 SBStream::SBStream(SBStream &&rhs)
23 : m_opaque_ap(std::move(rhs.m_opaque_ap)), m_is_file(rhs.m_is_file) {}
25 SBStream::~SBStream() {}
27 bool SBStream::IsValid() const { return (m_opaque_ap.get() != NULL); }
29 // If this stream is not redirected to a file, it will maintain a local
30 // cache for the stream data which can be accessed using this accessor.
31 const char *SBStream::GetData() {
32 if (m_is_file || m_opaque_ap.get() == NULL)
35 return static_cast<StreamString *>(m_opaque_ap.get())->GetData();
38 // If this stream is not redirected to a file, it will maintain a local
39 // cache for the stream output whose length can be accessed using this
41 size_t SBStream::GetSize() {
42 if (m_is_file || m_opaque_ap.get() == NULL)
45 return static_cast<StreamString *>(m_opaque_ap.get())->GetSize();
48 void SBStream::Printf(const char *format, ...) {
52 va_start(args, format);
53 ref().PrintfVarArg(format, args);
57 void SBStream::RedirectToFile(const char *path, bool append) {
61 std::string local_data;
62 if (m_opaque_ap.get()) {
63 // See if we have any locally backed data. If so, copy it so we can then
64 // redirect it to the file so we don't lose the data
66 local_data = static_cast<StreamString *>(m_opaque_ap.get())->GetString();
68 StreamFile *stream_file = new StreamFile;
69 uint32_t open_options = File::eOpenOptionWrite | File::eOpenOptionCanCreate;
71 open_options |= File::eOpenOptionAppend;
73 open_options |= File::eOpenOptionTruncate;
74 stream_file->GetFile().Open(path, open_options,
75 lldb::eFilePermissionsFileDefault);
77 m_opaque_ap.reset(stream_file);
79 if (m_opaque_ap.get()) {
82 // If we had any data locally in our StreamString, then pass that along to
83 // the to new file we are redirecting to.
84 if (!local_data.empty())
85 m_opaque_ap->Write(&local_data[0], local_data.size());
90 void SBStream::RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership) {
94 std::string local_data;
95 if (m_opaque_ap.get()) {
96 // See if we have any locally backed data. If so, copy it so we can then
97 // redirect it to the file so we don't lose the data
99 local_data = static_cast<StreamString *>(m_opaque_ap.get())->GetString();
101 m_opaque_ap.reset(new StreamFile(fh, transfer_fh_ownership));
103 if (m_opaque_ap.get()) {
106 // If we had any data locally in our StreamString, then pass that along to
107 // the to new file we are redirecting to.
108 if (!local_data.empty())
109 m_opaque_ap->Write(&local_data[0], local_data.size());
114 void SBStream::RedirectToFileDescriptor(int fd, bool transfer_fh_ownership) {
115 std::string local_data;
116 if (m_opaque_ap.get()) {
117 // See if we have any locally backed data. If so, copy it so we can then
118 // redirect it to the file so we don't lose the data
120 local_data = static_cast<StreamString *>(m_opaque_ap.get())->GetString();
123 m_opaque_ap.reset(new StreamFile(::fdopen(fd, "w"), transfer_fh_ownership));
124 if (m_opaque_ap.get()) {
127 // If we had any data locally in our StreamString, then pass that along to
128 // the to new file we are redirecting to.
129 if (!local_data.empty())
130 m_opaque_ap->Write(&local_data[0], local_data.size());
135 lldb_private::Stream *SBStream::operator->() { return m_opaque_ap.get(); }
137 lldb_private::Stream *SBStream::get() { return m_opaque_ap.get(); }
139 lldb_private::Stream &SBStream::ref() {
140 if (m_opaque_ap.get() == NULL)
141 m_opaque_ap.reset(new StreamString());
142 return *m_opaque_ap.get();
145 void SBStream::Clear() {
146 if (m_opaque_ap.get()) {
147 // See if we have any locally backed data. If so, copy it so we can then
148 // redirect it to the file so we don't lose the data
152 static_cast<StreamString *>(m_opaque_ap.get())->Clear();