Skip to content

Commit

Permalink
Push log source description out of libutil and report build hook @nix
Browse files Browse the repository at this point in the history
…warning correctly
  • Loading branch information
roberth committed Nov 21, 2024
1 parent 1469535 commit 8179d9d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/libstore/build/derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ HookReply DerivationGoal::tryBuildHook()
throw;
}
}();
if (handleJSONLogMessage(s, worker.act, worker.hook->activities, true))
if (handleJSONLogMessage(s, worker.act, worker.hook->activities, "the build hook", true))
;
else if (s.substr(0, 2) == "# ") {
reply = s.substr(2);
Expand Down Expand Up @@ -1346,9 +1346,9 @@ void DerivationGoal::handleChildOutput(Descriptor fd, std::string_view data)
if (hook && fd == hook->fromHook.readSide.get()) {
for (auto c : data)
if (c == '\n') {
auto json = parseJSONMessage(currentHookLine);
auto json = parseJSONMessage(currentHookLine, "the derivation builder");
if (json) {
auto s = handleJSONLogMessage(*json, worker.act, hook->activities, true);
auto s = handleJSONLogMessage(*json, worker.act, hook->activities, "the derivation builder", true);
// ensure that logs from a builder using `ssh-ng://` as protocol
// are also available to `nix log`.
if (s && !isWrittenToLog && logSink) {
Expand Down Expand Up @@ -1390,7 +1390,7 @@ void DerivationGoal::handleEOF(Descriptor fd)

void DerivationGoal::flushLine()
{
if (handleJSONLogMessage(currentLogLine, *act, builderActivities, false))
if (handleJSONLogMessage(currentLogLine, *act, builderActivities, "the derivation builder", false))
;

else {
Expand Down
17 changes: 10 additions & 7 deletions src/libutil/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -280,20 +280,22 @@ static Logger::Fields getFields(nlohmann::json & json)
return fields;
}

std::optional<nlohmann::json> parseJSONMessage(const std::string & msg)
std::optional<nlohmann::json> parseJSONMessage(const std::string & msg, std::string_view source)
{
if (!hasPrefix(msg, "@nix ")) return std::nullopt;
try {
return nlohmann::json::parse(std::string(msg, 5));
} catch (std::exception & e) {
printError("bad JSON log message from builder: %s", e.what());
printError("bad JSON log message from %s: %s",
Uncolored(source),
e.what());
}
return std::nullopt;
}

bool handleJSONLogMessage(nlohmann::json & json,
const Activity & act, std::map<ActivityId, Activity> & activities,
bool trusted)
std::string_view source, bool trusted)
{
try {
std::string action = json["action"];
Expand Down Expand Up @@ -329,20 +331,21 @@ bool handleJSONLogMessage(nlohmann::json & json,
return true;
} catch (nlohmann::json::exception &e) {
warn(
"warning: Unable to handle a JSON message from the builder: %s",
"warning: Unable to handle a JSON message from %s: %s",
Uncolored(source),
e.what()
);
return false;
}
}

bool handleJSONLogMessage(const std::string & msg,
const Activity & act, std::map<ActivityId, Activity> & activities, bool trusted)
const Activity & act, std::map<ActivityId, Activity> & activities, std::string_view source, bool trusted)
{
auto json = parseJSONMessage(msg);
auto json = parseJSONMessage(msg, source);
if (!json) return false;

return handleJSONLogMessage(*json, act, activities, trusted);
return handleJSONLogMessage(*json, act, activities, source, trusted);
}

Activity::~Activity()
Expand Down
13 changes: 12 additions & 1 deletion src/libutil/logging.hh
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,25 @@ Logger * makeSimpleLogger(bool printBuildLogs = true);

Logger * makeJSONLogger(Logger & prevLogger);

std::optional<nlohmann::json> parseJSONMessage(const std::string & msg);
/**
* @param source A noun phrase describing the source of the message, e.g. "the builder".
*/
std::optional<nlohmann::json> parseJSONMessage(const std::string & msg, std::string_view source);

/**
* @param source A noun phrase describing the source of the message, e.g. "the builder".
*/
bool handleJSONLogMessage(nlohmann::json & json,
const Activity & act, std::map<ActivityId, Activity> & activities,
std::string_view source,
bool trusted);

/**
* @param source A noun phrase describing the source of the message, e.g. "the builder".
*/
bool handleJSONLogMessage(const std::string & msg,
const Activity & act, std::map<ActivityId, Activity> & activities,
std::string_view source,
bool trusted);

/**
Expand Down

0 comments on commit 8179d9d

Please sign in to comment.