235class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
238 bool ShouldPrefixNextString;
239 bool ShouldEmitNewLineOnDestruction;
243 void write_impl(const char *Ptr, size_t Size) final {
244 auto Str = StringRef(Ptr, Size);
245 auto Eol = Str.find('\n');
246 // Handle `\n` occurring in the string, ensure to print the prefix at the
247 // beginning of each line.
248 while (Eol != StringRef::npos) {
249 // Take the line up to the newline (including the newline).
250 StringRef Line = Str.take_front(Eol + 1);
252 writeWithPrefix(Line);
253 // We printed a newline, record here to print a prefix.
254 ShouldPrefixNextString = true;
255 Str = Str.drop_front(Eol + 1);
256 Eol = Str.find('\n');
259 writeWithPrefix(Str);
261 void emitPrefix() { Os.write(Prefix.c_str(), Prefix.size()); }
262 void writeWithPrefix(StringRef Str) {
263 if (ShouldPrefixNextString) {
265 ShouldPrefixNextString = false;
267 Os.write(Str.data(), Str.size());
271 explicit raw_ldbg_ostream(std::string Prefix, raw_ostream &Os,
272 bool ShouldPrefixNextString = true,
273 bool ShouldEmitNewLineOnDestruction = false)
274 : Prefix(std::move(Prefix)), Os(Os),
275 ShouldPrefixNextString(ShouldPrefixNextString),
276 ShouldEmitNewLineOnDestruction(ShouldEmitNewLineOnDestruction) {
279 ~raw_ldbg_ostream() final {
280 if (ShouldEmitNewLineOnDestruction)
285 uint64_t current_pos() const final { return Os.tell(); }
289 raw_ldbg_ostream &asLvalue() { return *this; }
293class RAIINewLineStream final : public raw_ostream {
297 RAIINewLineStream(raw_ostream &Os) : Os(Os) { SetUnbuffered(); }
298 ~RAIINewLineStream() { Os << '\n'; }
299 void write_impl(const char *Ptr, size_t Size) final { Os.write(Ptr, Size); }
300 uint64_t current_pos() const final { return Os.tell(); }
301 RAIINewLineStream &asLvalue() { return *this; }
319computePrefix(StringRef DebugType, const char *File, int Line, int Level) {
321 raw_string_ostream OsPrefix(Prefix);
323 if (!DebugType.empty() && DebugType != File)
324 OsPrefix << DebugType << " ";
325 OsPrefix << File << ":" << Line << " " << Level << "] ";
326 return OsPrefix.str();
330computePrefix(int Level, const char *File, int Line, StringRef DebugType) {
331 return computePrefix(DebugType, File, Line, Level);