blob: f5b5c08411c7707b2a185bf3998c7bde93d569eb [file] [log] [blame]
Greg Claytone2ae97f2010-09-17 17:42:16 +00001//===-- SBStream.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/API/SBStream.h"
11
Greg Clayton51b1e2d2011-02-09 01:08:52 +000012#include "lldb/Core/Error.h"
Greg Claytone2ae97f2010-09-17 17:42:16 +000013#include "lldb/Core/Stream.h"
14#include "lldb/Core/StreamFile.h"
15#include "lldb/Core/StreamString.h"
16
17using namespace lldb;
18using namespace lldb_private;
19
20SBStream::SBStream () :
Johnny Chenf6eaba82010-12-11 01:20:39 +000021 m_opaque_ap (new StreamString()),
Greg Claytone2ae97f2010-09-17 17:42:16 +000022 m_is_file (false)
23{
24}
25
26SBStream::~SBStream ()
27{
28}
29
30bool
31SBStream::IsValid() const
32{
33 return (m_opaque_ap.get() != NULL);
34}
35
36// If this stream is not redirected to a file, it will maintain a local
37// cache for the stream data which can be accessed using this accessor.
38const char *
39SBStream::GetData ()
40{
41 if (m_is_file || m_opaque_ap.get() == NULL)
42 return NULL;
43
44 return static_cast<StreamString *>(m_opaque_ap.get())->GetData();
45}
46
47// If this stream is not redirected to a file, it will maintain a local
48// cache for the stream output whose length can be accessed using this
49// accessor.
50size_t
51SBStream::GetSize()
52{
53 if (m_is_file || m_opaque_ap.get() == NULL)
Bill Wendling60fd91f2012-04-03 04:14:31 +000054 return 0;
Greg Claytone2ae97f2010-09-17 17:42:16 +000055
56 return static_cast<StreamString *>(m_opaque_ap.get())->GetSize();
57}
58
59void
60SBStream::Printf (const char *format, ...)
61{
Johnny Chen4f8189b2011-12-20 00:41:28 +000062 if (!format)
63 return;
Greg Claytone2ae97f2010-09-17 17:42:16 +000064 va_list args;
65 va_start (args, format);
66 ref().PrintfVarArg (format, args);
67 va_end (args);
68}
69
70void
71SBStream::RedirectToFile (const char *path, bool append)
72{
73 std::string local_data;
74 if (m_opaque_ap.get())
75 {
76 // See if we have any locally backed data. If so, copy it so we can then
77 // redirect it to the file so we don't lose the data
78 if (!m_is_file)
79 local_data.swap(static_cast<StreamString *>(m_opaque_ap.get())->GetString());
80 }
Greg Clayton51b1e2d2011-02-09 01:08:52 +000081 StreamFile *stream_file = new StreamFile;
82 uint32_t open_options = File::eOpenOptionWrite | File::eOpenOptionCanCreate;
83 if (append)
84 open_options |= File::eOpenOptionAppend;
Greg Clayton06357c92014-07-30 17:38:47 +000085 else
86 open_options |= File::eOpenOptionTruncate;
Greg Claytonfbb76342013-11-20 21:07:01 +000087 stream_file->GetFile().Open (path, open_options, lldb::eFilePermissionsFileDefault);
Greg Clayton51b1e2d2011-02-09 01:08:52 +000088
89 m_opaque_ap.reset (stream_file);
Greg Claytone2ae97f2010-09-17 17:42:16 +000090
91 if (m_opaque_ap.get())
92 {
93 m_is_file = true;
94
95 // If we had any data locally in our StreamString, then pass that along to
96 // the to new file we are redirecting to.
97 if (!local_data.empty())
98 m_opaque_ap->Write (&local_data[0], local_data.size());
99 }
100 else
101 m_is_file = false;
102}
103
104void
105SBStream::RedirectToFileHandle (FILE *fh, bool transfer_fh_ownership)
106{
107 std::string local_data;
108 if (m_opaque_ap.get())
109 {
110 // See if we have any locally backed data. If so, copy it so we can then
111 // redirect it to the file so we don't lose the data
112 if (!m_is_file)
113 local_data.swap(static_cast<StreamString *>(m_opaque_ap.get())->GetString());
114 }
115 m_opaque_ap.reset (new StreamFile (fh, transfer_fh_ownership));
116
117 if (m_opaque_ap.get())
118 {
119 m_is_file = true;
120
121 // If we had any data locally in our StreamString, then pass that along to
122 // the to new file we are redirecting to.
123 if (!local_data.empty())
124 m_opaque_ap->Write (&local_data[0], local_data.size());
125 }
126 else
127 m_is_file = false;
128}
129
130void
131SBStream::RedirectToFileDescriptor (int fd, bool transfer_fh_ownership)
132{
133 std::string local_data;
134 if (m_opaque_ap.get())
135 {
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
138 if (!m_is_file)
139 local_data.swap(static_cast<StreamString *>(m_opaque_ap.get())->GetString());
140 }
141
142 m_opaque_ap.reset (new StreamFile (::fdopen (fd, "w"), transfer_fh_ownership));
143 if (m_opaque_ap.get())
144 {
145 m_is_file = true;
146
147 // If we had any data locally in our StreamString, then pass that along to
148 // the to new file we are redirecting to.
149 if (!local_data.empty())
150 m_opaque_ap->Write (&local_data[0], local_data.size());
151 }
152 else
153 m_is_file = false;
154
155}
156
157lldb_private::Stream *
158SBStream::operator->()
159{
160 return m_opaque_ap.get();
161}
162
163lldb_private::Stream *
164SBStream::get()
165{
166 return m_opaque_ap.get();
167}
168
169lldb_private::Stream &
170SBStream::ref()
171{
172 if (m_opaque_ap.get() == NULL)
173 m_opaque_ap.reset (new StreamString());
174 return *m_opaque_ap.get();
175}
176
177void
178SBStream::Clear ()
179{
180 if (m_opaque_ap.get())
181 {
182 // See if we have any locally backed data. If so, copy it so we can then
183 // redirect it to the file so we don't lose the data
184 if (m_is_file)
185 m_opaque_ap.reset();
186 else
187 static_cast<StreamString *>(m_opaque_ap.get())->GetString().clear();
188 }
189}