]> granicus.if.org Git - pdns/commitdiff
logger: Use a function-level static var for the logger object
authorRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 29 May 2018 10:20:31 +0000 (12:20 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 29 May 2018 10:20:31 +0000 (12:20 +0200)
pdns/logger.cc
pdns/logger.hh

index b1b0487fe443ba84e39f56433d4f3cbc9cce28cc..852d486e9bc46e1faf9be266168dcfd65dedff50 100644 (file)
@@ -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
index 544a4d69d979b1fe62287bdf136eefc90e7894f0..200efc9319c45ff0b194c02308bcedca08596744 100644 (file)
@@ -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