From 29a9e638c1bf70eb5f57bf8c6b78de71293cdedf Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 13 Mar 2025 13:37:38 +0100 Subject: [PATCH] Remove "@nix" prefix from json-log-path output --- src/libutil/logging.cc | 22 ++++++++++++++-------- src/libutil/logging.hh | 4 ++-- src/nix/main.cc | 5 +++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index 0bffe40e3..fcbc61d5e 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -168,8 +168,12 @@ void to_json(nlohmann::json & json, std::shared_ptr pos) struct JSONLogger : Logger { Descriptor fd; + bool includeNixPrefix; - JSONLogger(Descriptor fd) : fd(fd) { } + JSONLogger(Descriptor fd, bool includeNixPrefix) + : fd(fd) + , includeNixPrefix(includeNixPrefix) + { } bool isVerbose() override { return true; @@ -190,7 +194,9 @@ struct JSONLogger : Logger { void write(const nlohmann::json & json) { - writeLine(fd, "@nix " + json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace)); + writeLine(fd, + (includeNixPrefix ? "@nix " : "") + + json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace)); } void log(Verbosity lvl, std::string_view s) override @@ -262,18 +268,18 @@ struct JSONLogger : Logger { } }; -Logger * makeJSONLogger(Descriptor fd) +Logger * makeJSONLogger(Descriptor fd, bool includeNixPrefix) { - return new JSONLogger(fd); + return new JSONLogger(fd, includeNixPrefix); } -Logger * makeJSONLogger(const std::filesystem::path & path) +Logger * makeJSONLogger(const std::filesystem::path & path, bool includeNixPrefix) { struct JSONFileLogger : JSONLogger { AutoCloseFD fd; - JSONFileLogger(AutoCloseFD && fd) - : JSONLogger(fd.get()) + JSONFileLogger(AutoCloseFD && fd, bool includeNixPrefix) + : JSONLogger(fd.get(), includeNixPrefix) , fd(std::move(fd)) { } }; @@ -282,7 +288,7 @@ Logger * makeJSONLogger(const std::filesystem::path & path) if (!fd) throw SysError("opening log file '%1%'", path); - return new JSONFileLogger(std::move(fd)); + return new JSONFileLogger(std::move(fd), includeNixPrefix); } static Logger::Fields getFields(nlohmann::json & json) diff --git a/src/libutil/logging.hh b/src/libutil/logging.hh index cadeafea4..ef449d03e 100644 --- a/src/libutil/logging.hh +++ b/src/libutil/logging.hh @@ -189,9 +189,9 @@ Logger * makeSimpleLogger(bool printBuildLogs = true); Logger * makeTeeLogger(std::vector loggers); -Logger * makeJSONLogger(Descriptor fd); +Logger * makeJSONLogger(Descriptor fd, bool includeNixPrefix = true); -Logger * makeJSONLogger(const std::filesystem::path & path); +Logger * makeJSONLogger(const std::filesystem::path & path, bool includeNixPrefix = true); /** * @param source A noun phrase describing the source of the message, e.g. "the builder". diff --git a/src/nix/main.cc b/src/nix/main.cc index 10a02fe3f..68137a216 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -354,7 +354,8 @@ struct ExtLoggerSettings : Config this, "", "json-log-path", R"( A path to which JSON records of Nix's log output will be - written, in the same format as `--log-format internal-json`. + written, in the same format as `--log-format internal-json` + (without the `@nix ` prefixes on each line). )"}; }; @@ -501,7 +502,7 @@ void mainWrapped(int argc, char * * argv) } if (!extLoggerSettings.jsonLogPath.get().empty()) { - logger = makeTeeLogger({logger, makeJSONLogger(std::filesystem::path(extLoggerSettings.jsonLogPath.get()))}); + logger = makeTeeLogger({logger, makeJSONLogger(std::filesystem::path(extLoggerSettings.jsonLogPath.get()), false)}); } if (args.helpRequested) {