1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-28 22:01:15 +02:00

Merge pull request #12483 from DeterminateSystems/json-logger

JSONLogger: Log to a file descriptor instead of another Logger
This commit is contained in:
Eelco Dolstra 2025-02-17 18:48:46 +01:00 committed by GitHub
commit db7577a660
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 10 additions and 9 deletions

View file

@ -51,7 +51,7 @@ static bool allSupportedLocally(Store & store, const std::set<std::string>& requ
static int main_build_remote(int argc, char * * argv) static int main_build_remote(int argc, char * * argv)
{ {
{ {
logger = makeJSONLogger(*logger); logger = makeJSONLogger(getStandardError());
/* Ensure we don't get any SSH passphrase or host key popups. */ /* Ensure we don't get any SSH passphrase or host key popups. */
unsetenv("DISPLAY"); unsetenv("DISPLAY");

View file

@ -27,7 +27,7 @@ Logger * makeDefaultLogger() {
case LogFormat::rawWithLogs: case LogFormat::rawWithLogs:
return makeSimpleLogger(true); return makeSimpleLogger(true);
case LogFormat::internalJSON: case LogFormat::internalJSON:
return makeJSONLogger(*makeSimpleLogger(true)); return makeJSONLogger(getStandardError());
case LogFormat::bar: case LogFormat::bar:
return makeProgressBar(); return makeProgressBar();
case LogFormat::barWithLogs: { case LogFormat::barWithLogs: {

View file

@ -2225,7 +2225,7 @@ void LocalDerivationGoal::runChild()
/* Execute the program. This should not return. */ /* Execute the program. This should not return. */
if (drv->isBuiltin()) { if (drv->isBuiltin()) {
try { try {
logger = makeJSONLogger(*logger); logger = makeJSONLogger(getStandardError());
std::map<std::string, Path> outputs; std::map<std::string, Path> outputs;
for (auto & e : drv->outputs) for (auto & e : drv->outputs)

View file

@ -167,9 +167,9 @@ void to_json(nlohmann::json & json, std::shared_ptr<Pos> pos)
} }
struct JSONLogger : Logger { struct JSONLogger : Logger {
Logger & prevLogger; Descriptor fd;
JSONLogger(Logger & prevLogger) : prevLogger(prevLogger) { } JSONLogger(Descriptor fd) : fd(fd) { }
bool isVerbose() override { bool isVerbose() override {
return true; return true;
@ -190,7 +190,7 @@ struct JSONLogger : Logger {
void write(const nlohmann::json & json) void write(const nlohmann::json & json)
{ {
prevLogger.log(lvlError, "@nix " + json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace)); writeLine(fd, "@nix " + json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace));
} }
void log(Verbosity lvl, std::string_view s) override void log(Verbosity lvl, std::string_view s) override
@ -262,9 +262,9 @@ struct JSONLogger : Logger {
} }
}; };
Logger * makeJSONLogger(Logger & prevLogger) Logger * makeJSONLogger(Descriptor fd)
{ {
return new JSONLogger(prevLogger); return new JSONLogger(fd);
} }
static Logger::Fields getFields(nlohmann::json & json) static Logger::Fields getFields(nlohmann::json & json)

View file

@ -3,6 +3,7 @@
#include "error.hh" #include "error.hh"
#include "config.hh" #include "config.hh"
#include "file-descriptor.hh"
#include <nlohmann/json_fwd.hpp> #include <nlohmann/json_fwd.hpp>
@ -183,7 +184,7 @@ extern Logger * logger;
Logger * makeSimpleLogger(bool printBuildLogs = true); Logger * makeSimpleLogger(bool printBuildLogs = true);
Logger * makeJSONLogger(Logger & prevLogger); Logger * makeJSONLogger(Descriptor fd);
/** /**
* @param source A noun phrase describing the source of the message, e.g. "the builder". * @param source A noun phrase describing the source of the message, e.g. "the builder".