From 772dd39c586d174d36ba0d17b560a51cb63e8dbd Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Tue, 29 May 2018 12:20:31 +0200 Subject: [PATCH] logger: Use a function-level static var for the logger object --- pdns/logger.cc | 16 +++++++++++++++- pdns/logger.hh | 4 +++- 2 files changed, 18 insertions(+), 2 deletions(-) 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 -- 2.47.2