Skip to content

Commit e9af7a0

Browse files
authored
Merge pull request #12647 from DeterminateSystems/lock-json-logger
JSONLogger: Acquire a lock to prevent log messages from clobbering each other
2 parents b8eaf1b + d0227f8 commit e9af7a0

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/libutil/logging.cc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "config-global.hh"
77
#include "source-path.hh"
88
#include "position.hh"
9+
#include "sync.hh"
910

1011
#include <atomic>
1112
#include <sstream>
@@ -201,9 +202,22 @@ struct JSONLogger : Logger {
201202
unreachable();
202203
}
203204

205+
struct State
206+
{
207+
};
208+
209+
Sync<State> _state;
210+
204211
void write(const nlohmann::json & json)
205212
{
206-
writeLine(fd, "@nix " + json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace));
213+
auto line =
214+
"@nix " +
215+
json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace);
216+
217+
/* Acquire a lock to prevent log messages from clobbering each
218+
other. */
219+
auto state(_state.lock());
220+
writeLine(fd, line);
207221
}
208222

209223
void log(Verbosity lvl, std::string_view s) override

0 commit comments

Comments
 (0)