247class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
250 bool ShouldPrefixNextString;
251 bool ShouldEmitNewLineOnDestruction;
255 void write_impl(const char *Ptr, size_t Size) final {
256 auto Str = StringRef(Ptr, Size);
257 auto Eol = Str.find('\n');
258 // Handle `\n` occurring in the string, ensure to print the prefix at the
259 // beginning of each line.
260 while (Eol != StringRef::npos) {
261 // Take the line up to the newline (including the newline).
262 StringRef Line = Str.take_front(Eol + 1);
264 writeWithPrefix(Line);
265 // We printed a newline, record here to print a prefix.
266 ShouldPrefixNextString = true;
267 Str = Str.drop_front(Eol + 1);
268 Eol = Str.find('\n');
271 writeWithPrefix(Str);
273 void emitPrefix() { Os.write(Prefix.c_str(), Prefix.size()); }
274 void writeWithPrefix(StringRef Str) {
275 if (ShouldPrefixNextString) {
277 ShouldPrefixNextString = false;
279 Os.write(Str.data(), Str.size());
283 explicit raw_ldbg_ostream(std::string Prefix, raw_ostream &Os,
284 bool ShouldPrefixNextString = true,
285 bool ShouldEmitNewLineOnDestruction = false)
286 : Prefix(std::move(Prefix)), Os(Os),
287 ShouldPrefixNextString(ShouldPrefixNextString),
288 ShouldEmitNewLineOnDestruction(ShouldEmitNewLineOnDestruction) {
291 ~raw_ldbg_ostream() final {
292 if (ShouldEmitNewLineOnDestruction)
297 uint64_t current_pos() const final { return Os.tell(); }
301 raw_ldbg_ostream &asLvalue() { return *this; }
305class RAIINewLineStream final : public raw_ostream {
309 RAIINewLineStream(raw_ostream &Os) : Os(Os) { SetUnbuffered(); }
310 ~RAIINewLineStream() { Os << '\n'; }
311 void write_impl(const char *Ptr, size_t Size) final { Os.write(Ptr, Size); }
312 uint64_t current_pos() const final { return Os.tell(); }
313 RAIINewLineStream &asLvalue() { return *this; }
331computePrefix(StringRef DebugType, const char *File, int Line, int Level) {
333 raw_string_ostream OsPrefix(Prefix);
334 if (!DebugType.empty())
335 OsPrefix << "[" << DebugType << ":" << Level << "] ";
336 OsPrefix << File << ":" << Line << " ";
337 return OsPrefix.str();
341computePrefix(int Level, const char *File, int Line, StringRef DebugType) {
342 return computePrefix(DebugType, File, Line, Level);