-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[lldb-dap] Ensure logging statements are written as a single chunk. #131916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…hunk. 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.
|
@llvm/pr-subscribers-lldb Author: John Harrison (ashgti) ChangesI 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. Full diff: https://github.com/llvm/llvm-project/pull/131916.diff 1 Files Affected:
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)
|
JDevlieghere
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we just take a lock instead? I don't think this is all that much safer than what we had, depending on how buffering is implemented in std::ofstream.
|
You can't guarantee you are safe of print race conditions on all platforms just because you buffered the input to the stream writer and you have that manual flush. I'm pretty sure that now race conditions will be less frequent, but they can still happen. |
…t log statements.
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
…iteMessage call instead of having it in the macro.
JDevlieghere
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🥳
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/18/builds/13173 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/197/builds/2886 Here is the relevant piece of the build log for the reference |
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.