Skip to content

Commit 03d4bfd

Browse files
committed
Push log source description out of libutil and report build hook @nix warning correctly
1 parent 1485937 commit 03d4bfd

3 files changed

Lines changed: 26 additions & 12 deletions

File tree

src/libstore/build/derivation-goal.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ HookReply DerivationGoal::tryBuildHook()
11611161
throw;
11621162
}
11631163
}();
1164-
if (handleJSONLogMessage(s, worker.act, worker.hook->activities, true))
1164+
if (handleJSONLogMessage(s, worker.act, worker.hook->activities, "the build hook", true))
11651165
;
11661166
else if (s.substr(0, 2) == "# ") {
11671167
reply = s.substr(2);
@@ -1346,9 +1346,9 @@ void DerivationGoal::handleChildOutput(Descriptor fd, std::string_view data)
13461346
if (hook && fd == hook->fromHook.readSide.get()) {
13471347
for (auto c : data)
13481348
if (c == '\n') {
1349-
auto json = parseJSONMessage(currentHookLine);
1349+
auto json = parseJSONMessage(currentHookLine, "the derivation builder");
13501350
if (json) {
1351-
auto s = handleJSONLogMessage(*json, worker.act, hook->activities, true);
1351+
auto s = handleJSONLogMessage(*json, worker.act, hook->activities, "the derivation builder", true);
13521352
// ensure that logs from a builder using `ssh-ng://` as protocol
13531353
// are also available to `nix log`.
13541354
if (s && !isWrittenToLog && logSink) {
@@ -1390,7 +1390,7 @@ void DerivationGoal::handleEOF(Descriptor fd)
13901390

13911391
void DerivationGoal::flushLine()
13921392
{
1393-
if (handleJSONLogMessage(currentLogLine, *act, builderActivities, false))
1393+
if (handleJSONLogMessage(currentLogLine, *act, builderActivities, "the derivation builder", false))
13941394
;
13951395

13961396
else {

src/libutil/logging.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,20 +280,22 @@ static Logger::Fields getFields(nlohmann::json & json)
280280
return fields;
281281
}
282282

283-
std::optional<nlohmann::json> parseJSONMessage(const std::string & msg)
283+
std::optional<nlohmann::json> parseJSONMessage(const std::string & msg, std::string_view source)
284284
{
285285
if (!hasPrefix(msg, "@nix ")) return std::nullopt;
286286
try {
287287
return nlohmann::json::parse(std::string(msg, 5));
288288
} catch (std::exception & e) {
289-
printError("bad JSON log message from builder: %s", e.what());
289+
printError("bad JSON log message from %s: %s",
290+
Uncolored(source),
291+
e.what());
290292
}
291293
return std::nullopt;
292294
}
293295

294296
bool handleJSONLogMessage(nlohmann::json & json,
295297
const Activity & act, std::map<ActivityId, Activity> & activities,
296-
bool trusted)
298+
std::string_view source, bool trusted)
297299
{
298300
try {
299301
std::string action = json["action"];
@@ -329,20 +331,21 @@ bool handleJSONLogMessage(nlohmann::json & json,
329331
return true;
330332
} catch (const nlohmann::json::exception &e) {
331333
warn(
332-
"warning: Unable to handle a JSON message from the builder: %s",
334+
"warning: Unable to handle a JSON message from %s: %s",
335+
Uncolored(source),
333336
e.what()
334337
);
335338
return false;
336339
}
337340
}
338341

339342
bool handleJSONLogMessage(const std::string & msg,
340-
const Activity & act, std::map<ActivityId, Activity> & activities, bool trusted)
343+
const Activity & act, std::map<ActivityId, Activity> & activities, std::string_view source, bool trusted)
341344
{
342-
auto json = parseJSONMessage(msg);
345+
auto json = parseJSONMessage(msg, source);
343346
if (!json) return false;
344347

345-
return handleJSONLogMessage(*json, act, activities, trusted);
348+
return handleJSONLogMessage(*json, act, activities, source, trusted);
346349
}
347350

348351
Activity::~Activity()

src/libutil/logging.hh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,25 @@ Logger * makeSimpleLogger(bool printBuildLogs = true);
185185

186186
Logger * makeJSONLogger(Logger & prevLogger);
187187

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

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

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

198209
/**

0 commit comments

Comments
 (0)