Skip to content

Commit 9261821

Browse files
authored
Add a mutex to the integration test log methods, to avoid a rare crash. (#258)
Because this is desktop only, C++11 is available and std::mutex can be used cross-platform.
1 parent 80af29d commit 9261821

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

testing/sample_framework/src/desktop/desktop_app_framework.cc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <algorithm>
2020
#include <cstring>
2121
#include <iostream>
22+
#include <mutex>
2223
#include <string>
2324
#include <thread> // NOLINT
2425
#include <vector>
@@ -95,7 +96,7 @@ std::string PathForResource() {
9596
return std::string();
9697
}
9798
void LogMessageV(bool suppress, const char* format, va_list list) {
98-
// Save the log to the g_full_logs list regardless of whether it should be
99+
// Save the log to the Full Logs list regardless of whether it should be
99100
// suppressed.
100101
static const int kLineBufferSize = 1024;
101102
char buffer[kLineBufferSize + 2];
@@ -122,20 +123,27 @@ void LogMessage(const char* format, ...) {
122123

123124
static bool g_save_full_log = false;
124125
static std::vector<std::string> g_full_logs; // NOLINT
126+
static std::mutex g_full_log_mutex;
125127

126-
void AddToFullLog(const char* str) { g_full_logs.push_back(std::string(str)); }
128+
void AddToFullLog(const char* str) {
129+
std::lock_guard<std::mutex> guard(g_full_log_mutex);
130+
g_full_logs.push_back(std::string(str)); }
127131

128132
bool GetPreserveFullLog() { return g_save_full_log; }
129133
void SetPreserveFullLog(bool b) { g_save_full_log = b; }
130134

131-
void ClearFullLog() { g_full_logs.clear(); }
135+
void ClearFullLog() {
136+
std::lock_guard<std::mutex> guard(g_full_log_mutex);
137+
g_full_logs.clear();
138+
}
132139

133140
void OutputFullLog() {
141+
std::lock_guard<std::mutex> guard(g_full_log_mutex);
134142
for (int i = 0; i < g_full_logs.size(); ++i) {
135143
fputs(g_full_logs[i].c_str(), stdout);
136144
}
137145
fflush(stdout);
138-
ClearFullLog();
146+
g_full_logs.clear();
139147
}
140148

141149
WindowContext GetWindowContext() { return nullptr; }

0 commit comments

Comments
 (0)