diff options
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/args.cc | 18 | ||||
-rw-r--r-- | src/libutil/args.hh | 68 | ||||
-rw-r--r-- | src/libutil/config.cc | 6 | ||||
-rw-r--r-- | src/libutil/logging.cc | 130 | ||||
-rw-r--r-- | src/libutil/logging.hh | 6 | ||||
-rw-r--r-- | src/libutil/serialise.hh | 22 | ||||
-rw-r--r-- | src/libutil/util.cc | 10 | ||||
-rw-r--r-- | src/libutil/util.hh | 6 |
8 files changed, 213 insertions, 53 deletions
diff --git a/src/libutil/args.cc b/src/libutil/args.cc index d17a1e7a9abb..7af2a1bf731a 100644 --- a/src/libutil/args.cc +++ b/src/libutil/args.cc @@ -100,7 +100,7 @@ bool Args::processFlag(Strings::iterator & pos, Strings::iterator end) auto process = [&](const std::string & name, const Flag & flag) -> bool { ++pos; - Strings args; + std::vector<std::string> args; for (size_t n = 0 ; n < flag.arity; ++n) { if (pos == end) { if (flag.arity == ArityAny) break; @@ -109,7 +109,7 @@ bool Args::processFlag(Strings::iterator & pos, Strings::iterator end) } args.push_back(*pos++); } - flag.handler(args); + flag.handler(std::move(args)); return true; }; @@ -144,7 +144,9 @@ bool Args::processArgs(const Strings & args, bool finish) if ((exp.arity == 0 && finish) || (exp.arity > 0 && args.size() == exp.arity)) { - exp.handler(args); + std::vector<std::string> ss; + for (auto & s : args) ss.push_back(s); + exp.handler(std::move(ss)); expectedArgs.pop_front(); res = true; } @@ -155,13 +157,17 @@ bool Args::processArgs(const Strings & args, bool finish) return res; } -void Args::mkHashTypeFlag(const std::string & name, HashType * ht) +Args::FlagMaker & Args::FlagMaker::mkHashTypeFlag(HashType * ht) { - mkFlag1(0, name, "TYPE", "hash algorithm ('md5', 'sha1', 'sha256', or 'sha512')", [=](std::string s) { + arity(1); + label("type"); + description("hash algorithm ('md5', 'sha1', 'sha256', or 'sha512')"); + handler([ht](std::string s) { *ht = parseHashType(s); if (*ht == htUnknown) - throw UsageError(format("unknown hash type '%1%'") % s); + throw UsageError("unknown hash type '%1%'", s); }); + return *this; } Strings argvToStrings(int argc, char * * argv) diff --git a/src/libutil/args.hh b/src/libutil/args.hh index 37e31825ab37..ad5fcca39418 100644 --- a/src/libutil/args.hh +++ b/src/libutil/args.hh @@ -37,7 +37,7 @@ protected: std::string description; Strings labels; size_t arity = 0; - std::function<void(Strings)> handler; + std::function<void(std::vector<std::string>)> handler; std::string category; }; @@ -54,7 +54,7 @@ protected: std::string label; size_t arity; // 0 = any bool optional; - std::function<void(Strings)> handler; + std::function<void(std::vector<std::string>)> handler; }; std::list<ExpectedArg> expectedArgs; @@ -76,24 +76,35 @@ public: FlagMaker & longName(const std::string & s) { flag->longName = s; return *this; }; FlagMaker & shortName(char s) { flag->shortName = s; return *this; }; FlagMaker & description(const std::string & s) { flag->description = s; return *this; }; - FlagMaker & labels(const Strings & ls) { flag->labels = ls; return *this; }; + FlagMaker & label(const std::string & l) { flag->arity = 1; flag->labels = {l}; return *this; }; + FlagMaker & labels(const Strings & ls) { flag->arity = ls.size(); flag->labels = ls; return *this; }; FlagMaker & arity(size_t arity) { flag->arity = arity; return *this; }; - FlagMaker & handler(std::function<void(Strings)> handler) { flag->handler = handler; return *this; }; + FlagMaker & handler(std::function<void(std::vector<std::string>)> handler) { flag->handler = handler; return *this; }; + FlagMaker & handler(std::function<void()> handler) { flag->handler = [handler](std::vector<std::string>) { handler(); }; return *this; }; + FlagMaker & handler(std::function<void(std::string)> handler) { + flag->arity = 1; + flag->handler = [handler](std::vector<std::string> ss) { handler(std::move(ss[0])); }; + return *this; + }; FlagMaker & category(const std::string & s) { flag->category = s; return *this; }; template<class T> - FlagMaker & dest(T * dest) { + FlagMaker & dest(T * dest) + { flag->arity = 1; - flag->handler = [=](Strings ss) { *dest = ss.front(); }; + flag->handler = [=](std::vector<std::string> ss) { *dest = ss[0]; }; return *this; }; template<class T> - FlagMaker & set(T * dest, const T & val) { + FlagMaker & set(T * dest, const T & val) + { flag->arity = 0; - flag->handler = [=](Strings ss) { *dest = val; }; + flag->handler = [=](std::vector<std::string> ss) { *dest = val; }; return *this; }; + + FlagMaker & mkHashTypeFlag(HashType * ht); }; FlagMaker mkFlag(); @@ -101,16 +112,6 @@ public: /* Helper functions for constructing flags / positional arguments. */ - void mkFlag(char shortName, const std::string & longName, - const std::string & description, std::function<void()> fun) - { - mkFlag() - .shortName(shortName) - .longName(longName) - .description(description) - .handler(std::bind(fun)); - } - void mkFlag1(char shortName, const std::string & longName, const std::string & label, const std::string & description, std::function<void(std::string)> fun) @@ -121,7 +122,7 @@ public: .labels({label}) .description(description) .arity(1) - .handler([=](Strings ss) { fun(ss.front()); }); + .handler([=](std::vector<std::string> ss) { fun(ss[0]); }); } void mkFlag(char shortName, const std::string & name, @@ -130,17 +131,6 @@ public: mkFlag(shortName, name, description, dest, true); } - void mkFlag(char shortName, const std::string & longName, - const std::string & label, const std::string & description, - string * dest) - { - mkFlag1(shortName, longName, label, description, [=](std::string s) { - *dest = s; - }); - } - - void mkHashTypeFlag(const std::string & name, HashType * ht); - template<class T> void mkFlag(char shortName, const std::string & longName, const std::string & description, T * dest, const T & value) @@ -149,7 +139,7 @@ public: .shortName(shortName) .longName(longName) .description(description) - .handler([=](Strings ss) { *dest = value; }); + .handler([=](std::vector<std::string> ss) { *dest = value; }); } template<class I> @@ -171,10 +161,10 @@ public: .labels({"N"}) .description(description) .arity(1) - .handler([=](Strings ss) { + .handler([=](std::vector<std::string> ss) { I n; - if (!string2Int(ss.front(), n)) - throw UsageError(format("flag '--%1%' requires a integer argument") % longName); + if (!string2Int(ss[0], n)) + throw UsageError("flag '--%s' requires a integer argument", longName); fun(n); }); } @@ -182,16 +172,16 @@ public: /* Expect a string argument. */ void expectArg(const std::string & label, string * dest, bool optional = false) { - expectedArgs.push_back(ExpectedArg{label, 1, optional, [=](Strings ss) { - *dest = ss.front(); + expectedArgs.push_back(ExpectedArg{label, 1, optional, [=](std::vector<std::string> ss) { + *dest = ss[0]; }}); } /* Expect 0 or more arguments. */ - void expectArgs(const std::string & label, Strings * dest) + void expectArgs(const std::string & label, std::vector<std::string> * dest) { - expectedArgs.push_back(ExpectedArg{label, 0, false, [=](Strings ss) { - *dest = ss; + expectedArgs.push_back(ExpectedArg{label, 0, false, [=](std::vector<std::string> ss) { + *dest = std::move(ss); }}); } diff --git a/src/libutil/config.cc b/src/libutil/config.cc index 27157a83178a..14c4cca031bb 100644 --- a/src/libutil/config.cc +++ b/src/libutil/config.cc @@ -152,7 +152,7 @@ void BaseSetting<T>::convertToArg(Args & args, const std::string & category) .longName(name) .description(description) .arity(1) - .handler([=](Strings ss) { set(*ss.begin()); }) + .handler([=](std::vector<std::string> ss) { set(ss[0]); }) .category(category); } @@ -201,12 +201,12 @@ template<> void BaseSetting<bool>::convertToArg(Args & args, const std::string & args.mkFlag() .longName(name) .description(description) - .handler([=](Strings ss) { value = true; }) + .handler([=](std::vector<std::string> ss) { value = true; }) .category(category); args.mkFlag() .longName("no-" + name) .description(description) - .handler([=](Strings ss) { value = false; }) + .handler([=](std::vector<std::string> ss) { value = false; }) .category(category); } diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index e38a460537ea..011155871122 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -2,6 +2,7 @@ #include "util.hh" #include <atomic> +#include <nlohmann/json.hpp> namespace nix { @@ -90,4 +91,133 @@ Activity::Activity(Logger & logger, Verbosity lvl, ActivityType type, logger.startActivity(id, lvl, type, s, fields, parent); } +struct JSONLogger : Logger +{ + Logger & prevLogger; + + JSONLogger(Logger & prevLogger) : prevLogger(prevLogger) { } + + void addFields(nlohmann::json & json, const Fields & fields) + { + if (fields.empty()) return; + auto & arr = json["fields"] = nlohmann::json::array(); + for (auto & f : fields) + if (f.type == Logger::Field::tInt) + arr.push_back(f.i); + else if (f.type == Logger::Field::tString) + arr.push_back(f.s); + else + abort(); + } + + void write(const nlohmann::json & json) + { + prevLogger.log(lvlError, "@nix " + json.dump()); + } + + void log(Verbosity lvl, const FormatOrString & fs) override + { + nlohmann::json json; + json["action"] = "msg"; + json["level"] = lvl; + json["msg"] = fs.s; + write(json); + } + + void startActivity(ActivityId act, Verbosity lvl, ActivityType type, + const std::string & s, const Fields & fields, ActivityId parent) override + { + nlohmann::json json; + json["action"] = "start"; + json["id"] = act; + json["level"] = lvl; + json["type"] = type; + json["text"] = s; + addFields(json, fields); + // FIXME: handle parent + write(json); + } + + void stopActivity(ActivityId act) override + { + nlohmann::json json; + json["action"] = "stop"; + json["id"] = act; + write(json); + } + + void result(ActivityId act, ResultType type, const Fields & fields) override + { + nlohmann::json json; + json["action"] = "result"; + json["id"] = act; + json["type"] = type; + addFields(json, fields); + write(json); + } +}; + +Logger * makeJSONLogger(Logger & prevLogger) +{ + return new JSONLogger(prevLogger); +} + +static Logger::Fields getFields(nlohmann::json & json) +{ + Logger::Fields fields; + for (auto & f : json) { + if (f.type() == nlohmann::json::value_t::number_unsigned) + fields.emplace_back(Logger::Field(f.get<uint64_t>())); + else if (f.type() == nlohmann::json::value_t::string) + fields.emplace_back(Logger::Field(f.get<std::string>())); + else throw Error("unsupported JSON type %d", (int) f.type()); + } + return fields; +} + +bool handleJSONLogMessage(const std::string & msg, + const Activity & act, std::map<ActivityId, Activity> & activities, bool trusted) +{ + if (!hasPrefix(msg, "@nix ")) return false; + + try { + auto json = nlohmann::json::parse(std::string(msg, 5)); + + std::string action = json["action"]; + + if (action == "start") { + auto type = (ActivityType) json["type"]; + if (trusted || type == actDownload) + activities.emplace(std::piecewise_construct, + std::forward_as_tuple(json["id"]), + std::forward_as_tuple(*logger, (Verbosity) json["level"], type, + json["text"], getFields(json["fields"]), act.id)); + } + + else if (action == "stop") + activities.erase((ActivityId) json["id"]); + + else if (action == "result") { + auto i = activities.find((ActivityId) json["id"]); + if (i != activities.end()) + i->second.result((ResultType) json["type"], getFields(json["fields"])); + } + + else if (action == "setPhase") { + std::string phase = json["phase"]; + act.result(resSetPhase, phase); + } + + else if (action == "msg") { + std::string msg = json["msg"]; + logger->log((Verbosity) json["level"], msg); + } + + } catch (std::exception & e) { + printError("bad log message from builder: %s", e.what()); + } + + return true; +} + } diff --git a/src/libutil/logging.hh b/src/libutil/logging.hh index 21898c03a276..677aa4daec4d 100644 --- a/src/libutil/logging.hh +++ b/src/libutil/logging.hh @@ -130,6 +130,12 @@ extern Logger * logger; Logger * makeDefaultLogger(); +Logger * makeJSONLogger(Logger & prevLogger); + +bool handleJSONLogMessage(const std::string & msg, + const Activity & act, std::map<ActivityId, Activity> & activities, + bool trusted); + extern Verbosity verbosity; /* suppress msgs > this */ /* Print a message if the current log level is at least the specified diff --git a/src/libutil/serialise.hh b/src/libutil/serialise.hh index 70b193941638..2ea5b6354ee9 100644 --- a/src/libutil/serialise.hh +++ b/src/libutil/serialise.hh @@ -92,7 +92,17 @@ struct FdSink : BufferedSink FdSink() : fd(-1) { } FdSink(int fd) : fd(fd) { } FdSink(FdSink&&) = default; - FdSink& operator=(FdSink&&) = default; + + FdSink& operator=(FdSink && s) + { + flush(); + fd = s.fd; + s.fd = -1; + warn = s.warn; + written = s.written; + return *this; + } + ~FdSink(); void write(const unsigned char * data, size_t len) override; @@ -112,6 +122,16 @@ struct FdSource : BufferedSource FdSource() : fd(-1) { } FdSource(int fd) : fd(fd) { } + FdSource(FdSource&&) = default; + + FdSource& operator=(FdSource && s) + { + fd = s.fd; + s.fd = -1; + read = s.read; + return *this; + } + size_t readUnbuffered(unsigned char * data, size_t len) override; bool good() override; private: diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 3c98a61f9e50..9346d5dc4cf8 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -1142,6 +1142,16 @@ std::string toLower(const std::string & s) } +std::string shellEscape(const std::string & s) +{ + std::string r = "'"; + for (auto & i : s) + if (i == '\'') r += "'\\''"; else r += i; + r += '\''; + return r; +} + + void ignoreException() { try { diff --git a/src/libutil/util.hh b/src/libutil/util.hh index 6a66576e96ce..fccf5d854800 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -352,10 +352,8 @@ bool hasSuffix(const string & s, const string & suffix); std::string toLower(const std::string & s); -/* Escape a string that contains octal-encoded escape codes such as - used in /etc/fstab and /proc/mounts (e.g. "foo\040bar" decodes to - "foo bar"). */ -string decodeOctalEscaped(const string & s); +/* Escape a string as a shell word. */ +std::string shellEscape(const std::string & s); /* Exception handling in destructors: print an error message, then |