mirror of
https://github.com/NixOS/nix
synced 2025-06-29 06:21:14 +02:00
Merge pull request #3073 from tweag/machine-logs
Add an option to print the logs in a machine-readable format
This commit is contained in:
commit
ac4d43a31b
19 changed files with 187 additions and 31 deletions
|
@ -18,7 +18,7 @@ void setCurActivity(const ActivityId activityId)
|
|||
curActivity = activityId;
|
||||
}
|
||||
|
||||
Logger * logger = makeDefaultLogger();
|
||||
Logger * logger = makeSimpleLogger(true);
|
||||
|
||||
void Logger::warn(const std::string & msg)
|
||||
{
|
||||
|
@ -35,13 +35,19 @@ class SimpleLogger : public Logger
|
|||
public:
|
||||
|
||||
bool systemd, tty;
|
||||
bool printBuildLogs;
|
||||
|
||||
SimpleLogger()
|
||||
SimpleLogger(bool printBuildLogs)
|
||||
: printBuildLogs(printBuildLogs)
|
||||
{
|
||||
systemd = getEnv("IN_SYSTEMD") == "1";
|
||||
tty = isatty(STDERR_FILENO);
|
||||
}
|
||||
|
||||
bool isVerbose() override {
|
||||
return printBuildLogs;
|
||||
}
|
||||
|
||||
void log(Verbosity lvl, const FormatOrString & fs) override
|
||||
{
|
||||
if (lvl > verbosity) return;
|
||||
|
@ -70,6 +76,18 @@ public:
|
|||
if (lvl <= verbosity && !s.empty())
|
||||
log(lvl, s + "...");
|
||||
}
|
||||
|
||||
void result(ActivityId act, ResultType type, const Fields & fields) override
|
||||
{
|
||||
if (type == resBuildLogLine && printBuildLogs) {
|
||||
auto lastLine = fields[0].s;
|
||||
printError(lastLine);
|
||||
}
|
||||
else if (type == resPostBuildLogLine && printBuildLogs) {
|
||||
auto lastLine = fields[0].s;
|
||||
printError("post-build-hook: " + lastLine);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Verbosity verbosity = lvlInfo;
|
||||
|
@ -94,9 +112,9 @@ void writeToStderr(const string & s)
|
|||
}
|
||||
}
|
||||
|
||||
Logger * makeDefaultLogger()
|
||||
Logger * makeSimpleLogger(bool printBuildLogs)
|
||||
{
|
||||
return new SimpleLogger();
|
||||
return new SimpleLogger(printBuildLogs);
|
||||
}
|
||||
|
||||
std::atomic<uint64_t> nextId{(uint64_t) getpid() << 32};
|
||||
|
@ -114,6 +132,10 @@ struct JSONLogger : Logger
|
|||
|
||||
JSONLogger(Logger & prevLogger) : prevLogger(prevLogger) { }
|
||||
|
||||
bool isVerbose() override {
|
||||
return true;
|
||||
}
|
||||
|
||||
void addFields(nlohmann::json & json, const Fields & fields)
|
||||
{
|
||||
if (fields.empty()) return;
|
||||
|
|
|
@ -63,6 +63,11 @@ public:
|
|||
|
||||
virtual ~Logger() { }
|
||||
|
||||
virtual void stop() { };
|
||||
|
||||
// Whether the logger prints the whole build log
|
||||
virtual bool isVerbose() { return false; }
|
||||
|
||||
virtual void log(Verbosity lvl, const FormatOrString & fs) = 0;
|
||||
|
||||
void log(const FormatOrString & fs)
|
||||
|
@ -141,7 +146,7 @@ struct PushActivity
|
|||
|
||||
extern Logger * logger;
|
||||
|
||||
Logger * makeDefaultLogger();
|
||||
Logger * makeSimpleLogger(bool printBuildLogs = true);
|
||||
|
||||
Logger * makeJSONLogger(Logger & prevLogger);
|
||||
|
||||
|
|
|
@ -989,7 +989,7 @@ pid_t startProcess(std::function<void()> fun, const ProcessOptions & options)
|
|||
{
|
||||
auto wrapper = [&]() {
|
||||
if (!options.allowVfork)
|
||||
logger = makeDefaultLogger();
|
||||
logger = makeSimpleLogger();
|
||||
try {
|
||||
#if __linux__
|
||||
if (options.dieWithParent && prctl(PR_SET_PDEATHSIG, SIGKILL) == -1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue