You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by sw...@apache.org on 2024/04/29 02:03:56 UTC

(logging-log4cxx) branch master updated: Add a simple termination test using AsyncAppender (#375)

This is an automated email from the ASF dual-hosted git repository.

swebb2066 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git


The following commit(s) were added to refs/heads/master by this push:
     new 81ef59e9 Add a simple termination test using AsyncAppender (#375)
81ef59e9 is described below

commit 81ef59e97f6ee7476732e31a35d649cb19e288e2
Author: Stephen Webb <st...@ieee.org>
AuthorDate: Mon Apr 29 12:03:52 2024 +1000

    Add a simple termination test using AsyncAppender (#375)
---
 src/main/cpp/aprinitializer.cpp      |  6 ++++
 src/test/cpp/CMakeLists.txt          |  1 +
 src/test/cpp/terminationtestcase.cpp | 70 ++++++++++++++++++++++++++++++++++++
 3 files changed, 77 insertions(+)

diff --git a/src/main/cpp/aprinitializer.cpp b/src/main/cpp/aprinitializer.cpp
index 135f696a..f44e9864 100644
--- a/src/main/cpp/aprinitializer.cpp
+++ b/src/main/cpp/aprinitializer.cpp
@@ -25,6 +25,7 @@
 #include <apr_thread_proc.h>
 #include <log4cxx/helpers/filewatchdog.h>
 #include <log4cxx/helpers/date.h>
+#include <log4cxx/helpers/loglog.h>
 #include <list>
 #include <algorithm>
 
@@ -169,6 +170,11 @@ void APRInitializer::addObject(size_t key, const ObjectPtr& pObject)
 const ObjectPtr& APRInitializer::findOrAddObject(size_t key, std::function<ObjectPtr()> creator)
 {
 	std::lock_guard<std::mutex> lock(m_priv->mutex);
+	if (m_priv->objects.empty())
+	{
+		// Ensure the internal logger has a longer life than other Log4cxx static data
+		LogLog::debug(LOG4CXX_STR("Log4cxx starting"));
+	}
 	auto pItem = m_priv->objects.find(key);
 	if (m_priv->objects.end() == pItem)
 		pItem = m_priv->objects.emplace(key, creator()).first;
diff --git a/src/test/cpp/CMakeLists.txt b/src/test/cpp/CMakeLists.txt
index 47918f2b..19dcabb1 100644
--- a/src/test/cpp/CMakeLists.txt
+++ b/src/test/cpp/CMakeLists.txt
@@ -61,6 +61,7 @@ set(ALL_LOG4CXX_TESTS
     streamtestcase
     locationtest
     locationdisabledtest
+    terminationtestcase
 )
 if(${ENABLE_FMT_LAYOUT})
     set(ALL_LOG4CXX_TESTS ${ALL_LOG4CXX_TESTS} fmttest)
diff --git a/src/test/cpp/terminationtestcase.cpp b/src/test/cpp/terminationtestcase.cpp
new file mode 100644
index 00000000..eca9e2f0
--- /dev/null
+++ b/src/test/cpp/terminationtestcase.cpp
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "logunit.h"
+
+#include <log4cxx/logger.h>
+#include <log4cxx/logmanager.h>
+#include "vectorappender.h"
+#include <log4cxx/asyncappender.h>
+#include <thread>
+
+using namespace log4cxx;
+
+static VectorAppenderPtr vectorAppender = std::make_shared<VectorAppender>();
+
+LOGUNIT_CLASS(TerminationTestCase)
+{
+	LOGUNIT_TEST_SUITE(TerminationTestCase);
+	LOGUNIT_TEST(logOnce);
+	LOGUNIT_TEST_SUITE_END();
+
+public:
+
+	static void setDefaultAppender()
+	{
+		auto r = LogManager::getLoggerRepository();
+		r->ensureIsConfigured([r]()
+			{
+			auto asyncAppender = std::make_shared<AsyncAppender>();
+			asyncAppender->addAppender(vectorAppender);
+			r->getRootLogger()->addAppender(asyncAppender);
+			}
+		);
+	}
+
+	static LoggerPtr getLogger(const LogString& name = LogString())
+	{
+		static struct initializer
+		{
+			initializer() { setDefaultAppender(); }
+			~initializer() { LogManager::shutdown(); }
+		} x;
+		auto r = LogManager::getLoggerRepository();
+		return name.empty() ? r->getLogger(name) : r->getRootLogger();
+	}
+
+	void logOnce()
+	{
+		auto root = getLogger();
+		LOG4CXX_INFO(root, "Message");
+		std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
+		const std::vector<spi::LoggingEventPtr>& v = vectorAppender->getVector();
+		LOGUNIT_ASSERT_EQUAL((size_t) 1, v.size());
+	}
+};
+
+LOGUNIT_TEST_SUITE_REGISTRATION(TerminationTestCase);