More descriptive message when we fail with a "Double registration of singleton" error
authorDhruv Matani <dhruvbird@fb.com>
Sat, 15 Aug 2015 15:37:23 +0000 (08:37 -0700)
committerfacebook-github-bot-1 <folly-bot@fb.com>
Sat, 15 Aug 2015 16:20:20 +0000 (09:20 -0700)
Summary: When we FATAL with a "Double registration of singleton" error, make it abundantly clear why such a thing could have happended so that the developer doesn't feel lost.

Reviewed By: @chipturner

Differential Revision: D2345728

folly/Singleton-inl.h

index 3848ab449145d56db1633c1c9e50aa2d7dd3a3eb..d3e6ea3024e7cd2313b4d399dec0280afe742c15 100644 (file)
@@ -32,7 +32,26 @@ void SingletonHolder<T>::registerSingleton(CreateFunc c, TeardownFunc t) {
   std::lock_guard<std::mutex> entry_lock(mutex_);
 
   if (state_ != SingletonHolderState::NotRegistered) {
-    LOG(FATAL) << "Double registration of singleton: " << type_.name();
+    /* Possible causes:
+     *
+     * You have two instances of the same
+     * folly::Singleton<Class>. Probably because you define the
+     * singleton in a header included in multiple places? In general,
+     * folly::Singleton shouldn't be in the header, only off in some
+     * anonymous namespace in a cpp file. Code needing the singleton
+     * will find it when that code references folly::Singleton<Class>.
+     *
+     * Alternatively, you could have 2 singletons with the same type
+     * defined with a different name in a .cpp (source) file. For
+     * example:
+     *
+     * Singleton<int> a([] { return new int(3); });
+     * Singleton<int> b([] { return new int(4); });
+     *
+     */
+    LOG(FATAL) << "Double registration of singletons of the same "
+               << "underlying type; check for multiple definitions "
+               << "of type folly::Singleton<" + type_.name() + ">";
   }
 
   create_ = std::move(c);