From 8e512b82fdccc81c64d58862be8e5e809a5873dd Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Sun, 20 Sep 2026 09:41:31 +0200 Subject: [PATCH] Make abseil logging sink thread-safe --- src/main/main.cc | 3 +++ tests/single_test_main.cc | 3 +++ tests/system-tests/position_test.cc | 3 +++ tests/test_main.cc | 3 +++ utils/front-end-cal/main.cc | 3 +++ 5 files changed, 15 insertions(+) diff --git a/src/main/main.cc b/src/main/main.cc index 97c17c486..9b949b1d9 100644 --- a/src/main/main.cc +++ b/src/main/main.cc @@ -40,6 +40,7 @@ #include // for ofstream #include // for operator<< #include // for unique_ptr +#include // for lock_guard, mutex #include // for std::flush #include // for string @@ -81,10 +82,12 @@ public: } void Send(const absl::LogEntry& entry) override { + std::lock_guard lock(logfile_mutex); logfile << entry.text_message_with_prefix_and_newline() << std::flush; } private: + std::mutex logfile_mutex; std::ofstream logfile; std::string filename; }; diff --git a/tests/single_test_main.cc b/tests/single_test_main.cc index 9a340de1b..9537a1f09 100644 --- a/tests/single_test_main.cc +++ b/tests/single_test_main.cc @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -61,10 +62,12 @@ public: } void Send(const absl::LogEntry& entry) override { + std::lock_guard lock(logfile_mutex); logfile << entry.text_message_with_prefix_and_newline() << std::flush; } private: + std::mutex logfile_mutex; std::ofstream logfile; }; diff --git a/tests/system-tests/position_test.cc b/tests/system-tests/position_test.cc index 0d30c1e8a..df90734ad 100644 --- a/tests/system-tests/position_test.cc +++ b/tests/system-tests/position_test.cc @@ -47,6 +47,7 @@ #include #include #include +#include #include #include #include @@ -89,10 +90,12 @@ public: } void Send(const absl::LogEntry& entry) override { + std::lock_guard lock(logfile_mutex); logfile << entry.text_message_with_prefix_and_newline() << std::flush; } private: + std::mutex logfile_mutex; std::ofstream logfile; }; #endif diff --git a/tests/test_main.cc b/tests/test_main.cc index b1ef7ea70..15528adf5 100644 --- a/tests/test_main.cc +++ b/tests/test_main.cc @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -63,10 +64,12 @@ public: } void Send(const absl::LogEntry& entry) override { + std::lock_guard lock(logfile_mutex); logfile << entry.text_message_with_prefix_and_newline() << std::flush; } private: + std::mutex logfile_mutex; std::ofstream logfile; std::string filename; }; diff --git a/utils/front-end-cal/main.cc b/utils/front-end-cal/main.cc index 47bd1822f..6ff908fe1 100644 --- a/utils/front-end-cal/main.cc +++ b/utils/front-end-cal/main.cc @@ -63,6 +63,7 @@ #include #include #include +#include #include // for logic_error #include #include @@ -122,10 +123,12 @@ public: } void Send(const absl::LogEntry& entry) override { + std::lock_guard lock(logfile_mutex); logfile << entry.text_message_with_prefix_and_newline() << std::flush; } private: + std::mutex logfile_mutex; std::ofstream logfile; };