mirror of
https://github.com/NixOS/nix.git
synced 2025-11-16 07:22:43 +01:00
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:
parent
d558fb98f6
commit
170e86dff5
12 changed files with 107 additions and 13 deletions
|
|
@ -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
47
src/libmain/loggers.cc
Normal 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
19
src/libmain/loggers.hh
Normal 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();
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue