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

View file

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

View file

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

View file

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

View file

@ -18,7 +18,7 @@ void setCurActivity(const ActivityId activityId)
curActivity = activityId;
}
Logger * logger = makeSimpleLogger();
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 * makeSimpleLogger()
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;

View file

@ -65,6 +65,9 @@ public:
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)
@ -143,7 +146,7 @@ struct PushActivity
extern Logger * logger;
Logger * makeSimpleLogger();
Logger * makeSimpleLogger(bool printBuildLogs = true);
Logger * makeJSONLogger(Logger & prevLogger);

View file

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