dht: add logger to constructor

This commit is contained in:
Adrien Béraud committed 2019-05-22 00:47:25 -04:00
1 parent 37b12c114d
commit c7e2483160
13 files changed
+72 -34

No files matched your search

-1
View File
@@ -22,7 +22,6 @@
#ifdef OPENDHT_PROXY_SERVER
#include "opendht/dht_proxy_server.h"
#endif
#include "opendht/log.h"
#include "opendht/default_types.h"
#ifdef OPENDHT_INDEXATION
#include "opendht/indexation/pht.h"
+1 -1
View File
@@ -68,7 +68,7 @@ public:
* Initialise the Dht with two open sockets (for IPv4 and IP6)
* and an ID for the node.
*/
Dht(const int& s, const int& s6, const Config& config);
Dht(const int& s, const int& s6, const Config& config, const Logger& l = {});
virtual ~Dht();
/**
+5
View File
@@ -26,6 +26,7 @@ namespace dht {
class OPENDHT_PUBLIC DhtInterface {
public:
DhtInterface() = default;
DhtInterface(const Logger& l) : DHT_LOG(l) {};
virtual ~DhtInterface() = default;
// [[deprecated]]
@@ -221,6 +222,10 @@ public:
DHT_LOG.ERR = error;
}
virtual void setLogger(const Logger& l) {
DHT_LOG = l;
}
/**
* Only print logs related to the given InfoHash (if given), or disable filter (if zeroes).
*/
+1 -1
View File
@@ -44,7 +44,7 @@ public:
DhtProxyClient();
explicit DhtProxyClient(std::function<void()> loopSignal, const std::string& serverHost, const std::string& pushClientId = "");
explicit DhtProxyClient(std::function<void()> loopSignal, const std::string& serverHost, const std::string& pushClientId = "", const Logger& = {});
virtual void setPushNotificationToken(const std::string& token) {
#ifdef OPENDHT_PUSH_NOTIFICATIONS
+1
View File
@@ -322,6 +322,7 @@ public:
std::vector<ValuesExport> exportValues() const;
void setLogger(const Logger& logger = {});
void setLoggers(LogMethod err = NOLOG, LogMethod warn = NOLOG, LogMethod debug = NOLOG);
/**
+9
View File
@@ -66,6 +66,15 @@ constexpr const Color::Modifier yellow(Color::FG_YELLOW);
OPENDHT_PUBLIC void
printLog(std::ostream &s, char const *m, va_list args);
OPENDHT_PUBLIC
std::unique_ptr<Logger> getStdLogger();
OPENDHT_PUBLIC
std::unique_ptr<Logger> getFileLogger(const std::string &path);
OPENDHT_PUBLIC
std::unique_ptr<Logger> getSyslogLogger(const char* name);
OPENDHT_PUBLIC void
enableLogging(dht::DhtRunner &dht);
+3
View File
@@ -88,6 +88,9 @@ struct Logger {
LogMethod DBG = NOLOG;
LogMethod WARN = NOLOG;
LogMethod ERR = NOLOG;
Logger() = default;
Logger(LogMethod&& err, LogMethod&& warn, LogMethod&& dbg)
: DBG(std::move(dbg)), WARN(std::move(warn)), ERR(std::move(err)) {}
void setFilter(const InfoHash& f) {
filter_ = f;
filterEnable_ = static_cast<bool>(filter_);
+2 -2
View File
@@ -1691,8 +1691,8 @@ Dht::~Dht()
Dht::Dht() : store(), network_engine(DHT_LOG, scheduler) {}
Dht::Dht(const int& s, const int& s6, const Config& config)
: myid(config.node_id ? config.node_id : InfoHash::getRandom()), store(), store_quota(),
Dht::Dht(const int& s, const int& s6, const Config& config, const Logger& l)
: DhtInterface(l), myid(config.node_id ? config.node_id : InfoHash::getRandom()), store(), store_quota(),
network_engine(myid, config.network, s, s6, DHT_LOG, scheduler,
std::bind(&Dht::onError, this, _1, _2),
std::bind(&Dht::onNewNode, this, _1, _2),
+2 -2
View File
@@ -72,8 +72,8 @@ struct DhtProxyClient::ProxySearch {
DhtProxyClient::DhtProxyClient() {}
DhtProxyClient::DhtProxyClient(std::function<void()> signal, const std::string& serverHost, const std::string& pushClientId)
: serverHost_(serverHost), pushClientId_(pushClientId), loopSignal_(signal)
DhtProxyClient::DhtProxyClient(std::function<void()> signal, const std::string& serverHost, const std::string& pushClientId, const Logger& l)
: DhtInterface(l), serverHost_(serverHost), pushClientId_(pushClientId), loopSignal_(signal)
{
if (serverHost_.find("://") == std::string::npos)
serverHost_ = proxy::HTTP_PROTO + serverHost_;
+12 -10
View File
@@ -105,19 +105,15 @@ DhtRunner::run(const SockAddr& local4, const SockAddr& local6, const DhtRunner::
return;
startNetwork(local4, local6);
auto dht = std::unique_ptr<DhtInterface>(new Dht(s4, s6, SecureDht::getConfig(config.dht_config)));
auto dht = std::unique_ptr<DhtInterface>(new Dht(s4, s6, SecureDht::getConfig(config.dht_config), context.logger ? *context.logger : Logger{}));
dht_ = std::unique_ptr<SecureDht>(new SecureDht(std::move(dht), config.dht_config));
#ifdef OPENDHT_PROXY_CLIENT
config_ = config;
#endif
enableProxy(not config.proxy_server.empty());
if (context.logger) {
if (dht_)
dht_->setLoggers(context.logger->ERR, context.logger->WARN, context.logger->DBG);
if (dht_via_proxy_)
dht_via_proxy_->setLoggers(context.logger->ERR, context.logger->WARN, context.logger->DBG);
if (context.logger and dht_via_proxy_) {
dht_via_proxy_->setLogger(*context.logger);
}
running = true;
@@ -297,16 +293,22 @@ DhtRunner::exportValues() const {
}
void
DhtRunner::setLoggers(LogMethod error, LogMethod warn, LogMethod debug) {
DhtRunner::setLogger(const Logger& logger) {
std::lock_guard<std::mutex> lck(dht_mtx);
if (dht_)
dht_->setLoggers(error, warn, debug);
dht_->setLogger(logger);
#ifdef OPENDHT_PROXY_CLIENT
if (dht_via_proxy_)
dht_via_proxy_->setLoggers(error, warn, debug);
dht_via_proxy_->setLogger(logger);
#endif
}
void
DhtRunner::setLoggers(LogMethod error, LogMethod warn, LogMethod debug) {
Logger logger {std::move(error), std::move(warn), std::move(debug)};
setLogger(logger);
}
void
DhtRunner::setLogFilter(const InfoHash& f) {
std::lock_guard<std::mutex> lck(dht_mtx);
+29 -12
View File
@@ -55,9 +55,9 @@ printLog(std::ostream& s, char const *m, va_list args) {
s << std::endl;
}
void
enableLogging(dht::DhtRunner &dht) {
dht.setLoggers(
std::unique_ptr<Logger>
getStdLogger() {
return std::unique_ptr<Logger>(new Logger(
[](char const *m, va_list args) {
std::cerr << red;
printLog(std::cerr, m, args);
@@ -69,23 +69,23 @@ enableLogging(dht::DhtRunner &dht) {
std::cout << def;
},
[](char const *m, va_list args) { printLog(std::cout, m, args); }
);
));
}
void
enableFileLogging(dht::DhtRunner &dht, const std::string &path) {
std::unique_ptr<Logger>
getFileLogger(const std::string &path) {
auto logfile = std::make_shared<std::ofstream>();
logfile->open(path, std::ios::out);
dht.setLoggers(
return std::unique_ptr<Logger>(new Logger(
[=](char const *m, va_list args) { printLog(*logfile, m, args); },
[=](char const *m, va_list args) { printLog(*logfile, m, args); },
[=](char const *m, va_list args) { printLog(*logfile, m, args); }
);
));
}
OPENDHT_PUBLIC void
enableSyslog(dht::DhtRunner &dht, const char* name) {
std::unique_ptr<Logger>
getSyslogLogger(const char* name) {
#ifndef _WIN32
struct Syslog {
Syslog(const char* n) {
@@ -102,14 +102,31 @@ enableSyslog(dht::DhtRunner &dht, const char* name) {
logfile = std::make_shared<Syslog>(name);
opened_logfile = logfile;
}
dht.setLoggers(
return std::unique_ptr<Logger>(new Logger(
[logfile](char const *m, va_list args) { vsyslog(LOG_ERR, m, args); },
[logfile](char const *m, va_list args) { vsyslog(LOG_WARNING, m, args); },
[logfile](char const *m, va_list args) { vsyslog(LOG_INFO, m, args); }
);
));
#else
return std::unique_ptr<Logger>(new Logger());
#endif
}
void
enableLogging(dht::DhtRunner &dht) {
dht.setLogger(*getStdLogger());
}
void
enableFileLogging(dht::DhtRunner &dht, const std::string &path) {
dht.setLogger(*getFileLogger(path));
}
OPENDHT_PUBLIC void
enableSyslog(dht::DhtRunner &dht, const char* name) {
dht.setLogger(*getSyslogLogger(name));
}
void
disableLogging(dht::DhtRunner &dht) {
dht.setLoggers(dht::NOLOG, dht::NOLOG, dht::NOLOG);
+6 -5
View File
@@ -529,17 +529,18 @@ main(int argc, char **argv)
if (not params.proxyclient.empty())
node->setPushNotificationToken(params.devicekey);
node->run(params.port, config);
dht::DhtRunner::Context context {};
if (params.log) {
if (params.syslog or (params.daemonize and params.logfile.empty()))
log::enableSyslog(*node, "dhtnode");
context.logger = log::getSyslogLogger("dhtnode");
else if (not params.logfile.empty())
log::enableFileLogging(*node, params.logfile);
context.logger = log::getFileLogger(params.logfile);
else
log::enableLogging(*node);
context.logger = log::getStdLogger();
}
node->run(params.port, config, std::move(context));
if (not params.bootstrap.first.empty()) {
//std::cout << "Bootstrap: " << params.bootstrap.first << ":" << params.bootstrap.second << std::endl;
node->bootstrap(params.bootstrap.first.c_str(), params.bootstrap.second.c_str());
+1
View File
@@ -22,6 +22,7 @@
#pragma once
#include <opendht.h>
#include <opendht/log.h>
#ifndef WIN32_NATIVE
#include <getopt.h>
#include <readline/readline.h>