Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/libutil/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,23 @@ class SimpleLogger : public Logger

Verbosity verbosity = lvlInfo;

void writeToStderr(std::string_view s)
static void writeFullLogging(Descriptor fd, std::string_view s)
{
try {
writeFull(getStandardError(), s, false);
writeFull(fd, s, false);
} catch (SystemError & e) {
/* Ignore failing writes to stderr. We need to ignore write
errors to ensure that cleanup code that logs to stderr runs
to completion if the other side of stderr has been closed
unexpectedly. */
/* Ignore failing logging writes. We need to ignore write
errors to ensure that cleanup code that writes logs runs
to completion if the other side of the logging fd has
been closed unexpectedly. */
}
}

void writeToStderr(std::string_view s)
{
writeFullLogging(getStandardError(), s);
}

std::unique_ptr<Logger> makeSimpleLogger(bool printBuildLogs)
{
return std::make_unique<SimpleLogger>(printBuildLogs);
Expand Down Expand Up @@ -245,15 +250,15 @@ struct JSONLogger : Logger

void write(const nlohmann::json & json)
{
auto line =
(includeNixPrefix ? "@nix " : "") + json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace);
auto line = (includeNixPrefix ? "@nix " : "")
+ json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace) + "\n";

/* Acquire a lock to prevent log messages from clobbering each
other. */
try {
auto state(_state.lock());
if (state->enabled)
writeLine(fd, line);
writeFullLogging(fd, line);
} catch (...) {
bool enabled = false;
std::swap(_state.lock()->enabled, enabled);
Expand Down
Loading