Skip to content

Commit

Permalink
Merge libmain/loggers into libutil/logging
Browse files Browse the repository at this point in the history
  • Loading branch information
9999years committed Feb 9, 2024
1 parent 28a988b commit 3925e7d
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 81 deletions.
1 change: 0 additions & 1 deletion src/libmain/common-args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "args/root.hh"
#include "globals.hh"
#include "logging.hh"
#include "loggers.hh"
#include "util.hh"

namespace nix {
Expand Down
56 changes: 0 additions & 56 deletions src/libmain/loggers.cc

This file was deleted.

21 changes: 0 additions & 21 deletions src/libmain/loggers.hh

This file was deleted.

1 change: 0 additions & 1 deletion src/libmain/shared.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
11 changes: 10 additions & 1 deletion src/libutil/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
97 changes: 97 additions & 0 deletions src/libutil/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,101 @@ Activity::~Activity()
}
}

LogFormat defaultLogFormat = LogFormat::raw;

std::optional<LogFormat> 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<std::string>());
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();
}

}
37 changes: 37 additions & 0 deletions src/libutil/logging.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,44 @@
#include "config.hh"

#include <nlohmann/json_fwd.hpp>
#include <nlohmann/detail/exceptions.hpp>

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<LogFormat> 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,
Expand Down Expand Up @@ -186,6 +221,8 @@ Logger * makeSimpleLogger(bool printBuildLogs = true);

Logger * makeJSONLogger(Logger & prevLogger);

Logger * makeProgressBar();

std::optional<nlohmann::json> parseJSONMessage(const std::string & msg);

bool handleJSONLogMessage(nlohmann::json & json,
Expand Down
1 change: 0 additions & 1 deletion src/nix/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 3925e7d

Please sign in to comment.