From c584480cd46fb49e690e931f326472e512a82878 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 18 May 2020 00:39:50 +0100 Subject: chore(3p/nix/libutil): Remove logging implementation --- third_party/nix/src/libutil/logging.cc | 227 -------------------------------- third_party/nix/src/libutil/logging.hh | 168 ----------------------- third_party/nix/src/libutil/meson.build | 42 ------ 3 files changed, 437 deletions(-) delete mode 100644 third_party/nix/src/libutil/logging.cc delete mode 100644 third_party/nix/src/libutil/logging.hh (limited to 'third_party/nix/src/libutil') diff --git a/third_party/nix/src/libutil/logging.cc b/third_party/nix/src/libutil/logging.cc deleted file mode 100644 index 4f88044757..0000000000 --- a/third_party/nix/src/libutil/logging.cc +++ /dev/null @@ -1,227 +0,0 @@ -#include "logging.hh" -#include -#include -#include "util.hh" - -namespace nix { - -static thread_local ActivityId curActivity = 0; - -ActivityId getCurActivity() { return curActivity; } -void setCurActivity(const ActivityId activityId) { curActivity = activityId; } - -Logger* logger = makeDefaultLogger(); - -void Logger::warn(const std::string& msg) { - log(lvlWarn, ANSI_RED "warning:" ANSI_NORMAL " " + msg); -} - -class SimpleLogger : public Logger { - public: - bool systemd, tty; - - SimpleLogger() { - systemd = getEnv("IN_SYSTEMD") == "1"; - tty = isatty(STDERR_FILENO); - } - - void log(Verbosity lvl, const FormatOrString& fs) override { - if (lvl > verbosity) return; - - std::string prefix; - - if (systemd) { - char c; - switch (lvl) { - case lvlError: - c = '3'; - break; - case lvlWarn: - c = '4'; - break; - case lvlInfo: - c = '5'; - break; - case lvlTalkative: - case lvlChatty: - c = '6'; - break; - default: - c = '7'; - } - prefix = std::string("<") + c + ">"; - } - - writeToStderr(prefix + filterANSIEscapes(fs.s, !tty) + "\n"); - } - - void startActivity(ActivityId act, Verbosity lvl, ActivityType type, - const std::string& s, const Fields& fields, - ActivityId parent) override { - if (lvl <= verbosity && !s.empty()) log(lvl, s + "..."); - } -}; - -Verbosity verbosity = lvlInfo; - -void warnOnce(bool& haveWarned, const FormatOrString& fs) { - if (!haveWarned) { - warn(fs.s); - haveWarned = true; - } -} - -void writeToStderr(const string& s) { - try { - writeFull(STDERR_FILENO, s, false); - } catch (SysError& e) { - /* Ignore failing writes to stderr. We need to ignore write - errors to ensure that cleanup code that logs to stderr runs - to completion if the other side of stderr has been closed - unexpectedly. */ - } -} - -Logger* makeDefaultLogger() { return new SimpleLogger(); } - -std::atomic nextId{(uint64_t)getpid() << 32}; - -Activity::Activity(Logger& logger, Verbosity lvl, ActivityType type, - const std::string& s, const Logger::Fields& fields, - ActivityId parent) - : logger(logger), id(nextId++) { - 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())); - else if (f.type() == nlohmann::json::value_t::string) - fields.emplace_back(Logger::Field(f.get())); - else - throw Error("unsupported JSON type %d", (int)f.type()); - } - return fields; -} - -bool handleJSONLogMessage(const std::string& msg, const Activity& act, - std::map& 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; -} - -Activity::~Activity() { - try { - logger.stopActivity(id); - } catch (...) { - ignoreException(); - } -} - -} // namespace nix diff --git a/third_party/nix/src/libutil/logging.hh b/third_party/nix/src/libutil/logging.hh deleted file mode 100644 index 8b04dfd17e..0000000000 --- a/third_party/nix/src/libutil/logging.hh +++ /dev/null @@ -1,168 +0,0 @@ -#pragma once - -#include "types.hh" - -namespace nix { - -typedef enum { - lvlError = 0, - lvlWarn, - lvlInfo, - lvlTalkative, - lvlChatty, - lvlDebug, - lvlVomit -} Verbosity; - -typedef enum { - actUnknown = 0, - actCopyPath = 100, - actDownload = 101, - actRealise = 102, - actCopyPaths = 103, - actBuilds = 104, - actBuild = 105, - actOptimiseStore = 106, - actVerifyPaths = 107, - actSubstitute = 108, - actQueryPathInfo = 109, - actPostBuildHook = 110, -} ActivityType; - -typedef enum { - resFileLinked = 100, - resBuildLogLine = 101, - resUntrustedPath = 102, - resCorruptedPath = 103, - resSetPhase = 104, - resProgress = 105, - resSetExpected = 106, - resPostBuildLogLine = 107, -} ResultType; - -typedef uint64_t ActivityId; - -class Logger { - friend struct Activity; - - public: - struct Field { - // FIXME: use std::variant. - enum { tInt = 0, tString = 1 } type; - uint64_t i = 0; - std::string s; - Field(const std::string& s) : type(tString), s(s) {} - Field(const char* s) : type(tString), s(s) {} - Field(const uint64_t& i) : type(tInt), i(i) {} - }; - - typedef std::vector Fields; - - virtual ~Logger() {} - - virtual void log(Verbosity lvl, const FormatOrString& fs) = 0; - - void log(const FormatOrString& fs) { log(lvlInfo, fs); } - - virtual void warn(const std::string& msg); - - virtual void startActivity(ActivityId act, Verbosity lvl, ActivityType type, - const std::string& s, const Fields& fields, - ActivityId parent){}; - - virtual void stopActivity(ActivityId act){}; - - virtual void result(ActivityId act, ResultType type, const Fields& fields){}; -}; - -ActivityId getCurActivity(); -void setCurActivity(const ActivityId activityId); - -struct Activity { - Logger& logger; - - const ActivityId id; - - Activity(Logger& logger, Verbosity lvl, ActivityType type, - const std::string& s = "", const Logger::Fields& fields = {}, - ActivityId parent = getCurActivity()); - - Activity(Logger& logger, ActivityType type, const Logger::Fields& fields = {}, - ActivityId parent = getCurActivity()) - : Activity(logger, lvlError, type, "", fields, parent){}; - - Activity(const Activity& act) = delete; - - ~Activity(); - - void progress(uint64_t done = 0, uint64_t expected = 0, uint64_t running = 0, - uint64_t failed = 0) const { - result(resProgress, done, expected, running, failed); - } - - void setExpected(ActivityType type2, uint64_t expected) const { - result(resSetExpected, type2, expected); - } - - template - void result(ResultType type, const Args&... args) const { - Logger::Fields fields; - nop{(fields.emplace_back(Logger::Field(args)), 1)...}; - result(type, fields); - } - - void result(ResultType type, const Logger::Fields& fields) const { - logger.result(id, type, fields); - } - - friend class Logger; -}; - -struct PushActivity { - const ActivityId prevAct; - PushActivity(ActivityId act) : prevAct(getCurActivity()) { - setCurActivity(act); - } - ~PushActivity() { setCurActivity(prevAct); } -}; - -extern Logger* logger; - -Logger* makeDefaultLogger(); - -Logger* makeJSONLogger(Logger& prevLogger); - -bool handleJSONLogMessage(const std::string& msg, const Activity& act, - std::map& activities, - bool trusted); - -extern Verbosity verbosity; /* suppress msgs > this */ - -/* Print a message if the current log level is at least the specified - level. Note that this has to be implemented as a macro to ensure - that the arguments are evaluated lazily. */ -#define printMsg(level, args...) \ - do { \ - if (level <= nix::verbosity) { \ - logger->log(level, fmt(args)); \ - } \ - } while (0) - -#define printError(args...) printMsg(lvlError, args) -#define printInfo(args...) printMsg(lvlInfo, args) -#define printTalkative(args...) printMsg(lvlTalkative, args) -#define debug(args...) printMsg(lvlDebug, args) -#define vomit(args...) printMsg(lvlVomit, args) - -template -inline void warn(const std::string& fs, Args... args) { - boost::format f(fs); - nop{boost::io::detail::feed(f, args)...}; - logger->warn(f.str()); -} - -void warnOnce(bool& haveWarned, const FormatOrString& fs); - -void writeToStderr(const string& s); - -} // namespace nix diff --git a/third_party/nix/src/libutil/meson.build b/third_party/nix/src/libutil/meson.build index c6eec21c5a..c747929212 100644 --- a/third_party/nix/src/libutil/meson.build +++ b/third_party/nix/src/libutil/meson.build @@ -1,12 +1,3 @@ -# Nix lib store build file -#============================================================================ - - - - -# src files -#============================================================================ - src_inc += include_directories('.') libutil_src = files( @@ -17,7 +8,6 @@ libutil_src = files( join_paths(meson.source_root(), 'src/libutil/config.cc'), join_paths(meson.source_root(), 'src/libutil/hash.cc'), join_paths(meson.source_root(), 'src/libutil/json.cc'), - join_paths(meson.source_root(), 'src/libutil/logging.cc'), join_paths(meson.source_root(), 'src/libutil/serialise.cc'), join_paths(meson.source_root(), 'src/libutil/thread-pool.cc'), join_paths(meson.source_root(), 'src/libutil/util.cc'), @@ -34,7 +24,6 @@ libutil_headers = files( join_paths(meson.source_root(), 'src/libutil/istringstream_nocopy.hh'), join_paths(meson.source_root(), 'src/libutil/json.hh'), join_paths(meson.source_root(), 'src/libutil/lazy.hh'), - join_paths(meson.source_root(), 'src/libutil/logging.hh'), join_paths(meson.source_root(), 'src/libutil/lru-cache.hh'), join_paths(meson.source_root(), 'src/libutil/monitor-fd.hh'), join_paths(meson.source_root(), 'src/libutil/pool.hh'), @@ -47,11 +36,6 @@ libutil_headers = files( join_paths(meson.source_root(), 'src/libutil/xml-writer.hh') ) - - -# dependancies -#============================================================================ - libutil_dep_list = [ boost_dep, libbz2_dep, @@ -61,36 +45,10 @@ libutil_dep_list = [ pthread_dep, libsodium_dep] - - - -# Link args -#============================================================================ - libutil_link_list = [] - libutil_link_args = [] - - - - -# compiler args -#============================================================================ - libutil_cxx_args = [] - - - -# targets -#============================================================================ - - - - -# build -#============================================================================ - libutil_lib = library( 'nixutil', install : true, -- cgit 1.4.1