refactor(3p/nix/libutil): Replace internal logging library with glog

This commit is contained in:
Vincent Ambo 2020-05-18 02:34:41 +01:00
parent c584480cd4
commit 6dc6c29fa4
10 changed files with 76 additions and 44 deletions

View file

@ -1,4 +1,5 @@
#include "affinity.hh"
#include <glog/logging.h>
#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
}

View file

@ -10,6 +10,7 @@
#include <map>
#include <vector>
#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);

View file

@ -7,7 +7,7 @@
#include <cstring>
#include <iostream>
#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?

View file

@ -1,6 +1,9 @@
#define GOOGLE_STRIP_LOG 0
#include "config.hh"
#include <glog/logging.h>
#include "args.hh"
#include "json.hh"
// #include <glog/log_severity.h>
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() {

View file

@ -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(

View file

@ -3,6 +3,7 @@
#include <cerrno>
#include <cstring>
#include <memory>
#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) {

View file

@ -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 &&

View file

@ -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

View file

@ -18,6 +18,7 @@
#include <thread>
#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<void()> fun) {
pid_t startProcess(std::function<void()> 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<void()> 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<int>& 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<void(std::exception_ptr exc)>& failure,
try {
failure(exc);
} catch (std::exception& e) {
printError(format("uncaught exception: %s") % e.what());
LOG(ERROR) << "uncaught exception: " << e.what();
abort();
}
}

View file

@ -12,7 +12,6 @@
#include <map>
#include <optional>
#include <sstream>
#include "logging.hh"
#include "types.hh"
#ifndef HAVE_STRUCT_DIRENT_D_TYPE