1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-21 17:59: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;
else if (logFormatStr == "bar")
return LogFormat::bar;
else if (logFormatStr == "bar-with-logs")
return LogFormat::barWithLogs;
throw Error("option 'log-format' has an invalid value '%s'", logFormatStr);
}
@ -30,8 +28,6 @@ Logger * makeDefaultLogger() {
return makeJSONLogger(*makeSimpleLogger(true));
case LogFormat::bar:
return makeProgressBar();
case LogFormat::barWithLogs:
return makeProgressBar(true);
default:
abort();
}

View file

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

View file

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

View file

@ -4,10 +4,16 @@
namespace nix {
Logger * makeProgressBar(bool printBuildLogs = false);
void startProgressBar(bool printBuildLogs = false);
Logger * makeProgressBar();
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 "progress-bar.hh"
#include "tarfile.hh"
#include "loggers.hh"
#include <iostream>
@ -103,7 +104,7 @@ static int main_nix_prefetch_url(int argc, char * * argv)
Finally f([]() { stopProgressBar(); });
if (isatty(STDERR_FILENO))
startProgressBar();
setLogFormat(LogFormat::bar);
auto store = openStore();
auto state = std::make_unique<EvalState>(myArgs.searchPath, store);

View file

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