aboutsummaryrefslogtreecommitdiff
path: root/src/libmain/loggers.cc
diff options
context:
space:
mode:
authorregnat <rg@regnat.ovh>2020-06-05 17:01:02 +0200
committerregnat <rg@regnat.ovh>2020-06-08 09:31:15 +0200
commit170e86dff5724264e0d3d25b9af1bd42df6aec74 (patch)
tree194379f0539e1ce0dd004c30630dee4606ca16ca /src/libmain/loggers.cc
parentd558fb98f6f8ce32e5c08a36d798441f1b941ba8 (diff)
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)
Diffstat (limited to 'src/libmain/loggers.cc')
-rw-r--r--src/libmain/loggers.cc47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/libmain/loggers.cc b/src/libmain/loggers.cc
new file mode 100644
index 000000000..d3d5b104b
--- /dev/null
+++ b/src/libmain/loggers.cc
@@ -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();
+}
+
+}