233class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
236 bool ShouldPrefixNextString;
237 bool ShouldEmitNewLineOnDestruction;
241 void write_impl(const char *Ptr, size_t Size) final {
242 auto Str = StringRef(Ptr, Size);
243 auto Eol = Str.find('\n');
244 // Handle `\n` occurring in the string, ensure to print the prefix at the
245 // beginning of each line.
246 while (Eol != StringRef::npos) {
247 // Take the line up to the newline (including the newline).
248 StringRef Line = Str.take_front(Eol + 1);
250 writeWithPrefix(Line);
251 // We printed a newline, record here to print a prefix.
252 ShouldPrefixNextString = true;
253 Str = Str.drop_front(Eol + 1);
254 Eol = Str.find('\n');
257 writeWithPrefix(Str);
259 void emitPrefix() { Os.write(Prefix.c_str(), Prefix.size()); }
260 void writeWithPrefix(StringRef Str) {
261 if (ShouldPrefixNextString) {
263 ShouldPrefixNextString = false;
265 Os.write(Str.data(), Str.size());
269 explicit raw_ldbg_ostream(std::string Prefix, raw_ostream &Os,
270 bool ShouldPrefixNextString = true,
271 bool ShouldEmitNewLineOnDestruction = false)
272 : Prefix(std::move(Prefix)), Os(Os),
273 ShouldPrefixNextString(ShouldPrefixNextString),
274 ShouldEmitNewLineOnDestruction(ShouldEmitNewLineOnDestruction) {
277 ~raw_ldbg_ostream() final {
278 if (ShouldEmitNewLineOnDestruction)
283 uint64_t current_pos() const final { return Os.tell(); }
287 raw_ldbg_ostream &asLvalue() { return *this; }
291class RAIINewLineStream final : public raw_ostream {
295 RAIINewLineStream(raw_ostream &Os) : Os(Os) { SetUnbuffered(); }
296 ~RAIINewLineStream() { Os << '\n'; }
297 void write_impl(const char *Ptr, size_t Size) final { Os.write(Ptr, Size); }
298 uint64_t current_pos() const final { return Os.tell(); }
299 RAIINewLineStream &asLvalue() { return *this; }
317computePrefix(StringRef DebugType, const char *File, int Line, int Level) {
319 raw_string_ostream OsPrefix(Prefix);
321 if (!DebugType.empty() && DebugType != File)
322 OsPrefix << DebugType << " ";
323 OsPrefix << File << ":" << Line << " " << Level << "] ";
324 return OsPrefix.str();
328computePrefix(int Level, const char *File, int Line, StringRef DebugType) {
329 return computePrefix(DebugType, File, Line, Level);