1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-30 19:57:59 +02:00

Push log source description out of libutil and report build hook @nix warning correctly

This commit is contained in:
Robert Hensing 2024-11-21 16:03:21 +01:00
parent 1485937b89
commit 03d4bfd852
3 changed files with 26 additions and 12 deletions

View file

@ -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"];
@ -329,7 +331,8 @@ bool handleJSONLogMessage(nlohmann::json & json,
return true;
} catch (const 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;
@ -337,12 +340,12 @@ bool handleJSONLogMessage(nlohmann::json & json,
}
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()