Skip to content

Commit 3566c7e

Browse files
committed
GH-49433: [C++] Buffer ARROW_LOG output to prevent thread interleaving
When using the ARROW_LOG macros from multiple threads, messages were getting mingled together in stderr because the operator<< was writing directly to the global stream piece-by-piece. This commit introduces an internal std::ostringstream buffer to CerrLog so that messages are accumulated locally and flushed atomically upon destruction.
1 parent 008e082 commit 3566c7e

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

cpp/src/arrow/util/logging.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#endif
2525
#include <cstdlib>
2626
#include <iostream>
27+
#include <sstream>
2728

2829
#ifdef ARROW_USE_GLOG
2930

@@ -67,7 +68,7 @@ class CerrLog {
6768

6869
virtual ~CerrLog() {
6970
if (has_logged_) {
70-
std::cerr << std::endl;
71+
std::cerr << buffer_.str() << std::endl;
7172
}
7273
if (severity_ == ArrowLogLevel::ARROW_FATAL) {
7374
PrintBackTrace();
@@ -77,21 +78,22 @@ class CerrLog {
7778

7879
std::ostream& Stream() {
7980
has_logged_ = true;
80-
return std::cerr;
81+
return buffer_;
8182
}
8283

8384
template <class T>
8485
CerrLog& operator<<(const T& t) {
8586
if (severity_ != ArrowLogLevel::ARROW_DEBUG) {
8687
has_logged_ = true;
87-
std::cerr << t;
88+
buffer_ << t;
8889
}
8990
return *this;
9091
}
9192

9293
protected:
9394
const ArrowLogLevel severity_;
9495
bool has_logged_;
96+
std::ostringstream buffer_;
9597

9698
void PrintBackTrace() {
9799
#ifdef ARROW_WITH_BACKTRACE

0 commit comments

Comments
 (0)