Skip to content

Commit

Permalink
JSONLogger: Log to a file descriptor instead of another Logger
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
edolstra committed Feb 17, 2025
1 parent ca2e526 commit bc66a9b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/build-remote/build-remote.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/libmain/loggers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/unix/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/libutil/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/libutil/logging.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "error.hh"
#include "config.hh"
#include "file-descriptor.hh"

#include <nlohmann/json_fwd.hpp>

Expand Down Expand Up @@ -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".
Expand Down

0 comments on commit bc66a9b

Please sign in to comment.