Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add log-format and log-format-legacy settings #1

Draft
wants to merge 2 commits into
base: setting-handlers
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions 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 Expand Up @@ -56,14 +55,6 @@ MixCommonArgs::MixCommonArgs(const std::string & programName)
}
});

addFlag({
.longName = "log-format",
.description = "Set the format of log output; one of `raw`, `internal-json`, `bar` or `bar-with-logs`.",
.category = loggingCategory,
.labels = {"format"},
.handler = {[](std::string format) { setLogFormat(format); }},
});

addFlag({
.longName = "max-jobs",
.shortName = 'j',
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.

3 changes: 1 addition & 2 deletions 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 Expand Up @@ -183,7 +182,7 @@ LegacyArgs::LegacyArgs(const std::string & programName,
.longName = "no-build-output",
.shortName = 'Q',
.description = "Do not show build output.",
.handler = {[&]() {setLogFormat(LogFormat::raw); }},
.handler = {[&]() {loggerSettings.logFormat.assign(LogFormat::raw); }},
});

addFlag({
Expand Down
56 changes: 56 additions & 0 deletions src/libutil/config-impl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,60 @@ std::string Setting<T>::to_string() const
return std::to_string(value);
}

template<>
LogFormat Setting<LogFormat>::parse(const std::string & str) const
{
auto format = parseLogFormat(str);
if (format.has_value()) {
return format.value();
} else {
throw UsageError("setting '%s' has invalid value '%s'", name, str);
}
}

template<>
std::string Setting<LogFormat>::to_string() const
{
return logFormatToString(value);
}

template<>
void Setting<LogFormat>::convertToArg(Args &args, const std::string &category)
{
args.addFlag({
.longName = name,
.description = fmt("Set the `%s` setting.", name),
.category = category,
.labels = {"format"},
.handler = {[this](std::string format) { override(parse(format)); }},
.experimentalFeature = experimentalFeature,
});
}

template<>
std::optional<LogFormat> Setting<std::optional<LogFormat>>::parse(const std::string & str) const
{
if (str.empty()) {
return std::nullopt;
}

auto format = parseLogFormat(str);
if (format.has_value()) {
return format.value();
} else {
throw UsageError("setting '%s' has invalid value '%s'", name, str);
}
}

template<>
std::string Setting<std::optional<LogFormat>>::to_string() const
{
if (value.has_value()) {
return logFormatToString(value.value());
} else {
// TODO: Will returning the empty string here cause problems?
return "";
}
}

}
2 changes: 2 additions & 0 deletions src/libutil/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ template class Setting<Strings>;
template class Setting<StringSet>;
template class Setting<StringMap>;
template class Setting<std::set<ExperimentalFeature>>;
template class Setting<LogFormat>;
template class Setting<std::optional<LogFormat>>;

static Path parsePath(const AbstractSetting & s, const std::string & str)
{
Expand Down
4 changes: 4 additions & 0 deletions src/libutil/json-utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ struct json_avoids_null<std::list<T>> : std::true_type {};
template<typename K, typename V>
struct json_avoids_null<std::map<K, V>> : std::true_type {};

enum class LogFormat;
template<>
struct json_avoids_null<LogFormat> : std::true_type {};

}

namespace nlohmann {
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();
}

}
Loading
Loading