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 "affinity.hh"
#include <glog/logging.h>
#include "types.hh" #include "types.hh"
#include "util.hh" #include "util.hh"
@ -15,14 +16,18 @@ static cpu_set_t savedAffinity;
void setAffinityTo(int cpu) { void setAffinityTo(int cpu) {
#if __linux__ #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; didSaveAffinity = true;
debug(format("locking this thread to CPU %1%") % cpu); DLOG(INFO) << "locking this thread to CPU " << cpu;
cpu_set_t newAffinity; cpu_set_t newAffinity;
CPU_ZERO(&newAffinity); CPU_ZERO(&newAffinity);
CPU_SET(cpu, &newAffinity); CPU_SET(cpu, &newAffinity);
if (sched_setaffinity(0, sizeof(cpu_set_t), &newAffinity) == -1) if (sched_setaffinity(0, sizeof(cpu_set_t), &newAffinity) == -1) {
printError(format("failed to lock thread to CPU %1%") % cpu); LOG(ERROR) << "failed to lock thread to CPU " << cpu;
}
#endif #endif
} }
@ -38,9 +43,13 @@ int lockToCurrentCPU() {
void restoreAffinity() { void restoreAffinity() {
#if __linux__ #if __linux__
if (!didSaveAffinity) return; if (!didSaveAffinity) {
if (sched_setaffinity(0, sizeof(cpu_set_t), &savedAffinity) == -1) return;
printError("failed to restore affinity %1%"); }
if (sched_setaffinity(0, sizeof(cpu_set_t), &savedAffinity) == -1) {
LOG(ERROR) << "failed to restore affinity";
}
#endif #endif
} }

View file

@ -10,6 +10,7 @@
#include <map> #include <map>
#include <vector> #include <vector>
#include "config.hh" #include "config.hh"
#include "glog/logging.h"
#include "util.hh" #include "util.hh"
namespace nix { namespace nix {
@ -61,8 +62,9 @@ static void dump(const Path& path, Sink& sink, PathFilter& filter) {
checkInterrupt(); checkInterrupt();
struct stat st; struct stat st;
if (lstat(path.c_str(), &st)) if (lstat(path.c_str(), &st)) {
throw SysError(format("getting attributes of path '%1%'") % path); throw SysError(format("getting attributes of path '%1%'") % path);
}
sink << "("; sink << "(";
@ -87,8 +89,9 @@ static void dump(const Path& path, Sink& sink, PathFilter& filter) {
string name(i.name); string name(i.name);
size_t pos = i.name.find(caseHackSuffix); size_t pos = i.name.find(caseHackSuffix);
if (pos != string::npos) { if (pos != string::npos) {
debug(format("removing case hack suffix from '%1%'") % DLOG(INFO) << "removing case hack suffix from " << path << "/"
(path + "/" + i.name)); << i.name;
name.erase(pos); name.erase(pos);
} }
if (unhacked.find(name) != unhacked.end()) if (unhacked.find(name) != unhacked.end())
@ -247,15 +250,17 @@ static void parse(ParseSink& sink, Source& source, const Path& path) {
if (archiveSettings.useCaseHack) { if (archiveSettings.useCaseHack) {
auto i = names.find(name); auto i = names.find(name);
if (i != names.end()) { if (i != names.end()) {
debug(format("case collision between '%1%' and '%2%'") % DLOG(INFO) << "case collision between '" << i->first << "' and '"
i->first % name); << name << "'";
name += caseHackSuffix; name += caseHackSuffix;
name += std::to_string(++i->second); name += std::to_string(++i->second);
} else } else
names[name] = 0; names[name] = 0;
} }
} else if (s == "node") { } 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); parse(sink, source, path + "/" + name);
} else } else
throw badArchive("unknown field " + s); throw badArchive("unknown field " + s);

View file

@ -7,7 +7,7 @@
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include "finally.hh" #include "finally.hh"
#include "logging.hh" #include "glog/logging.h"
#include "util.hh" #include "util.hh"
namespace nix { namespace nix {
@ -217,16 +217,18 @@ struct XzCompressionSink : CompressionSink {
ret = lzma_stream_encoder_mt(&strm, &mt_options); ret = lzma_stream_encoder_mt(&strm, &mt_options);
done = true; done = true;
#else #else
printMsg(lvlError, LOG(ERROR) << "parallel XZ compression requested but not supported, "
"warning: parallel XZ compression requested but not supported, " << "falling back to single-threaded compression";
"falling back to single-threaded compression");
#endif #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"); throw CompressionError("unable to initialise lzma encoder");
}
// FIXME: apply the x86 BCJ filter? // FIXME: apply the x86 BCJ filter?

View file

@ -1,6 +1,9 @@
#define GOOGLE_STRIP_LOG 0
#include "config.hh" #include "config.hh"
#include <glog/logging.h>
#include "args.hh" #include "args.hh"
#include "json.hh" #include "json.hh"
// #include <glog/log_severity.h>
namespace nix { namespace nix {
@ -30,9 +33,12 @@ void Config::addSetting(AbstractSetting* setting) {
for (auto& alias : setting->aliases) { for (auto& alias : setting->aliases) {
auto i = unknownSettings.find(alias); auto i = unknownSettings.find(alias);
if (i != unknownSettings.end()) { if (i != unknownSettings.end()) {
if (set) if (set) {
warn("setting '%s' is set, but it's an alias of '%s' which is also set", LOG(WARNING) << "setting '" << alias
alias, setting->name); << "' is set, but it's an alias of '" << setting->name
<< "', which is also set";
}
else { else {
setting->set(i->second); setting->set(i->second);
setting->overriden = true; setting->overriden = true;
@ -44,7 +50,9 @@ void Config::addSetting(AbstractSetting* setting) {
} }
void AbstractConfig::warnUnknownSettings() { 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() { void AbstractConfig::reapplyUnknownSettings() {

View file

@ -37,6 +37,7 @@ libutil_headers = files(
) )
libutil_dep_list = [ libutil_dep_list = [
glog_dep,
boost_dep, boost_dep,
libbz2_dep, libbz2_dep,
liblzma_dep, liblzma_dep,
@ -47,7 +48,6 @@ libutil_dep_list = [
libutil_link_list = [] libutil_link_list = []
libutil_link_args = [] libutil_link_args = []
libutil_cxx_args = []
libutil_lib = library( libutil_lib = library(
'nixutil', 'nixutil',
@ -57,6 +57,7 @@ libutil_lib = library(
include_directories : src_inc, include_directories : src_inc,
sources : libutil_src, sources : libutil_src,
link_args : libutil_link_args, link_args : libutil_link_args,
# cpp_args : [ '-E' ],
dependencies : libutil_dep_list) dependencies : libutil_dep_list)
install_headers( install_headers(

View file

@ -3,6 +3,7 @@
#include <cerrno> #include <cerrno>
#include <cstring> #include <cstring>
#include <memory> #include <memory>
#include "glog/logging.h"
#include "util.hh" #include "util.hh"
namespace nix { namespace nix {
@ -47,9 +48,8 @@ FdSink::~FdSink() {
size_t threshold = 256 * 1024 * 1024; size_t threshold = 256 * 1024 * 1024;
static void warnLargeDump() { static void warnLargeDump() {
printError( LOG(WARNING)
"warning: dumping very large path (> 256 MiB); this may run out of " << "dumping very large path (> 256 MiB); this may run out of memory";
"memory");
} }
void FdSink::write(const unsigned char* data, size_t len) { void FdSink::write(const unsigned char* data, size_t len) {

View file

@ -1,5 +1,6 @@
#include "thread-pool.hh" #include "thread-pool.hh"
#include "affinity.hh" #include "affinity.hh"
#include "glog/logging.h"
namespace nix { namespace nix {
@ -11,7 +12,7 @@ ThreadPool::ThreadPool(size_t _maxThreads) : maxThreads(_maxThreads) {
if (!maxThreads) maxThreads = 1; if (!maxThreads) maxThreads = 1;
} }
debug("starting pool of %d threads", maxThreads - 1); DLOG(INFO) << "starting pool of " << maxThreads - 1 << " threads";
} }
ThreadPool::~ThreadPool() { shutdown(); } ThreadPool::~ThreadPool() { shutdown(); }
@ -26,18 +27,21 @@ void ThreadPool::shutdown() {
if (workers.empty()) return; if (workers.empty()) return;
debug("reaping %d worker threads", workers.size()); DLOG(INFO) << "reaping " << workers.size() << " worker threads";
work.notify_all(); work.notify_all();
for (auto& thr : workers) thr.join(); for (auto& thr : workers) {
thr.join();
}
} }
void ThreadPool::enqueue(const work_t& t) { void ThreadPool::enqueue(const work_t& t) {
auto state(state_.lock()); auto state(state_.lock());
if (quit) if (quit) {
throw ThreadPoolShutDown( throw ThreadPoolShutDown(
"cannot enqueue a work item while the thread pool is shutting down"); "cannot enqueue a work item while the thread pool is shutting down");
}
state->pending.push(t); state->pending.push(t);
/* Note: process() also executes items, so count it as a worker. */ /* Note: process() also executes items, so count it as a worker. */
if (state->pending.size() > state->workers.size() + 1 && 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...)) {} BaseError(Args... args) : err(fmt(args...)) {}
#ifdef EXCEPTION_NEEDS_THROW_SPEC #ifdef EXCEPTION_NEEDS_THROW_SPEC
~BaseError() throw(){}; ~BaseError() noexcept {};
const char* what() const throw() { return err.c_str(); } const char* what() const noexcept { return err.c_str(); }
#else #else
const char* what() const noexcept { return err.c_str(); } const char* what() const noexcept { return err.c_str(); }
#endif #endif

View file

@ -18,6 +18,7 @@
#include <thread> #include <thread>
#include "affinity.hh" #include "affinity.hh"
#include "finally.hh" #include "finally.hh"
#include "glog/logging.h"
#include "lazy.hh" #include "lazy.hh"
#include "serialise.hh" #include "serialise.hh"
#include "sync.hh" #include "sync.hh"
@ -669,7 +670,7 @@ Pid::operator pid_t() { return pid; }
int Pid::kill() { int Pid::kill() {
assert(pid != -1); 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 /* Send the requested signal to the child. If it has its own
process group, send the signal to every process in the child process group, send the signal to every process in the child
@ -681,7 +682,7 @@ int Pid::kill() {
#if __FreeBSD__ || __APPLE__ #if __FreeBSD__ || __APPLE__
if (errno != EPERM || ::kill(pid, 0) != 0) if (errno != EPERM || ::kill(pid, 0) != 0)
#endif #endif
printError((SysError("killing process %d", pid).msg())); LOG(ERROR) << SysError("killing process %d", pid).msg();
} }
return wait(); return wait();
@ -696,7 +697,9 @@ int Pid::wait() {
pid = -1; pid = -1;
return status; return status;
} }
if (errno != EINTR) throw SysError("cannot get child exit status"); if (errno != EINTR) {
throw SysError("cannot get child exit status");
}
checkInterrupt(); checkInterrupt();
} }
} }
@ -712,7 +715,7 @@ pid_t Pid::release() {
} }
void killUser(uid_t uid) { 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... */ assert(uid != 0); /* just to be safe... */
@ -725,7 +728,9 @@ void killUser(uid_t uid) {
Pid pid = startProcess( Pid pid = startProcess(
[&]() { [&]() {
if (setuid(uid) == -1) throw SysError("setting uid"); if (setuid(uid) == -1) {
throw SysError("setting uid");
}
while (true) { while (true) {
#ifdef __APPLE__ #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) { pid_t startProcess(std::function<void()> fun, const ProcessOptions& options) {
auto wrapper = [&]() { auto wrapper = [&]() {
if (!options.allowVfork) logger = makeDefaultLogger();
try { try {
#if __linux__ #if __linux__
if (options.dieWithParent && prctl(PR_SET_PDEATHSIG, SIGKILL) == -1) if (options.dieWithParent && prctl(PR_SET_PDEATHSIG, SIGKILL) == -1)
@ -787,7 +791,7 @@ pid_t startProcess(std::function<void()> fun, const ProcessOptions& options) {
fun(); fun();
} catch (std::exception& e) { } catch (std::exception& e) {
try { try {
std::cerr << options.errorPrefix << e.what() << "\n"; LOG(ERROR) << options.errorPrefix << e.what();
} catch (...) { } catch (...) {
} }
} catch (...) { } catch (...) {
@ -954,7 +958,7 @@ void closeMostFDs(const set<int>& exceptions) {
for (auto& s : readDirectory("/proc/self/fd")) { for (auto& s : readDirectory("/proc/self/fd")) {
auto fd = std::stoi(s.name); auto fd = std::stoi(s.name);
if (!exceptions.count(fd)) { if (!exceptions.count(fd)) {
debug("closing leaked FD %d", fd); DLOG(INFO) << "closing leaked FD " << fd;
close(fd); close(fd);
} }
} }
@ -1111,7 +1115,7 @@ void ignoreException() {
try { try {
throw; throw;
} catch (std::exception& e) { } 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 { try {
failure(exc); failure(exc);
} catch (std::exception& e) { } catch (std::exception& e) {
printError(format("uncaught exception: %s") % e.what()); LOG(ERROR) << "uncaught exception: " << e.what();
abort(); abort();
} }
} }

View file

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