You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by GitBox <gi...@apache.org> on 2022/01/01 13:38:11 UTC

[GitHub] [logging-log4cxx] rm5248 commented on a change in pull request #93: Add a unit test for the automatic configuration feature

rm5248 commented on a change in pull request #93:
URL: https://github.com/apache/logging-log4cxx/pull/93#discussion_r776895762



##########
File path: src/main/cpp/aprinitializer.cpp
##########
@@ -127,3 +127,21 @@ void APRInitializer::unregisterCleanup(FileWatchdog* watchdog)
 	}
 }
 
+void APRInitializer::addObject(size_t key, const ObjectPtr& pObject)
+{
+#if APR_HAS_THREADS
+	std::unique_lock<std::mutex> lock(this->mutex);
+#endif
+    this->objects[key] = pObject;
+}
+
+const ObjectPtr& APRInitializer::findOrAddObject(size_t key, std::function<ObjectPtr()> creator)
+{
+#if APR_HAS_THREADS
+	std::unique_lock<std::mutex> lock(this->mutex);
+#endif
+    auto pItem = this->objects.find(key);
+    if (this->objects.end() == pItem)
+        pItem = this->objects.emplace(key, creator()).first;

Review comment:
       We should probably check to make sure that the `creator` function is in fact valid first.

##########
File path: src/main/cpp/logmanager.cpp
##########
@@ -47,28 +47,19 @@ using namespace log4cxx::helpers;
 IMPLEMENT_LOG4CXX_OBJECT(DefaultRepositorySelector)
 
 void* LogManager::guard = 0;
-spi::RepositorySelectorPtr LogManager::repositorySelector;
-
 
 RepositorySelectorPtr LogManager::getRepositorySelector()
 {
-	//
-	//     call to initialize APR and trigger "start" of logging clock
-	//
-	APRInitializer::initialize();
-
-	if (!repositorySelector)
-	{
-		LoggerRepositoryPtr hierarchy = Hierarchy::create();
-		RepositorySelectorPtr selector(new DefaultRepositorySelector(hierarchy));
-		repositorySelector = selector;
-	}
-
-	return repositorySelector;
+	auto result = APRInitializer::getUnique<spi::RepositorySelector>( []() -> ObjectPtr
+		{
+			LoggerRepositoryPtr hierarchy = Hierarchy::create();
+			return ObjectPtr(new DefaultRepositorySelector(hierarchy));
+		}
+	);

Review comment:
       This kinda broke my brain here.  If it is called `getUnique`, I would expect it to only get something, not try to also construct something.  The private method name(`findOrAddObject`) is much better, although perhaps the signature would make more sense as `getOrCreate<T>( std::function<T> create_fn )`?  It makes more sense to me to have the function be a parameter so that you know that you may or may not call it, but I don't know if other people feel the same way.

##########
File path: src/main/cpp/logmanager.cpp
##########
@@ -47,28 +47,19 @@ using namespace log4cxx::helpers;
 IMPLEMENT_LOG4CXX_OBJECT(DefaultRepositorySelector)
 
 void* LogManager::guard = 0;
-spi::RepositorySelectorPtr LogManager::repositorySelector;
-
 
 RepositorySelectorPtr LogManager::getRepositorySelector()
 {
-	//
-	//     call to initialize APR and trigger "start" of logging clock
-	//
-	APRInitializer::initialize();
-
-	if (!repositorySelector)
-	{
-		LoggerRepositoryPtr hierarchy = Hierarchy::create();
-		RepositorySelectorPtr selector(new DefaultRepositorySelector(hierarchy));
-		repositorySelector = selector;
-	}
-
-	return repositorySelector;
+	auto result = APRInitializer::getUnique<spi::RepositorySelector>( []() -> ObjectPtr
+		{
+			LoggerRepositoryPtr hierarchy = Hierarchy::create();
+			return ObjectPtr(new DefaultRepositorySelector(hierarchy));
+		}
+	);

Review comment:
       upon further looking at it the signature is a function, but that name is still confusing to me.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org