Make abseil logging sink thread-safe

This commit is contained in:
Carles Fernandez
2026-09-20 09:41:31 +02:00
parent 6dc1eb2a90
commit 8e512b82fd
5 changed files with 15 additions and 0 deletions
+3
View File
@@ -40,6 +40,7 @@
#include <fstream> // for ofstream
#include <iostream> // for operator<<
#include <memory> // for unique_ptr
#include <mutex> // for lock_guard, mutex
#include <ostream> // for std::flush
#include <string> // for string
@@ -81,10 +82,12 @@ public:
}
void Send(const absl::LogEntry& entry) override
{
std::lock_guard<std::mutex> lock(logfile_mutex);
logfile << entry.text_message_with_prefix_and_newline() << std::flush;
}
private:
std::mutex logfile_mutex;
std::ofstream logfile;
std::string filename;
};
+3
View File
@@ -24,6 +24,7 @@
#include <fstream>
#include <iostream>
#include <memory>
#include <mutex>
#include <ostream>
#include <string>
@@ -61,10 +62,12 @@ public:
}
void Send(const absl::LogEntry& entry) override
{
std::lock_guard<std::mutex> lock(logfile_mutex);
logfile << entry.text_message_with_prefix_and_newline() << std::flush;
}
private:
std::mutex logfile_mutex;
std::ofstream logfile;
};
+3
View File
@@ -47,6 +47,7 @@
#include <fstream>
#include <iomanip>
#include <memory>
#include <mutex>
#include <numeric>
#include <ostream>
#include <string>
@@ -89,10 +90,12 @@ public:
}
void Send(const absl::LogEntry& entry) override
{
std::lock_guard<std::mutex> lock(logfile_mutex);
logfile << entry.text_message_with_prefix_and_newline() << std::flush;
}
private:
std::mutex logfile_mutex;
std::ofstream logfile;
};
#endif
+3
View File
@@ -22,6 +22,7 @@
#include <fstream>
#include <iostream>
#include <memory>
#include <mutex>
#include <ostream>
#include <string>
@@ -63,10 +64,12 @@ public:
}
void Send(const absl::LogEntry& entry) override
{
std::lock_guard<std::mutex> lock(logfile_mutex);
logfile << entry.text_message_with_prefix_and_newline() << std::flush;
}
private:
std::mutex logfile_mutex;
std::ofstream logfile;
std::string filename;
};
+3
View File
@@ -63,6 +63,7 @@
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <stdexcept> // for logic_error
#include <string>
#include <thread>
@@ -122,10 +123,12 @@ public:
}
void Send(const absl::LogEntry& entry) override
{
std::lock_guard<std::mutex> lock(logfile_mutex);
logfile << entry.text_message_with_prefix_and_newline() << std::flush;
}
private:
std::mutex logfile_mutex;
std::ofstream logfile;
};