Make the logger customisable

Add a new `--log-format` cli argument to change the format of the logs.
The possible values are
- raw (the default one for old-style commands)
- bar (the default one for new-style commands)
- bar-with-logs (equivalent to `--print-build-logs`)
- internal-json (the internal machine-readable json format)
This commit is contained in:
regnat 2020-06-05 17:01:02 +02:00
parent d558fb98f6
commit 170e86dff5
12 changed files with 107 additions and 13 deletions

View file

@ -1,5 +1,6 @@
#include "common-args.hh"
#include "globals.hh"
#include "loggers.hh"
namespace nix {
@ -38,6 +39,14 @@ MixCommonArgs::MixCommonArgs(const string & programName)
}},
});
addFlag({
.longName = "log-format",
.description = "Format of the logs. One of \"raw\", \"internal-json\", \"bar\" "
"or \"bar-with-logs\".",
.labels = {"format"},
.handler = {[](std::string format) { setLogFormat(format); }},
});
addFlag({
.longName = "max-jobs",
.shortName = 'j',

47
src/libmain/loggers.cc Normal file
View file

@ -0,0 +1,47 @@
#include "loggers.hh"
#include "../nix/progress-bar.hh"
namespace nix {
LogFormat defaultLogFormat = LogFormat::raw;
LogFormat parseLogFormat(const string &logFormatStr) {
if (logFormatStr == "raw")
return LogFormat::raw;
else if (logFormatStr == "internal-json")
return LogFormat::internalJson;
else if (logFormatStr == "bar")
return LogFormat::bar;
else if (logFormatStr == "bar-with-logs")
return LogFormat::barWithLogs;
throw Error(format("option 'log-format' has an invalid value '%s'") %
logFormatStr);
}
Logger *makeDefaultLogger() {
switch (defaultLogFormat) {
case LogFormat::raw:
return makeSimpleLogger();
case LogFormat::internalJson:
return makeJSONLogger(*makeSimpleLogger());
case LogFormat::bar:
return makeProgressBar();
case LogFormat::barWithLogs:
return makeProgressBar(true);
}
}
void setLogFormat(const string &logFormatStr) {
setLogFormat(parseLogFormat(logFormatStr));
}
void setLogFormat(const LogFormat &logFormat) {
defaultLogFormat = logFormat;
createDefaultLogger();
}
void createDefaultLogger() {
logger = makeDefaultLogger();
}
}

19
src/libmain/loggers.hh Normal file
View file

@ -0,0 +1,19 @@
#pragma once
#include "types.hh"
namespace nix {
enum class LogFormat {
raw,
internalJson,
bar,
barWithLogs,
};
void setLogFormat(const string &logFormatStr);
void setLogFormat(const LogFormat &logFormat);
void createDefaultLogger();
}

View file

@ -18,7 +18,7 @@ void setCurActivity(const ActivityId activityId)
curActivity = activityId;
}
Logger * logger = makeDefaultLogger();
Logger * logger = makeSimpleLogger();
void Logger::warn(const std::string & msg)
{
@ -94,7 +94,7 @@ void writeToStderr(const string & s)
}
}
Logger * makeDefaultLogger()
Logger * makeSimpleLogger()
{
return new SimpleLogger();
}

View file

@ -63,6 +63,8 @@ public:
virtual ~Logger() { }
virtual void stop() { };
virtual void log(Verbosity lvl, const FormatOrString & fs) = 0;
void log(const FormatOrString & fs)
@ -141,7 +143,7 @@ struct PushActivity
extern Logger * logger;
Logger * makeDefaultLogger();
Logger * makeSimpleLogger();
Logger * makeJSONLogger(Logger & prevLogger);

View file

@ -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)

View file

@ -472,6 +472,8 @@ static void _main(int argc, char * * argv)
restoreSignals();
logger->stop();
execvp(shell->c_str(), argPtrs.data());
throw SysError("executing shell '%s'", *shell);
@ -521,6 +523,8 @@ static void _main(int argc, char * * argv)
if (auto store2 = store.dynamic_pointer_cast<LocalFSStore>())
store2->addPermRoot(store->parseStorePath(symlink.second), absPath(symlink.first), true);
logger->stop();
for (auto & path : outPaths)
std::cout << path << '\n';
}

View file

@ -1446,6 +1446,8 @@ static int _main(int argc, char * * argv)
globals.state->printStats();
logger->stop();
return 0;
}
}

View file

@ -1098,6 +1098,8 @@ static int _main(int argc, char * * argv)
op(opFlags, opArgs);
logger->stop();
return 0;
}
}

View file

@ -10,6 +10,7 @@
#include "progress-bar.hh"
#include "filetransfer.hh"
#include "finally.hh"
#include "loggers.hh"
#include <sys/types.h>
#include <sys/socket.h>
@ -90,7 +91,7 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
.longName = "print-build-logs",
.shortName = 'L',
.description = "print full build logs on stderr",
.handler = {&printBuildLogs, true},
.handler = {[&]() {setLogFormat(LogFormat::barWithLogs); }},
});
addFlag({
@ -165,6 +166,10 @@ void mainWrapped(int argc, char * * argv)
verbosity = lvlWarn;
settings.verboseBuild = false;
setLogFormat("bar");
Finally f([] { logger->stop(); });
NixArgs args;
args.parseCmdline(argvToStrings(argc, argv));
@ -178,10 +183,6 @@ void mainWrapped(int argc, char * * argv)
&& args.command->first != "upgrade-nix")
settings.requireExperimentalFeature("nix-command");
Finally f([]() { stopProgressBar(); });
startProgressBar(args.printBuildLogs);
if (args.useNet && !haveInternet()) {
warn("you don't have Internet access; disabling some network-dependent features");
args.useNet = false;

View file

@ -106,7 +106,7 @@ public:
updateThread.join();
}
void stop()
void stop() override
{
auto state(state_.lock());
if (!state->active) return;
@ -457,11 +457,17 @@ public:
}
};
Logger *makeProgressBar(bool printBuildLogs)
{
return new ProgressBar(
printBuildLogs,
isatty(STDERR_FILENO) && getEnv("TERM").value_or("dumb") != "dumb"
);
}
void startProgressBar(bool printBuildLogs)
{
logger = new ProgressBar(
printBuildLogs,
isatty(STDERR_FILENO) && getEnv("TERM").value_or("dumb") != "dumb");
logger = makeProgressBar(printBuildLogs);
}
void stopProgressBar()

View file

@ -4,6 +4,8 @@
namespace nix {
Logger* makeProgressBar(bool printBuildLogs = false);
void startProgressBar(bool printBuildLogs = false);
void stopProgressBar();