aboutsummaryrefslogtreecommitdiff
path: root/src/libutil
diff options
context:
space:
mode:
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/args.cc11
-rw-r--r--src/libutil/args.hh2
-rw-r--r--src/libutil/hash.hh2
-rw-r--r--src/libutil/logging.cc30
-rw-r--r--src/libutil/logging.hh7
-rw-r--r--src/libutil/tests/hash.cc16
-rw-r--r--src/libutil/util.cc2
-rw-r--r--src/libutil/util.hh3
8 files changed, 53 insertions, 20 deletions
diff --git a/src/libutil/args.cc b/src/libutil/args.cc
index 10d6e89bb..ce6580119 100644
--- a/src/libutil/args.cc
+++ b/src/libutil/args.cc
@@ -217,10 +217,15 @@ MultiCommand::MultiCommand(const Commands & commands)
{
expectedArgs.push_back(ExpectedArg{"command", 1, true, [=](std::vector<std::string> ss) {
assert(!command);
- auto i = commands.find(ss[0]);
+ auto cmd = ss[0];
+ if (auto alias = get(deprecatedAliases, cmd)) {
+ warn("'%s' is a deprecated alias for '%s'", cmd, *alias);
+ cmd = *alias;
+ }
+ auto i = commands.find(cmd);
if (i == commands.end())
- throw UsageError("'%s' is not a recognised command", ss[0]);
- command = {ss[0], i->second()};
+ throw UsageError("'%s' is not a recognised command", cmd);
+ command = {cmd, i->second()};
}});
categories[Command::catDefault] = "Available commands";
diff --git a/src/libutil/args.hh b/src/libutil/args.hh
index 1932e6a8a..154d1e6aa 100644
--- a/src/libutil/args.hh
+++ b/src/libutil/args.hh
@@ -234,6 +234,8 @@ public:
std::map<Command::Category, std::string> categories;
+ std::map<std::string, std::string> deprecatedAliases;
+
// Selected command, if any.
std::optional<std::pair<std::string, ref<Command>>> command;
diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh
index ea9fca3e7..4ff13f6b2 100644
--- a/src/libutil/hash.hh
+++ b/src/libutil/hash.hh
@@ -79,7 +79,7 @@ struct Hash
/* Return a string representation of the hash, in base-16, base-32
or base-64. By default, this is prefixed by the hash type
(e.g. "sha256:"). */
- std::string to_string(Base base = Base32, bool includeType = true) const;
+ std::string to_string(Base base, bool includeType) const;
std::string gitRev() const
{
diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc
index 41378b0db..108dc3bd1 100644
--- a/src/libutil/logging.cc
+++ b/src/libutil/logging.cc
@@ -18,7 +18,7 @@ void setCurActivity(const ActivityId activityId)
curActivity = activityId;
}
-Logger * logger = makeDefaultLogger();
+Logger * logger = makeSimpleLogger(true);
void Logger::warn(const std::string & msg)
{
@@ -35,13 +35,19 @@ class SimpleLogger : public Logger
public:
bool systemd, tty;
+ bool printBuildLogs;
- SimpleLogger()
+ SimpleLogger(bool printBuildLogs)
+ : printBuildLogs(printBuildLogs)
{
systemd = getEnv("IN_SYSTEMD") == "1";
tty = isatty(STDERR_FILENO);
}
+ bool isVerbose() override {
+ return printBuildLogs;
+ }
+
void log(Verbosity lvl, const FormatOrString & fs) override
{
if (lvl > verbosity) return;
@@ -78,6 +84,18 @@ public:
if (lvl <= verbosity && !s.empty())
log(lvl, s + "...");
}
+
+ void result(ActivityId act, ResultType type, const Fields & fields) override
+ {
+ if (type == resBuildLogLine && printBuildLogs) {
+ auto lastLine = fields[0].s;
+ printError(lastLine);
+ }
+ else if (type == resPostBuildLogLine && printBuildLogs) {
+ auto lastLine = fields[0].s;
+ printError("post-build-hook: " + lastLine);
+ }
+ }
};
Verbosity verbosity = lvlInfo;
@@ -102,9 +120,9 @@ void writeToStderr(const string & s)
}
}
-Logger * makeDefaultLogger()
+Logger * makeSimpleLogger(bool printBuildLogs)
{
- return new SimpleLogger();
+ return new SimpleLogger(printBuildLogs);
}
std::atomic<uint64_t> nextId{(uint64_t) getpid() << 32};
@@ -121,6 +139,10 @@ struct JSONLogger : Logger {
JSONLogger(Logger & prevLogger) : prevLogger(prevLogger) { }
+ bool isVerbose() override {
+ return true;
+ }
+
void addFields(nlohmann::json & json, const Fields & fields)
{
if (fields.empty()) return;
diff --git a/src/libutil/logging.hh b/src/libutil/logging.hh
index eeb7233e9..b99b246c3 100644
--- a/src/libutil/logging.hh
+++ b/src/libutil/logging.hh
@@ -54,6 +54,11 @@ public:
virtual ~Logger() { }
+ virtual void stop() { };
+
+ // Whether the logger prints the whole build log
+ virtual bool isVerbose() { return false; }
+
virtual void log(Verbosity lvl, const FormatOrString & fs) = 0;
void log(const FormatOrString & fs)
@@ -140,7 +145,7 @@ struct PushActivity
extern Logger * logger;
-Logger * makeDefaultLogger();
+Logger * makeSimpleLogger(bool printBuildLogs = true);
Logger * makeJSONLogger(Logger & prevLogger);
diff --git a/src/libutil/tests/hash.cc b/src/libutil/tests/hash.cc
index 7cb439817..5334b046e 100644
--- a/src/libutil/tests/hash.cc
+++ b/src/libutil/tests/hash.cc
@@ -11,28 +11,28 @@ namespace nix {
// values taken from: https://tools.ietf.org/html/rfc1321
auto s1 = "";
auto hash = hashString(HashType::htMD5, s1);
- ASSERT_EQ(hash.to_string(Base::Base16), "md5:d41d8cd98f00b204e9800998ecf8427e");
+ ASSERT_EQ(hash.to_string(Base::Base16, true), "md5:d41d8cd98f00b204e9800998ecf8427e");
}
TEST(hashString, testKnownMD5Hashes2) {
// values taken from: https://tools.ietf.org/html/rfc1321
auto s2 = "abc";
auto hash = hashString(HashType::htMD5, s2);
- ASSERT_EQ(hash.to_string(Base::Base16), "md5:900150983cd24fb0d6963f7d28e17f72");
+ ASSERT_EQ(hash.to_string(Base::Base16, true), "md5:900150983cd24fb0d6963f7d28e17f72");
}
TEST(hashString, testKnownSHA1Hashes1) {
// values taken from: https://tools.ietf.org/html/rfc3174
auto s = "abc";
auto hash = hashString(HashType::htSHA1, s);
- ASSERT_EQ(hash.to_string(Base::Base16),"sha1:a9993e364706816aba3e25717850c26c9cd0d89d");
+ ASSERT_EQ(hash.to_string(Base::Base16, true),"sha1:a9993e364706816aba3e25717850c26c9cd0d89d");
}
TEST(hashString, testKnownSHA1Hashes2) {
// values taken from: https://tools.ietf.org/html/rfc3174
auto s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
auto hash = hashString(HashType::htSHA1, s);
- ASSERT_EQ(hash.to_string(Base::Base16),"sha1:84983e441c3bd26ebaae4aa1f95129e5e54670f1");
+ ASSERT_EQ(hash.to_string(Base::Base16, true),"sha1:84983e441c3bd26ebaae4aa1f95129e5e54670f1");
}
TEST(hashString, testKnownSHA256Hashes1) {
@@ -40,7 +40,7 @@ namespace nix {
auto s = "abc";
auto hash = hashString(HashType::htSHA256, s);
- ASSERT_EQ(hash.to_string(Base::Base16),
+ ASSERT_EQ(hash.to_string(Base::Base16, true),
"sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
}
@@ -48,7 +48,7 @@ namespace nix {
// values taken from: https://tools.ietf.org/html/rfc4634
auto s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
auto hash = hashString(HashType::htSHA256, s);
- ASSERT_EQ(hash.to_string(Base::Base16),
+ ASSERT_EQ(hash.to_string(Base::Base16, true),
"sha256:248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
}
@@ -56,7 +56,7 @@ namespace nix {
// values taken from: https://tools.ietf.org/html/rfc4634
auto s = "abc";
auto hash = hashString(HashType::htSHA512, s);
- ASSERT_EQ(hash.to_string(Base::Base16),
+ ASSERT_EQ(hash.to_string(Base::Base16, true),
"sha512:ddaf35a193617abacc417349ae20413112e6fa4e89a9"
"7ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd"
"454d4423643ce80e2a9ac94fa54ca49f");
@@ -67,7 +67,7 @@ namespace nix {
auto s = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
auto hash = hashString(HashType::htSHA512, s);
- ASSERT_EQ(hash.to_string(Base::Base16),
+ ASSERT_EQ(hash.to_string(Base::Base16, true),
"sha512:8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa1"
"7299aeadb6889018501d289e4900f7e4331b99dec4b5433a"
"c7d329eeb6dd26545e96e55b874be909");
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index dcf89ff69..0af2f8434 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -972,7 +972,7 @@ pid_t startProcess(std::function<void()> fun, const ProcessOptions & options)
{
auto wrapper = [&]() {
if (!options.allowVfork)
- logger = makeDefaultLogger();
+ logger = makeSimpleLogger();
try {
#if __linux__
if (options.dieWithParent && prctl(PR_SET_PDEATHSIG, SIGKILL) == -1)
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 30b647a6a..1afb2bb56 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -460,8 +460,7 @@ string base64Encode(const string & s);
string base64Decode(const string & s);
-/* Get a value for the specified key from an associate container, or a
- default value if the key doesn't exist. */
+/* Get a value for the specified key from an associate container. */
template <class T>
std::optional<typename T::mapped_type> get(const T & map, const typename T::key_type & key)
{