1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-27 00:11:17 +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

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

View file

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

View file

@ -2225,7 +2225,7 @@ void LocalDerivationGoal::runChild()
/* Execute the program. This should not return. */
if (drv->isBuiltin()) {
try {
logger = makeJSONLogger(*logger);
logger = makeJSONLogger(STDERR_FILENO);
std::map<std::string, Path> 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 {
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)

View file

@ -3,6 +3,7 @@
#include "error.hh"
#include "config.hh"
#include "file-descriptor.hh"
#include <nlohmann/json_fwd.hpp>
@ -183,7 +184,7 @@ extern Logger * logger;
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".