1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-21 09:49:36 +01:00

Replace LogFormat::barWithLogs with a setting

This will make it easier to add more settings to the progress bar.
This commit is contained in:
Eelco Dolstra 2020-11-18 13:04:11 +01:00
parent 99bb7aaf80
commit 4979bd468a
6 changed files with 25 additions and 28 deletions

View file

@ -15,8 +15,6 @@ LogFormat parseLogFormat(const std::string & logFormatStr) {
return LogFormat::internalJSON; return LogFormat::internalJSON;
else if (logFormatStr == "bar") else if (logFormatStr == "bar")
return LogFormat::bar; return LogFormat::bar;
else if (logFormatStr == "bar-with-logs")
return LogFormat::barWithLogs;
throw Error("option 'log-format' has an invalid value '%s'", logFormatStr); throw Error("option 'log-format' has an invalid value '%s'", logFormatStr);
} }
@ -30,8 +28,6 @@ Logger * makeDefaultLogger() {
return makeJSONLogger(*makeSimpleLogger(true)); return makeJSONLogger(*makeSimpleLogger(true));
case LogFormat::bar: case LogFormat::bar:
return makeProgressBar(); return makeProgressBar();
case LogFormat::barWithLogs:
return makeProgressBar(true);
default: default:
abort(); abort();
} }

View file

@ -9,7 +9,6 @@ enum class LogFormat {
rawWithLogs, rawWithLogs,
internalJSON, internalJSON,
bar, bar,
barWithLogs,
}; };
void setLogFormat(const std::string & logFormatStr); void setLogFormat(const std::string & logFormatStr);

View file

