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

JSONLogger: Log to a file descriptor instead of another Logger

Logging to another Logger was kind of nonsensical - it was really just
an easy way to get it to write its output to stderr, but that only
works if the underlying logger writes to stderr.

This change is needed to make it easy to log JSON output somewhere
else (like a file or socket).
This commit is contained in:
Eelco Dolstra 2025-02-17 14:59:07 +01:00
parent ca2e52690d
commit bc66a9bbcf
5 changed files with 10 additions and 9 deletions

View file

@ -167,9 +167,9 @@ void to_json(nlohmann::json & json, std::shared_ptr<Pos> pos)
}
struct JSONLogger : Logger {
Logger & prevLogger;
Descriptor fd;
JSONLogger(Logger & prevLogger) : prevLogger(prevLogger) { }
JSONLogger(Descriptor fd) : fd(fd) { }
bool isVerbose() override {
return true;
@ -190,7 +190,7 @@ struct JSONLogger : Logger {
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
@ -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)