Avoid thread-local storage in CLogCategoryList::fromClass

Use an ordinary function-local static variable instead.
This ensures we can still log messages after thread-local storage is cleaned up.
This commit is contained in:
Mat Sutcliffe
2020-03-28 22:05:38 +00:00
parent e8380173c9
commit 9dbe2f4018

View File

@@ -112,15 +112,16 @@ namespace BlackMisc
static const CLogCategoryList &fromClass() static const CLogCategoryList &fromClass()
{ {
static_assert(sizeof(T) > 0, "T must be a complete type, not forward declared"); static_assert(sizeof(T) > 0, "T must be a complete type, not forward declared");
static QThreadStorage<CLogCategoryList> list; //! \todo C++17: make list an inline static member variable template static const auto list = []
if (! list.hasLocalData())
{ {
list.localData().appendCategoriesFromMemberFunction(tag<T>(), THasGetLogCategories<T>()); CLogCategoryList list;
list.localData().appendCategoriesFromMetaType(tag<T>(), std::integral_constant<bool, QMetaTypeId<T>::Defined>()); list.appendCategoriesFromMemberFunction(tag<T>(), THasGetLogCategories<T>());
list.localData().appendCategoriesFromMetaObject(tag<T>(), std::is_base_of<QObject, T>()); list.appendCategoriesFromMetaType(tag<T>(), std::integral_constant<bool, QMetaTypeId<T>::Defined>());
if (list.localData().isEmpty()) { list.localData().push_back(CLogCategory::uncategorized()); } list.appendCategoriesFromMetaObject(tag<T>(), std::is_base_of<QObject, T>());
} if (list.isEmpty()) { list.push_back(CLogCategory::uncategorized()); }
return list.localData(); return list;
}();
return list;
} }
template <typename T> template <typename T>