Skip to content

Commit

Permalink
log: add syslog support
Browse files Browse the repository at this point in the history
Add support to log using standard syslog,
add related option in dhtnode.
dhtnode will use syslog by default
when starting in daemon mode and
no logfile is defined.
  • Loading branch information
aberaud committed May 30, 2017
1 parent 3aa720f commit 39a254c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
5 changes: 3 additions & 2 deletions include/opendht/log.h
Expand Up @@ -22,8 +22,6 @@
#include "dhtrunner.h"

#include <iostream>
#include <fstream>
#include <chrono>

namespace dht {
namespace log {
Expand Down Expand Up @@ -73,5 +71,8 @@ enableFileLogging(dht::DhtRunner &dht, const std::string &path);
OPENDHT_PUBLIC void
disableLogging(dht::DhtRunner &dht);

OPENDHT_PUBLIC void
enableSyslog(dht::DhtRunner &dht, const char* name);

} /* log */
} /* dht */
39 changes: 36 additions & 3 deletions src/log.cpp
Expand Up @@ -19,6 +19,13 @@

#include "log.h"

#ifndef _WIN32
#include <syslog.h>
#endif

#include <fstream>
#include <chrono>

namespace dht {
namespace log {

Expand All @@ -37,9 +44,9 @@ printLog(std::ostream& s, char const *m, va_list args) {
using namespace std::chrono;
using log_precision = microseconds;
constexpr auto den = log_precision::period::den;
auto micro = duration_cast<log_precision>(steady_clock::now().time_since_epoch()).count();
s << "[" << std::setfill('0') << std::setw(6) << micro / den << "."
<< std::setfill('0') << std::setw(6) << micro % den << "]" << " ";
auto num = duration_cast<log_precision>(steady_clock::now().time_since_epoch()).count();
s << "[" << std::setfill('0') << std::setw(6) << num / den << "."
<< std::setfill('0') << std::setw(6) << num % den << "]" << " ";

// write log
s.write(buffer.data(), std::min((size_t) ret, buffer.size()));
Expand Down Expand Up @@ -77,6 +84,32 @@ enableFileLogging(dht::DhtRunner &dht, const std::string &path) {
);
}

OPENDHT_PUBLIC void
enableSyslog(dht::DhtRunner &dht, const char* name) {
#ifndef _WIN32
struct Syslog {
Syslog(const char* n) {
openlog(n, LOG_NDELAY, LOG_USER);
}
~Syslog() {
closelog();
}
};
// syslog is global. Existing instance must be reused.
static std::weak_ptr<Syslog> opened_logfile;
auto logfile = opened_logfile.lock();
if (not logfile) {
logfile = std::make_shared<Syslog>(name);
opened_logfile = logfile;
}
dht.setLoggers(
[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); }
);
#endif
}

void
disableLogging(dht::DhtRunner &dht) {
dht.setLoggers(dht::NOLOG, dht::NOLOG, dht::NOLOG);
Expand Down
4 changes: 3 additions & 1 deletion tools/dhtnode.cpp
Expand Up @@ -370,7 +370,9 @@ main(int argc, char **argv)
dht->run(params.port, crt, true, params.network);

if (params.log) {
if (not params.logfile.empty())
if (params.syslog or (params.daemonize and params.logfile.empty()))
log::enableSyslog(*dht, "dhtnode");
else if (not params.logfile.empty())
log::enableFileLogging(*dht, params.logfile);
else
log::enableLogging(*dht);
Expand Down
6 changes: 6 additions & 0 deletions tools/tools_common.h
Expand Up @@ -111,6 +111,7 @@ struct dht_params {
bool help {false}; // print help and exit
bool log {false};
std::string logfile {};
bool syslog {false};
in_port_t port {0};
dht::NetId network {0};
bool generate_identity {false};
Expand All @@ -127,6 +128,7 @@ static const constexpr struct option long_options[] = {
{"verbose", no_argument , nullptr, 'v'},
{"daemonize", no_argument , nullptr, 'd'},
{"logfile", required_argument, nullptr, 'l'},
{"syslog", no_argument , nullptr, 'L'},
{nullptr, 0 , nullptr, 0}
};

Expand Down Expand Up @@ -161,6 +163,10 @@ parseArgs(int argc, char **argv) {
case 'l':
params.logfile = optarg;
break;
case 'L':
params.log = true;
params.syslog = true;
break;
case 'v':
params.log = true;
break;
Expand Down

0 comments on commit 39a254c

Please sign in to comment.