diff --git a/src/manager.cpp b/src/manager.cpp index c70f87e504dbefca5381e92d087043f5d8f963bc..12ef2550e373f2661f6de7bed1ecd541f0216d25 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 54c17a736ce79a136d7b9c09827edf8f3967433b..2734334847725ac75abbac76e5ac8c367f81903d 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 36a036d194fa5f8930799e2dd09ca6f7f113a28f..edabcc145b9d5852c738fb88aacf7ffdd4541917 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());