[Lldb-commits] [lldb] [lldb-dap] Ensure logging statements are written as a single chunk. (PR #131916)

John Harrison via lldb-commits lldb-commits at lists.llvm.org
Tue Mar 18 14:10:19 PDT 2025


https://github.com/ashgti created https://github.com/llvm/llvm-project/pull/131916

I noticed this while debugging some unit tests that the logs occasionally would intersperse two log statements.

Previously, it was possible for a log statement to have two messages interspersed since the timestamp and log statement were two different writes to the log stream.

Instead, combine the logging into a single buffer first before printing.

>From 6aa311afe9832bbd4a4118874e411243b05e490d Mon Sep 17 00:00:00 2001
From: John Harrison <harjohn at google.com>
Date: Tue, 18 Mar 2025 14:03:12 -0700
Subject: [PATCH] [lldb-dap] Ensure logging statements are written as a single
 atomic chunk.

Previously, it was possible for a log statement to have two messages interspersed since the timestamp and log statement were two different writes to the log stream.

Instead, combine the logging into a single buffer first before printing.
---
 lldb/tools/lldb-dap/DAPLog.h | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/lldb/tools/lldb-dap/DAPLog.h b/lldb/tools/lldb-dap/DAPLog.h
index 75a0a7d63a4f1..2574506739fea 100644
--- a/lldb/tools/lldb-dap/DAPLog.h
+++ b/lldb/tools/lldb-dap/DAPLog.h
@@ -23,8 +23,12 @@
     if (log_private) {                                                         \
       ::std::chrono::duration<double> now{                                     \
           ::std::chrono::system_clock::now().time_since_epoch()};              \
-      *log_private << ::llvm::formatv("{0:f9} ", now.count()).str()            \
-                   << ::llvm::formatv(__VA_ARGS__).str() << std::endl;         \
+      ::std::string out;                                                       \
+      ::llvm::raw_string_ostream os(out);                                      \
+      os << ::llvm::formatv("{0:f9} ", now.count()).str()                      \
+         << ::llvm::formatv(__VA_ARGS__).str() << "\n";                        \
+      *log_private << out;                                                     \
+      log_private->flush();                                                    \
     }                                                                          \
   } while (0)
 
@@ -37,10 +41,13 @@
     if (log_private && error_private) {                                        \
       ::std::chrono::duration<double> now{                                     \
           std::chrono::system_clock::now().time_since_epoch()};                \
-      *log_private << ::llvm::formatv("{0:f9} ", now.count()).str()            \
-                   << ::lldb_dap::FormatError(::std::move(error_private),      \
-                                              __VA_ARGS__)                     \
-                   << std::endl;                                               \
+      ::std::string out;                                                       \
+      ::llvm::raw_string_ostream os(out);                                      \
+      os << ::llvm::formatv("{0:f9} ", now.count()).str()                      \
+         << ::lldb_dap::FormatError(::std::move(error_private), __VA_ARGS__)   \
+         << "\n";                                                              \
+      *log_private << out;                                                     \
+      log_private->flush();                                                    \
     } else                                                                     \
       ::llvm::consumeError(::std::move(error_private));                        \
   } while (0)



More information about the lldb-commits mailing list