From 21d72035370ec68622ec05c4a88f5de854ab47f8 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Sat, 3 Feb 2024 16:17:19 -0800 Subject: [PATCH] Merge `libmain/loggers` into `libutil/logging` --- src/libmain/common-args.cc | 1 - src/libmain/loggers.cc | 56 ---------------------- src/libmain/loggers.hh | 21 --------- src/libmain/shared.cc | 1 - src/libutil/local.mk | 11 ++++- src/libutil/logging.cc | 97 ++++++++++++++++++++++++++++++++++++++ src/libutil/logging.hh | 37 +++++++++++++++ src/nix/main.cc | 1 - 8 files changed, 144 insertions(+), 81 deletions(-) delete mode 100644 src/libmain/loggers.cc delete mode 100644 src/libmain/loggers.hh diff --git a/src/libmain/common-args.cc b/src/libmain/common-args.cc index 5b49aaabcef..db6a5de3860 100644 --- a/src/libmain/common-args.cc +++ b/src/libmain/common-args.cc @@ -2,7 +2,6 @@ #include "args/root.hh" #include "globals.hh" #include "logging.hh" -#include "loggers.hh" #include "util.hh" namespace nix { diff --git a/src/libmain/loggers.cc b/src/libmain/loggers.cc deleted file mode 100644 index 9829859de32..00000000000 --- a/src/libmain/loggers.cc +++ /dev/null @@ -1,56 +0,0 @@ -#include "loggers.hh" -#include "environment-variables.hh" -#include "progress-bar.hh" - -namespace nix { - -LogFormat defaultLogFormat = LogFormat::raw; - -LogFormat parseLogFormat(const std::string & logFormatStr) { - if (logFormatStr == "raw" || getEnv("NIX_GET_COMPLETIONS")) - return LogFormat::raw; - else if (logFormatStr == "raw-with-logs") - return LogFormat::rawWithLogs; - 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("option 'log-format' has an invalid value '%s'", logFormatStr); -} - -Logger * makeDefaultLogger() { - switch (defaultLogFormat) { - case LogFormat::raw: - return makeSimpleLogger(false); - case LogFormat::rawWithLogs: - return makeSimpleLogger(true); - case LogFormat::internalJSON: - return makeJSONLogger(*makeSimpleLogger(true)); - case LogFormat::bar: - return makeProgressBar(); - case LogFormat::barWithLogs: { - auto logger = makeProgressBar(); - logger->setPrintBuildLogs(true); - return logger; - } - default: - abort(); - } -} - -void setLogFormat(const std::string & logFormatStr) { - setLogFormat(parseLogFormat(logFormatStr)); -} - -void setLogFormat(const LogFormat & logFormat) { - defaultLogFormat = logFormat; - createDefaultLogger(); -} - -void createDefaultLogger() { - logger = makeDefaultLogger(); -} - -} diff --git a/src/libmain/loggers.hh b/src/libmain/loggers.hh deleted file mode 100644 index e5721420cb6..00000000000 --- a/src/libmain/loggers.hh +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once -///@file - -#include "types.hh" - -namespace nix { - -enum class LogFormat { - raw, - rawWithLogs, - internalJSON, - bar, - barWithLogs, -}; - -void setLogFormat(const std::string & logFormatStr); -void setLogFormat(const LogFormat & logFormat); - -void createDefaultLogger(); - -} diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index 862ef355b95..5467ba26738 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -3,7 +3,6 @@ #include "shared.hh" #include "store-api.hh" #include "gc-store.hh" -#include "loggers.hh" #include "progress-bar.hh" #include "signals.hh" diff --git a/src/libutil/local.mk b/src/libutil/local.mk index 200026c1e06..6f7236ee9e8 100644 --- a/src/libutil/local.mk +++ b/src/libutil/local.mk @@ -8,7 +8,16 @@ libutil_SOURCES := $(wildcard $(d)/*.cc $(d)/signature/*.cc) libutil_CXXFLAGS += -I src/libutil -libutil_LDFLAGS += $(THREAD_LDFLAGS) $(LIBCURL_LIBS) $(SODIUM_LIBS) $(OPENSSL_LIBS) $(LIBBROTLI_LIBS) $(LIBARCHIVE_LIBS) $(BOOST_LDFLAGS) -lboost_context +libutil_LDFLAGS += \ + $(THREAD_LDFLAGS) \ + $(LIBCURL_LIBS) \ + $(SODIUM_LIBS) \ + $(OPENSSL_LIBS) \ + $(LIBBROTLI_LIBS) \ + $(LIBARCHIVE_LIBS) \ + $(BOOST_LDFLAGS) \ + -lboost_context \ + $(libmain_LDFLAGS) $(foreach i, $(wildcard $(d)/args/*.hh), \ $(eval $(call install-file-in, $(i), $(includedir)/nix/args, 0644))) diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index d68ddacc0c1..886400a0a1c 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -333,4 +333,101 @@ Activity::~Activity() } } +LogFormat defaultLogFormat = LogFormat::raw; + +std::optional parseLogFormat(const std::string & str) +{ + if (str == "raw" || getEnv("NIX_GET_COMPLETIONS")) + return LogFormat::raw; + else if (str == "raw-with-logs") + return LogFormat::rawWithLogs; + else if (str == "internal-json") + return LogFormat::internalJSON; + else if (str == "bar") + return LogFormat::bar; + else if (str == "bar-with-logs") + return LogFormat::barWithLogs; + else + return std::nullopt; +} + +std::ostream & operator<<(std::ostream & output, const LogFormat & format) +{ + switch (format) { + case LogFormat::raw: + return output << "raw"; + case LogFormat::rawWithLogs: + return output << "raw-with-logs"; + case LogFormat::internalJSON: + return output << "internal-json"; + case LogFormat::bar: + return output << "bar"; + case LogFormat::barWithLogs: + return output << "bar-with-logs"; + default: + abort(); + } + return output; +} + +std::string logFormatToString(const LogFormat & format) +{ + std::ostringstream stream; + stream << format; + return stream.str(); +} + +void to_json(nlohmann::json &j, const LogFormat &format) +{ + j = logFormatToString(format); +} + +void from_json(const nlohmann::json &j, LogFormat &format) +{ + auto parsed = parseLogFormat(j.template get()); + if (parsed.has_value()) { + format = parsed.value(); + } else { + // Error ID 302 seems to fit best: + // "During implicit or explicit value conversion, the JSON type + // must be compatible to the target type." + throw nlohmann::detail::type_error::create(302, "string is not a valid log-format", &j); + } +} + +Logger * makeDefaultLogger() +{ + switch (defaultLogFormat) { + case LogFormat::raw: + return makeSimpleLogger(false); + case LogFormat::rawWithLogs: + return makeSimpleLogger(true); + case LogFormat::internalJSON: + return makeJSONLogger(*makeSimpleLogger(true)); + case LogFormat::bar: + return makeProgressBar(); + case LogFormat::barWithLogs: { + auto logger = makeProgressBar(); + logger->setPrintBuildLogs(true); + return logger; + } + default: + abort(); + } +} + +void setLogFormat(const LogFormat & logFormat) +{ + if (defaultLogFormat == logFormat) { + return; + } + defaultLogFormat = logFormat; + createDefaultLogger(); +} + +void createDefaultLogger() +{ + logger = makeDefaultLogger(); +} + } diff --git a/src/libutil/logging.hh b/src/libutil/logging.hh index 183f2d8e13e..54d7fa83911 100644 --- a/src/libutil/logging.hh +++ b/src/libutil/logging.hh @@ -6,9 +6,44 @@ #include "config.hh" #include +#include namespace nix { +enum class LogFormat { + /** + * The format used by old-style commands like `nix-build`, but without build + * logs. + */ + raw, + /** + * The format used by old-style commands like `nix-build`. + */ + rawWithLogs, + /** + * A JSON format for internal use. + */ + internalJSON, + /** + * The progress bar format used by `nix` (v3) commands. + */ + bar, + /** + * The progress bar format used by `nix` (v3) commands, but with build logs. + */ + barWithLogs, +}; + +std::ostream & operator<<(std::ostream & output, const LogFormat & format); +std::string logFormatToString(const LogFormat & format); +std::optional parseLogFormat(const std::string & str); + +void to_json(nlohmann::json & j, const LogFormat & format); +void from_json(const nlohmann::json & j, LogFormat & format); + +void setLogFormat(const LogFormat & logFormat); +void createDefaultLogger(); + typedef enum { actUnknown = 0, actCopyPath = 100, @@ -175,6 +210,8 @@ Logger * makeSimpleLogger(bool printBuildLogs = true); Logger * makeJSONLogger(Logger & prevLogger); +Logger * makeProgressBar(); + std::optional parseJSONMessage(const std::string & msg); bool handleJSONLogMessage(nlohmann::json & json, diff --git a/src/nix/main.cc b/src/nix/main.cc index 39c04069be6..9cac407716d 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -13,7 +13,6 @@ #include "store-api.hh" #include "filetransfer.hh" #include "finally.hh" -#include "loggers.hh" #include "markdown.hh" #include "memory-input-accessor.hh"