Unify the printing of the logs between bar-with-logs and raw

Make the printing of the build logs systematically go through the
logger, and replicate the behavior of `no-build-output` by having two
different loggers (one that prints the build logs and one that doesn't)
This commit is contained in:
regnat 2020-06-05 18:20:11 +02:00
parent 2c4de6af10
commit 4983401440
7 changed files with 46 additions and 20 deletions

View file

@ -8,6 +8,8 @@ LogFormat defaultLogFormat = LogFormat::raw;
LogFormat parseLogFormat(const string &logFormatStr) { LogFormat parseLogFormat(const string &logFormatStr) {
if (logFormatStr == "raw") if (logFormatStr == "raw")
return LogFormat::raw; return LogFormat::raw;
else if (logFormatStr == "raw-with-logs")
return LogFormat::rawWithLogs;
else if (logFormatStr == "internal-json") else if (logFormatStr == "internal-json")
return LogFormat::internalJson; return LogFormat::internalJson;
else if (logFormatStr == "bar") else if (logFormatStr == "bar")
@ -21,7 +23,9 @@ LogFormat parseLogFormat(const string &logFormatStr) {
Logger *makeDefaultLogger() { Logger *makeDefaultLogger() {
switch (defaultLogFormat) { switch (defaultLogFormat) {
case LogFormat::raw: case LogFormat::raw:
return makeSimpleLogger(); return makeSimpleLogger(false);
case LogFormat::rawWithLogs:
return makeSimpleLogger(true);
case LogFormat::internalJson: case LogFormat::internalJson:
return makeJSONLogger(*makeSimpleLogger()); return makeJSONLogger(*makeSimpleLogger());
case LogFormat::bar: case LogFormat::bar:

View file

@ -6,6 +6,7 @@ namespace nix {
enum class LogFormat { enum class LogFormat {
raw, raw,
rawWithLogs,
internalJson, internalJson,
bar, bar,
barWithLogs, barWithLogs,

View file

@ -2,6 +2,7 @@
#include "shared.hh" #include "shared.hh"
#include "store-api.hh" #include "store-api.hh"
#include "util.hh" #include "util.hh"
#include "loggers.hh"
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
@ -169,7 +170,7 @@ LegacyArgs::LegacyArgs(const std::string & programName,
.longName = "no-build-output", .longName = "no-build-output",
.shortName = 'Q', .shortName = 'Q',
.description = "do not show build output", .description = "do not show build output",
.handler = {&settings.verboseBuild, false}, .handler = {[&]() {setLogFormat(LogFormat::raw); }},
}); });
addFlag({ addFlag({

View file

@ -1642,7 +1642,7 @@ void DerivationGoal::buildDone()
worker.store.printStorePath(drvPath), worker.store.printStorePath(drvPath),
statusToString(status)); statusToString(status));
if (!settings.verboseBuild && !logTail.empty()) { if (!logger->isVerbose() && !logTail.empty()) {
msg += (format("; last %d log lines:") % logTail.size()).str(); msg += (format("; last %d log lines:") % logTail.size()).str();
for (auto & line : logTail) for (auto & line : logTail)
msg += "\n " + line; msg += "\n " + line;
@ -1691,11 +1691,7 @@ void DerivationGoal::buildDone()
} }
void flushLine() { void flushLine() {
if (settings.verboseBuild) { act.result(resPostBuildLogLine, currentLine);
printError("post-build-hook: " + currentLine);
} else {
act.result(resPostBuildLogLine, currentLine);
}
currentLine.clear(); currentLine.clear();
} }
@ -4155,13 +4151,8 @@ void DerivationGoal::flushLine()
; ;
else { else {
if (settings.verboseBuild && logTail.push_back(currentLogLine);
(settings.printRepeatedBuilds || curRound == 1)) if (logTail.size() > settings.logLines) logTail.pop_front();
printError(currentLogLine);
else {
logTail.push_back(currentLogLine);
if (logTail.size() > settings.logLines) logTail.pop_front();
}
act->result(resBuildLogLine, currentLogLine); act->result(resBuildLogLine, currentLogLine);
} }

View file

@ -18,7 +18,7 @@ void setCurActivity(const ActivityId activityId)
curActivity = activityId; curActivity = activityId;
} }
Logger * logger = makeSimpleLogger(); Logger * logger = makeSimpleLogger(true);
void Logger::warn(const std::string & msg) void Logger::warn(const std::string & msg)
{ {
@ -35,13 +35,19 @@ class SimpleLogger : public Logger
public: public:
bool systemd, tty; bool systemd, tty;
bool printBuildLogs;
SimpleLogger() SimpleLogger(bool printBuildLogs)
: printBuildLogs(printBuildLogs)
{ {
systemd = getEnv("IN_SYSTEMD") == "1"; systemd = getEnv("IN_SYSTEMD") == "1";
tty = isatty(STDERR_FILENO); tty = isatty(STDERR_FILENO);
} }
bool isVerbose() override {
return printBuildLogs;
}
void log(Verbosity lvl, const FormatOrString & fs) override void log(Verbosity lvl, const FormatOrString & fs) override
{ {
if (lvl > verbosity) return; if (lvl > verbosity) return;
@ -70,6 +76,18 @@ public:
if (lvl <= verbosity && !s.empty()) if (lvl <= verbosity && !s.empty())
log(lvl, s + "..."); 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; Verbosity verbosity = lvlInfo;
@ -94,9 +112,9 @@ void writeToStderr(const string & s)
} }
} }
Logger * makeSimpleLogger() Logger * makeSimpleLogger(bool printBuildLogs)
{ {
return new SimpleLogger(); return new SimpleLogger(printBuildLogs);
} }
std::atomic<uint64_t> nextId{(uint64_t) getpid() << 32}; std::atomic<uint64_t> nextId{(uint64_t) getpid() << 32};
@ -114,6 +132,10 @@ struct JSONLogger : Logger
JSONLogger(Logger & prevLogger) : prevLogger(prevLogger) { } JSONLogger(Logger & prevLogger) : prevLogger(prevLogger) { }
bool isVerbose() override {
return true;
}
void addFields(nlohmann::json & json, const Fields & fields) void addFields(nlohmann::json & json, const Fields & fields)
{ {
if (fields.empty()) return; if (fields.empty()) return;

View file

@ -65,6 +65,9 @@ public:
virtual void stop() { }; 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; virtual void log(Verbosity lvl, const FormatOrString & fs) = 0;
void log(const FormatOrString & fs) void log(const FormatOrString & fs)
@ -143,7 +146,7 @@ struct PushActivity
extern Logger * logger; extern Logger * logger;
Logger * makeSimpleLogger(); Logger * makeSimpleLogger(bool printBuildLogs = true);
Logger * makeJSONLogger(Logger & prevLogger); Logger * makeJSONLogger(Logger & prevLogger);

View file

@ -119,6 +119,10 @@ public:
quitCV.notify_one(); quitCV.notify_one();
} }
bool isVerbose() override {
return printBuildLogs;
}
void log(Verbosity lvl, const FormatOrString & fs) override void log(Verbosity lvl, const FormatOrString & fs) override
{ {
auto state(state_.lock()); auto state(state_.lock());