From 2a2af3f72f1841a67d06120d0be5553fddda71d7 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 13 Mar 2025 18:23:00 +0100 Subject: [PATCH] Logger::result(): Support logging arbitrary JSON objects --- src/libstore/unix/build/local-derivation-goal.cc | 8 ++++---- src/libutil/logging.cc | 10 ++++++++++ src/libutil/logging.hh | 7 +++++++ src/libutil/tee-logger.cc | 6 ++++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/libstore/unix/build/local-derivation-goal.cc b/src/libstore/unix/build/local-derivation-goal.cc index 9ab0da32b..ec06c2044 100644 --- a/src/libstore/unix/build/local-derivation-goal.cc +++ b/src/libstore/unix/build/local-derivation-goal.cc @@ -2656,11 +2656,11 @@ SingleDrvOutputs LocalDerivationGoal::registerOutputs() worker.store.printStorePath(drvPath), wanted.to_string(HashFormat::SRI, true), got.to_string(HashFormat::SRI, true))); - // FIXME: put this in BuildResult and log that as JSON. act->result(resHashMismatch, - {worker.store.printStorePath(drvPath), - wanted.to_string(HashFormat::SRI, true), - got.to_string(HashFormat::SRI, true) + { + {"storePath", worker.store.printStorePath(drvPath)}, + {"wanted", wanted.to_string(HashFormat::SRI, true)}, + {"got", got.to_string(HashFormat::SRI, true)}, }); } if (!newInfo0.references.empty()) { diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index 94683cca5..c7b859bd5 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -279,6 +279,16 @@ struct JSONLogger : Logger { addFields(json, fields); write(json); } + + void result(ActivityId act, ResultType type, const nlohmann::json & j) override + { + nlohmann::json json; + json["action"] = "result"; + json["id"] = act; + json["type"] = type; + json["payload"] = j; + write(json); + } }; Logger * makeJSONLogger(Descriptor fd, bool includeNixPrefix) diff --git a/src/libutil/logging.hh b/src/libutil/logging.hh index ef449d03e..9d655f735 100644 --- a/src/libutil/logging.hh +++ b/src/libutil/logging.hh @@ -108,6 +108,8 @@ public: virtual void result(ActivityId act, ResultType type, const Fields & fields) { }; + virtual void result(ActivityId act, ResultType type, const nlohmann::json & json) { }; + virtual void writeToStdout(std::string_view s); template @@ -160,6 +162,11 @@ struct Activity void setExpected(ActivityType type2, uint64_t expected) const { result(resSetExpected, type2, expected); } + void result(ResultType type, const nlohmann::json & json) const + { + logger.result(id, type, json); + } + template void result(ResultType type, const Args & ... args) const { diff --git a/src/libutil/tee-logger.cc b/src/libutil/tee-logger.cc index 7a5115ea7..c9873a53a 100644 --- a/src/libutil/tee-logger.cc +++ b/src/libutil/tee-logger.cc @@ -65,6 +65,12 @@ struct TeeLogger : Logger logger->result(act, type, fields); } + void result(ActivityId act, ResultType type, const nlohmann::json & json) override + { + for (auto & logger : loggers) + logger->result(act, type, json); + } + void writeToStdout(std::string_view s) override { for (auto & logger : loggers) {