You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rm...@apache.org on 2022/11/10 23:58:17 UTC

[logging-log4cxx] branch next_stable updated: Keep track of appender even if not used (#147)

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

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


The following commit(s) were added to refs/heads/next_stable by this push:
     new 939ab764 Keep track of appender even if not used (#147)
939ab764 is described below

commit 939ab764ee7fe074fee09c3f07c03d11d44e86c3
Author: Robert Middleton <rm...@users.noreply.github.com>
AuthorDate: Thu Nov 10 18:58:11 2022 -0500

    Keep track of appender even if not used (#147)
---
 src/main/cpp/fallbackerrorhandler.cpp | 11 +++++++++++
 src/main/cpp/hierarchy.cpp            | 11 +++++++++++
 src/main/include/log4cxx/hierarchy.h  |  4 ++++
 3 files changed, 26 insertions(+)

diff --git a/src/main/cpp/fallbackerrorhandler.cpp b/src/main/cpp/fallbackerrorhandler.cpp
index 91b6dddd..df650d0a 100644
--- a/src/main/cpp/fallbackerrorhandler.cpp
+++ b/src/main/cpp/fallbackerrorhandler.cpp
@@ -22,6 +22,8 @@
 #include <log4cxx/helpers/loglog.h>
 #include <log4cxx/helpers/stringhelper.h>
 #include <log4cxx/spi/loggingevent.h>
+#include <log4cxx/hierarchy.h>
+#include <log4cxx/logmanager.h>
 
 using namespace log4cxx;
 using namespace log4cxx::helpers;
@@ -103,6 +105,15 @@ void FallbackErrorHandler::setBackupAppender(const AppenderPtr& backup1)
 	LogLog::debug(((LogString) LOG4CXX_STR("FB: Setting backup appender to ["))
 		+ backup1->getName() + LOG4CXX_STR("]."));
 	m_priv->backup = backup1;
+
+	// Make sure that we keep a reference to the appender around, since otherwise
+	// the appender would be lost if it has no loggers that use it.
+	LoggerRepository* repository = LogManager::getRootLogger()->getLoggerRepository();
+	Hierarchy* hierarchy = dynamic_cast<Hierarchy*>(repository);
+	if(hierarchy){
+		hierarchy->addAppender(backup1);
+	}
+
 }
 
 void FallbackErrorHandler::activateOptions(Pool&)
diff --git a/src/main/cpp/hierarchy.cpp b/src/main/cpp/hierarchy.cpp
index b8c0c0a9..98c4ba6c 100644
--- a/src/main/cpp/hierarchy.cpp
+++ b/src/main/cpp/hierarchy.cpp
@@ -70,6 +70,7 @@ struct Hierarchy::HierarchyPrivate
 	LoggerMap loggers;
 	ProvisionNodeMap provisionNodes;
 
+	std::vector<AppenderPtr> allAppenders;
 };
 
 IMPLEMENT_LOG4CXX_OBJECT(Hierarchy)
@@ -469,3 +470,13 @@ HierarchyPtr Hierarchy::create()
 	HierarchyPtr ret(new Hierarchy);
 	return ret;
 }
+
+void Hierarchy::clearAppenders()
+{
+	m_priv->allAppenders.clear();
+}
+
+void Hierarchy::addAppender(AppenderPtr appender)
+{
+	m_priv->allAppenders.push_back(appender);
+}
diff --git a/src/main/include/log4cxx/hierarchy.h b/src/main/include/log4cxx/hierarchy.h
index 058dfdfe..153e2e62 100644
--- a/src/main/include/log4cxx/hierarchy.h
+++ b/src/main/include/log4cxx/hierarchy.h
@@ -219,6 +219,10 @@ class LOG4CXX_EXPORT Hierarchy :
 		*/
 		void updateChildren(const Logger* parent);
 
+		void clearAppenders();
+
+		void addAppender(AppenderPtr appender);
+
 	private:
 
 		/**