1 //===-- 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 12 #include "lldb/Core/Error.h" 13 #include "lldb/Core/Stream.h" 14 #include "lldb/Core/StreamFile.h" 15 #include "lldb/Core/StreamString.h" 16 17 using namespace lldb; 18 using namespace lldb_private; 19 20 SBStream::SBStream () : 21 m_opaque_ap (new StreamString()), 22 m_is_file (false) 23 { 24 } 25 26 SBStream::~SBStream () 27 { 28 } 29 30 bool 31 SBStream::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. 38 const char * 39 SBStream::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. 50 size_t 51 SBStream::GetSize() 52 { 53 if (m_is_file || m_opaque_ap.get() == NULL) 54 return 0; 55 56 return static_cast<StreamString *>(m_opaque_ap.get())->GetSize(); 57 } 58 59 void 60 SBStream::Printf (const char *format, ...) 61 { 62 if (!format) 63 return; 64 va_list args; 65 va_start (args, format); 66 ref().PrintfVarArg (format, args); 67 va_end (args); 68 } 69 70 void 71 SBStream::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 } 81 StreamFile *stream_file = new StreamFile; 82 uint32_t open_options = File::eOpenOptionWrite | File::eOpenOptionCanCreate; 83 if (append) 84 open_options |= File::eOpenOptionAppend; 85 else 86 open_options |= File::eOpenOptionTruncate; 87 stream_file->GetFile().Open (path, open_options, lldb::eFilePermissionsFileDefault); 88 89 m_opaque_ap.reset (stream_file); 90 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 104 void 105 SBStream::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 130 void 131 SBStream::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 157 lldb_private::Stream * 158 SBStream::operator->() 159 { 160 return m_opaque_ap.get(); 161 } 162 163 lldb_private::Stream * 164 SBStream::get() 165 { 166 return m_opaque_ap.get(); 167 } 168 169 lldb_private::Stream & 170 SBStream::ref() 171 { 172 if (m_opaque_ap.get() == NULL) 173 m_opaque_ap.reset (new StreamString()); 174 return *m_opaque_ap.get(); 175 } 176 177 void 178 SBStream::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 } 190