From c1f2673ac4e4037d606fe1a587a369486aba241a Mon Sep 17 00:00:00 2001 From: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com> Date: Mon, 24 Oct 2016 19:01:07 -0400 Subject: [PATCH] dht: add DHTLOGLEVEL env var to select DHT log This patch adds the support of DHTLOGLEVEL environment variable to let the user choose OpenDHT log level when the deamon instance is created. Following level are available: - 0 : disable all logs - 1 : only errors - 2 : level 1 + warnings - 3 : level 2 + debugs Change-Id: Ib55bdabdea2e3ac72c86fa78260a5e3d16b52179 Reviewed-by: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com> --- src/manager.cpp | 30 ++++++++++++++++++++++++++++++ src/manager.h | 2 ++ src/ringdht/ringaccount.cpp | 18 +++++++++++------- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/manager.cpp b/src/manager.cpp index c70f87e504..12ef2550e3 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -119,6 +119,34 @@ restore_backup(const std::string &path) copy_over(backup_path, path); } +/** + * Set OpenDHT's log level based on the DHTLOGLEVEL environment variable. + * DHTLOGLEVEL = 0 minimum logging (=disable) + * DHTLOGLEVEL = 1 (=ERROR only) + * DHTLOGLEVEL = 2 (+=WARN) + * DHTLOGLEVEL = 3 maximum logging (+=DEBUG) + */ + +/** Environment variable used to set OpenDHT's logging level */ +static constexpr const char* DHTLOGLEVEL = "DHTLOGLEVEL"; + +static void +setDhtLogLevel() +{ + char* envvar = getenv(DHTLOGLEVEL); + int level = 0; + + if (envvar != nullptr) { + if (not (std::istringstream(envvar) >> level)) + level = 0; + + // From 0 (min) to 3 (max) + level = std::max(0, std::min(level, 3)); + RING_DBG("DHTLOGLEVEL=%u", level); + } + Manager::instance().dhtLogLevel = level; +} + /** * Set pjsip's log level based on the SIPLOGLEVEL environment variable. * SIPLOGLEVEL = 0 minimum logging @@ -273,6 +301,8 @@ Manager::init(const std::string &config_file) setGnuTlsLogLevel(); RING_DBG("GNU TLS version %s initialized", gnutls_check_version(nullptr)); + setDhtLogLevel(); + ice_tf_.reset(new IceTransportFactory()); path_ = config_file.empty() ? retrieveConfigPath() : config_file; diff --git a/src/manager.h b/src/manager.h index 54c17a736c..2734334847 100644 --- a/src/manager.h +++ b/src/manager.h @@ -1015,6 +1015,8 @@ class Manager { VideoManager& getVideoManager() const { return *videoManager_; } #endif // RING_VIDEO + std::atomic<unsigned> dhtLogLevel {0}; // default = disable + private: NON_COPYABLE(Manager); diff --git a/src/ringdht/ringaccount.cpp b/src/ringdht/ringaccount.cpp index 36a036d194..edabcc145b 100644 --- a/src/ringdht/ringaccount.cpp +++ b/src/ringdht/ringaccount.cpp @@ -1492,13 +1492,17 @@ RingAccount::doRegister_() return ret; }); -#if 0 // enable if dht_ logging is needed - dht_.setLoggers( - [](char const* m, va_list args){ vlogger(LOG_ERR, m, args); }, - [](char const* m, va_list args){ vlogger(LOG_WARNING, m, args); }, - [](char const* m, va_list args){ /*vlogger(LOG_DEBUG, m, args);*/ } - ); -#endif + auto dht_log_level = Manager::instance().dhtLogLevel.load(); + if (dht_log_level > 0) { + static auto silent = [](char const* m, va_list args) {}; + static auto log_error = [](char const* m, va_list args) { vlogger(LOG_ERR, m, args); }; + static auto log_warn = [](char const* m, va_list args) { vlogger(LOG_WARNING, m, args); }; + static auto log_debug = [](char const* m, va_list args) { vlogger(LOG_DEBUG, m, args); }; + dht_.setLoggers( + log_error, + (dht_log_level > 1) ? log_warn : silent, + (dht_log_level > 2) ? log_debug : silent); + } dht_.importValues(loadValues()); -- GitLab