@ -15,6 +15,10 @@
namespace nix { namespace nix {
ProgressBarSettings progressBarSettings;
static GlobalConfig::Register rProgressBarSettings(&progressBarSettings);
static std::string getS(const std::vector<Logger::Field> & fields, size_t n) static std::string getS(const std::vector<Logger::Field> & fields, size_t n)
{ {
assert(n < fields.size()); assert(n < fields.size());
@ -133,8 +137,6 @@ private:
bool active = true; bool active = true;
bool haveUpdate = true; bool haveUpdate = true;
bool printBuildLogs;
std::map<LineId, std::string> statusLines; std::map<LineId, std::string> statusLines;
/* How many lines need to be erased when redrawing. */ /* How many lines need to be erased when redrawing. */
@ -158,9 +160,9 @@ private:
public: public:
ProgressBar(bool printBuildLogs, bool isTTY) ProgressBar(bool isTTY)
: isTTY(isTTY) : isTTY(isTTY)
, state_({ .active = isTTY, .printBuildLogs = printBuildLogs }) , state_({ .active = isTTY })
{ {
state_.lock()->active = isTTY; state_.lock()->active = isTTY;
@ -224,10 +226,10 @@ public:
} }
if (c == 'l') { if (c == 'l') {
auto state(state_.lock()); auto state(state_.lock());
state->printBuildLogs = !state->printBuildLogs; progressBarSettings.printBuildLogs = !progressBarSettings.printBuildLogs;
updateStatusLine(*state); updateStatusLine(*state);
draw(*state, draw(*state,
state->printBuildLogs progressBarSettings.printBuildLogs
? ANSI_BOLD "Enabling build logs." ? ANSI_BOLD "Enabling build logs."
: ANSI_BOLD "Disabling build logs."); : ANSI_BOLD "Disabling build logs.");
} }
@ -298,7 +300,7 @@ public:
bool isVerbose() override bool isVerbose() override
{ {
return state_.lock()->printBuildLogs; return progressBarSettings.printBuildLogs;
} }
void log(Verbosity lvl, const FormatOrString & fs) override void log(Verbosity lvl, const FormatOrString & fs) override
@ -470,7 +472,7 @@ public:
auto i = state->its.find(act); auto i = state->its.find(act);
assert(i != state->its.end()); assert(i != state->its.end());
i->second->lastLine = lastLine; i->second->lastLine = lastLine;
if (state->printBuildLogs) { if (progressBarSettings.printBuildLogs) {
auto suffix = "> "; auto suffix = "> ";
if (type == resPostBuildLogLine) { if (type == resPostBuildLogLine) {
suffix = " (post)> "; suffix = " (post)> ";
@ -720,10 +722,9 @@ public:
} }
}; };
Logger * makeProgressBar(bool printBuildLogs) Logger * makeProgressBar()
{ {
return new ProgressBar( return new ProgressBar(
printBuildLogs,
isatty(STDIN_FILENO) isatty(STDIN_FILENO)
&& isatty(STDOUT_FILENO) && isatty(STDOUT_FILENO)
&& isatty(STDERR_FILENO) && isatty(STDERR_FILENO)
@ -731,11 +732,6 @@ Logger * makeProgressBar(bool printBuildLogs)
); );
} }
void startProgressBar(bool printBuildLogs)
{
logger = makeProgressBar(printBuildLogs);
}
void stopProgressBar() void stopProgressBar()
{ {
auto progressBar = dynamic_cast<ProgressBar *>(logger); auto progressBar = dynamic_cast<ProgressBar *>(logger);

View file

@ -4,10 +4,16 @@
namespace nix { namespace nix {
Logger * makeProgressBar(bool printBuildLogs = false); Logger * makeProgressBar();
void startProgressBar(bool printBuildLogs = false);
void stopProgressBar(); void stopProgressBar();
struct ProgressBarSettings : Config
{
Setting<bool> printBuildLogs{this, false, "print-build-logs",
"Whether the progress bar should print full build logs or just the most recent line."};
};
extern ProgressBarSettings progressBarSettings;
} }

View file

@ -10,6 +10,7 @@
#include "../nix/legacy.hh" #include "../nix/legacy.hh"
#include "progress-bar.hh" #include "progress-bar.hh"
#include "tarfile.hh" #include "tarfile.hh"
#include "loggers.hh"
#include <iostream> #include <iostream>
@ -103,7 +104,7 @@ static int main_nix_prefetch_url(int argc, char * * argv)
Finally f([]() { stopProgressBar(); }); Finally f([]() { stopProgressBar(); });
if (isatty(STDERR_FILENO)) if (isatty(STDERR_FILENO))
startProgressBar(); setLogFormat(LogFormat::bar);
auto store = openStore(); auto store = openStore();
auto state = std::make_unique<EvalState>(myArgs.searchPath, store); auto state = std::make_unique<EvalState>(myArgs.searchPath, store);

View file

@ -10,6 +10,7 @@
#include "filetransfer.hh" #include "filetransfer.hh"
#include "finally.hh" #include "finally.hh"
#include "loggers.hh" #include "loggers.hh"
#include "progress-bar.hh"
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -55,7 +56,6 @@ std::string programPath;
struct NixArgs : virtual MultiCommand, virtual MixCommonArgs struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
{ {
bool printBuildLogs = false;
bool useNet = true; bool useNet = true;
bool refresh = false; bool refresh = false;
@ -88,12 +88,11 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
}}, }},
}); });
// FIXME: make this a setting, remove LogFormat::barWithLogs.
addFlag({ addFlag({
.longName = "print-build-logs", .longName = "print-build-logs",
.shortName = 'L', .shortName = 'L',
.description = "print full build logs on stderr", .description = "print full build logs on stderr",
.handler = {[&]() { printBuildLogs = true; }}, .handler = {[&]() { progressBarSettings.printBuildLogs = true; }},
}); });
addFlag({ addFlag({
@ -298,7 +297,7 @@ void mainWrapped(int argc, char * * argv)
if (completions) return; if (completions) return;
setLogFormat(args.printBuildLogs ? LogFormat::bar : LogFormat::barWithLogs); setLogFormat(LogFormat::bar);
Finally f([] { logger->stop(); }); Finally f([] { logger->stop(); });