From: Remi Gacogne Date: Tue, 29 May 2018 10:20:31 +0000 (+0200) Subject: logger: Use a function-level static var for the logger object X-Git-Tag: rec-4.2.0-alpha1~41^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F6689%2Fhead;p=thirdparty%2Fpdns.git logger: Use a function-level static var for the logger object --- diff --git a/pdns/logger.cc b/pdns/logger.cc index b1b0487fe4..852d486e9b 100644 --- a/pdns/logger.cc +++ b/pdns/logger.cc @@ -31,9 +31,23 @@ extern StatBag S; #include "lock.hh" #include "namespaces.hh" -Logger g_log("", LOG_DAEMON); thread_local Logger::PerThread Logger::t_perThread; +Logger& getLogger() +{ + /* Since the Logger can be called very early, we need to make sure + that the relevant parts are initialized no matter what, which is tricky + because we can't easily control the initialization order, especially with + built-in backends. + t_perThread is thread_local, so it will be initialized when first accessed, + but we need to make sure that the object itself is initialized, and making + it a function-level static variable achieves that, because it will be + initialized the first time we enter this function at the very last. + */ + static Logger log("", LOG_DAEMON); + return log; +} + void Logger::log(const string &msg, Urgency u) { #ifndef RECURSOR diff --git a/pdns/logger.hh b/pdns/logger.hh index 544a4d69d9..200efc9319 100644 --- a/pdns/logger.hh +++ b/pdns/logger.hh @@ -115,7 +115,9 @@ private: bool d_prefixed{false}; }; -extern Logger g_log; +Logger& getLogger(); + +#define g_log getLogger() #ifdef VERBOSELOG #define DLOG(x) x