From 6dc6c29fa4a4ddd3bb72f8415fac5936d719bd44 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 18 May 2020 02:34:41 +0100 Subject: refactor(3p/nix/libutil): Replace internal logging library with glog --- third_party/nix/src/libutil/affinity.cc | 23 ++++++++++++++++------- third_party/nix/src/libutil/archive.cc | 17 +++++++++++------ third_party/nix/src/libutil/compression.cc | 14 ++++++++------ third_party/nix/src/libutil/config.cc | 16 ++++++++++++---- third_party/nix/src/libutil/meson.build | 3 ++- third_party/nix/src/libutil/serialise.cc | 6 +++--- third_party/nix/src/libutil/thread-pool.cc | 12 ++++++++---- third_party/nix/src/libutil/types.hh | 4 ++-- third_party/nix/src/libutil/util.cc | 24 ++++++++++++++---------- third_party/nix/src/libutil/util.hh | 1 - 10 files changed, 76 insertions(+), 44 deletions(-) (limited to 'third_party/nix/src') diff --git a/third_party/nix/src/libutil/affinity.cc b/third_party/nix/src/libutil/affinity.cc index faee4926d579..af3bc152f402 100644 --- a/third_party/nix/src/libutil/affinity.cc +++ b/third_party/nix/src/libutil/affinity.cc @@ -1,4 +1,5 @@ #include "affinity.hh" +#include #include "types.hh" #include "util.hh" @@ -15,14 +16,18 @@ static cpu_set_t savedAffinity; void setAffinityTo(int cpu) { #if __linux__ - if (sched_getaffinity(0, sizeof(cpu_set_t), &savedAffinity) == -1) return; + if (sched_getaffinity(0, sizeof(cpu_set_t), &savedAffinity) == -1) { + return; + } + didSaveAffinity = true; - debug(format("locking this thread to CPU %1%") % cpu); + DLOG(INFO) << "locking this thread to CPU " << cpu; cpu_set_t newAffinity; CPU_ZERO(&newAffinity); CPU_SET(cpu, &newAffinity); - if (sched_setaffinity(0, sizeof(cpu_set_t), &newAffinity) == -1) - printError(format("failed to lock thread to CPU %1%") % cpu); + if (sched_setaffinity(0, sizeof(cpu_set_t), &newAffinity) == -1) { + LOG(ERROR) << "failed to lock thread to CPU " << cpu; + } #endif } @@ -38,9 +43,13 @@ int lockToCurrentCPU() { void restoreAffinity() { #if __linux__ - if (!didSaveAffinity) return; - if (sched_setaffinity(0, sizeof(cpu_set_t), &savedAffinity) == -1) - printError("failed to restore affinity %1%"); + if (!didSaveAffinity) { + return; + } + + if (sched_setaffinity(0, sizeof(cpu_set_t), &savedAffinity) == -1) { + LOG(ERROR) << "failed to restore affinity"; + } #endif } diff --git a/third_party/nix/src/libutil/archive.cc b/third_party/nix/src/libutil/archive.cc index 9ae5c76db751..8f98a3daf301 100644 --- a/third_party/nix/src/libutil/archive.cc +++ b/third_party/nix/src/libutil/archive.cc @@ -10,6 +10,7 @@ #include #include #include "config.hh" +#include "glog/logging.h" #include "util.hh" namespace nix { @@ -61,8 +62,9 @@ static void dump(const Path& path, Sink& sink, PathFilter& filter) { checkInterrupt(); struct stat st; - if (lstat(path.c_str(), &st)) + if (lstat(path.c_str(), &st)) { throw SysError(format("getting attributes of path '%1%'") % path); + } sink << "("; @@ -87,8 +89,9 @@ static void dump(const Path& path, Sink& sink, PathFilter& filter) { string name(i.name); size_t pos = i.name.find(caseHackSuffix); if (pos != string::npos) { - debug(format("removing case hack suffix from '%1%'") % - (path + "/" + i.name)); + DLOG(INFO) << "removing case hack suffix from " << path << "/" + << i.name; + name.erase(pos); } if (unhacked.find(name) != unhacked.end()) @@ -247,15 +250,17 @@ static void parse(ParseSink& sink, Source& source, const Path& path) { if (archiveSettings.useCaseHack) { auto i = names.find(name); if (i != names.end()) { - debug(format("case collision between '%1%' and '%2%'") % - i->first % name); + DLOG(INFO) << "case collision between '" << i->first << "' and '" + << name << "'"; name += caseHackSuffix; name += std::to_string(++i->second); } else names[name] = 0; } } else if (s == "node") { - if (s.empty()) throw badArchive("entry name missing"); + if (s.empty()) { + throw badArchive("entry name missing"); + } parse(sink, source, path + "/" + name); } else throw badArchive("unknown field " + s); diff --git a/third_party/nix/src/libutil/compression.cc b/third_party/nix/src/libutil/compression.cc index 39bde37e0fd4..1f284b097054 100644 --- a/third_party/nix/src/libutil/compression.cc +++ b/third_party/nix/src/libutil/compression.cc @@ -7,7 +7,7 @@ #include #include #include "finally.hh" -#include "logging.hh" +#include "glog/logging.h" #include "util.hh" namespace nix { @@ -217,16 +217,18 @@ struct XzCompressionSink : CompressionSink { ret = lzma_stream_encoder_mt(&strm, &mt_options); done = true; #else - printMsg(lvlError, - "warning: parallel XZ compression requested but not supported, " - "falling back to single-threaded compression"); + LOG(ERROR) << "parallel XZ compression requested but not supported, " + << "falling back to single-threaded compression"; #endif } - if (!done) ret = lzma_easy_encoder(&strm, 6, LZMA_CHECK_CRC64); + if (!done) { + ret = lzma_easy_encoder(&strm, 6, LZMA_CHECK_CRC64); + } - if (ret != LZMA_OK) + if (ret != LZMA_OK) { throw CompressionError("unable to initialise lzma encoder"); + } // FIXME: apply the x86 BCJ filter? diff --git a/third_party/nix/src/libutil/config.cc b/third_party/nix/src/libutil/config.cc index d737945924e8..93550fafa3d6 100644 --- a/third_party/nix/src/libutil/config.cc +++ b/third_party/nix/src/libutil/config.cc @@ -1,6 +1,9 @@ +#define GOOGLE_STRIP_LOG 0 #include "config.hh" +#include #include "args.hh" #include "json.hh" +// #include namespace nix { @@ -30,9 +33,12 @@ void Config::addSetting(AbstractSetting* setting) { for (auto& alias : setting->aliases) { auto i = unknownSettings.find(alias); if (i != unknownSettings.end()) { - if (set) - warn("setting '%s' is set, but it's an alias of '%s' which is also set", - alias, setting->name); + if (set) { + LOG(WARNING) << "setting '" << alias + << "' is set, but it's an alias of '" << setting->name + << "', which is also set"; + } + else { setting->set(i->second); setting->overriden = true; @@ -44,7 +50,9 @@ void Config::addSetting(AbstractSetting* setting) { } void AbstractConfig::warnUnknownSettings() { - for (auto& s : unknownSettings) warn("unknown setting '%s'", s.first); + for (auto& s : unknownSettings) { + LOG(WARNING) << "unknown setting: " << s.first; + } } void AbstractConfig::reapplyUnknownSettings() { diff --git a/third_party/nix/src/libutil/meson.build b/third_party/nix/src/libutil/meson.build index c7479292122b..5fb3e4804bc3 100644 --- a/third_party/nix/src/libutil/meson.build +++ b/third_party/nix/src/libutil/meson.build @@ -37,6 +37,7 @@ libutil_headers = files( ) libutil_dep_list = [ + glog_dep, boost_dep, libbz2_dep, liblzma_dep, @@ -47,7 +48,6 @@ libutil_dep_list = [ libutil_link_list = [] libutil_link_args = [] -libutil_cxx_args = [] libutil_lib = library( 'nixutil', @@ -57,6 +57,7 @@ libutil_lib = library( include_directories : src_inc, sources : libutil_src, link_args : libutil_link_args, + # cpp_args : [ '-E' ], dependencies : libutil_dep_list) install_headers( diff --git a/third_party/nix/src/libutil/serialise.cc b/third_party/nix/src/libutil/serialise.cc index 6d7cb1d8d156..1014ef8f70c3 100644 --- a/third_party/nix/src/libutil/serialise.cc +++ b/third_party/nix/src/libutil/serialise.cc @@ -3,6 +3,7 @@ #include #include #include +#include "glog/logging.h" #include "util.hh" namespace nix { @@ -47,9 +48,8 @@ FdSink::~FdSink() { size_t threshold = 256 * 1024 * 1024; static void warnLargeDump() { - printError( - "warning: dumping very large path (> 256 MiB); this may run out of " - "memory"); + LOG(WARNING) + << "dumping very large path (> 256 MiB); this may run out of memory"; } void FdSink::write(const unsigned char* data, size_t len) { diff --git a/third_party/nix/src/libutil/thread-pool.cc b/third_party/nix/src/libutil/thread-pool.cc index 5b5be926539d..3f50b6aaa2e6 100644 --- a/third_party/nix/src/libutil/thread-pool.cc +++ b/third_party/nix/src/libutil/thread-pool.cc @@ -1,5 +1,6 @@ #include "thread-pool.hh" #include "affinity.hh" +#include "glog/logging.h" namespace nix { @@ -11,7 +12,7 @@ ThreadPool::ThreadPool(size_t _maxThreads) : maxThreads(_maxThreads) { if (!maxThreads) maxThreads = 1; } - debug("starting pool of %d threads", maxThreads - 1); + DLOG(INFO) << "starting pool of " << maxThreads - 1 << " threads"; } ThreadPool::~ThreadPool() { shutdown(); } @@ -26,18 +27,21 @@ void ThreadPool::shutdown() { if (workers.empty()) return; - debug("reaping %d worker threads", workers.size()); + DLOG(INFO) << "reaping " << workers.size() << " worker threads"; work.notify_all(); - for (auto& thr : workers) thr.join(); + for (auto& thr : workers) { + thr.join(); + } } void ThreadPool::enqueue(const work_t& t) { auto state(state_.lock()); - if (quit) + if (quit) { throw ThreadPoolShutDown( "cannot enqueue a work item while the thread pool is shutting down"); + } state->pending.push(t); /* Note: process() also executes items, so count it as a worker. */ if (state->pending.size() > state->workers.size() + 1 && diff --git a/third_party/nix/src/libutil/types.hh b/third_party/nix/src/libutil/types.hh index ab2fee392286..25e9d23017cf 100644 --- a/third_party/nix/src/libutil/types.hh +++ b/third_party/nix/src/libutil/types.hh @@ -77,8 +77,8 @@ class BaseError : public std::exception { BaseError(Args... args) : err(fmt(args...)) {} #ifdef EXCEPTION_NEEDS_THROW_SPEC - ~BaseError() throw(){}; - const char* what() const throw() { return err.c_str(); } + ~BaseError() noexcept {}; + const char* what() const noexcept { return err.c_str(); } #else const char* what() const noexcept { return err.c_str(); } #endif diff --git a/third_party/nix/src/libutil/util.cc b/third_party/nix/src/libutil/util.cc index 1f0bc74cc72d..21c14b0cf5d7 100644 --- a/third_party/nix/src/libutil/util.cc +++ b/third_party/nix/src/libutil/util.cc @@ -18,6 +18,7 @@ #include #include "affinity.hh" #include "finally.hh" +#include "glog/logging.h" #include "lazy.hh" #include "serialise.hh" #include "sync.hh" @@ -669,7 +670,7 @@ Pid::operator pid_t() { return pid; } int Pid::kill() { assert(pid != -1); - debug(format("killing process %1%") % pid); + DLOG(INFO) << "killing process " << pid; /* Send the requested signal to the child. If it has its own process group, send the signal to every process in the child @@ -681,7 +682,7 @@ int Pid::kill() { #if __FreeBSD__ || __APPLE__ if (errno != EPERM || ::kill(pid, 0) != 0) #endif - printError((SysError("killing process %d", pid).msg())); + LOG(ERROR) << SysError("killing process %d", pid).msg(); } return wait(); @@ -696,7 +697,9 @@ int Pid::wait() { pid = -1; return status; } - if (errno != EINTR) throw SysError("cannot get child exit status"); + if (errno != EINTR) { + throw SysError("cannot get child exit status"); + } checkInterrupt(); } } @@ -712,7 +715,7 @@ pid_t Pid::release() { } void killUser(uid_t uid) { - debug(format("killing all processes running under uid '%1%'") % uid); + DLOG(INFO) << "killing all processes running under UID " << uid; assert(uid != 0); /* just to be safe... */ @@ -725,7 +728,9 @@ void killUser(uid_t uid) { Pid pid = startProcess( [&]() { - if (setuid(uid) == -1) throw SysError("setting uid"); + if (setuid(uid) == -1) { + throw SysError("setting uid"); + } while (true) { #ifdef __APPLE__ @@ -777,7 +782,6 @@ static pid_t doFork(bool allowVfork, std::function fun) { pid_t startProcess(std::function fun, const ProcessOptions& options) { auto wrapper = [&]() { - if (!options.allowVfork) logger = makeDefaultLogger(); try { #if __linux__ if (options.dieWithParent && prctl(PR_SET_PDEATHSIG, SIGKILL) == -1) @@ -787,7 +791,7 @@ pid_t startProcess(std::function fun, const ProcessOptions& options) { fun(); } catch (std::exception& e) { try { - std::cerr << options.errorPrefix << e.what() << "\n"; + LOG(ERROR) << options.errorPrefix << e.what(); } catch (...) { } } catch (...) { @@ -954,7 +958,7 @@ void closeMostFDs(const set& exceptions) { for (auto& s : readDirectory("/proc/self/fd")) { auto fd = std::stoi(s.name); if (!exceptions.count(fd)) { - debug("closing leaked FD %d", fd); + DLOG(INFO) << "closing leaked FD " << fd; close(fd); } } @@ -1111,7 +1115,7 @@ void ignoreException() { try { throw; } catch (std::exception& e) { - printError(format("error (ignored): %1%") % e.what()); + LOG(ERROR) << "error (ignored): " << e.what(); } } @@ -1223,7 +1227,7 @@ void callFailure(const std::function& failure, try { failure(exc); } catch (std::exception& e) { - printError(format("uncaught exception: %s") % e.what()); + LOG(ERROR) << "uncaught exception: " << e.what(); abort(); } } diff --git a/third_party/nix/src/libutil/util.hh b/third_party/nix/src/libutil/util.hh index 8a23090def97..25e647325c83 100644 --- a/third_party/nix/src/libutil/util.hh +++ b/third_party/nix/src/libutil/util.hh @@ -12,7 +12,6 @@ #include #include #include -#include "logging.hh" #include "types.hh" #ifndef HAVE_STRUCT_DIRENT_D_TYPE -- cgit 1.4.1