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 2021/08/21 03:45:55 UTC

[logging-log4cxx] branch LOGCXX-431 updated (3687a8b -> a82c4d9)

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

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


    from 3687a8b  Fix pthread name test for linux
     new 39bbf1b  Turned the thread utility into a singleton
     new 6e725a7  Made documentation on logging threaded clearer
     new 46ad073  setname actually tests properly
     new a82c4d9  Fixed documentation

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/main/cpp/asyncappender.cpp                   |  2 +-
 src/main/cpp/filewatchdog.cpp                    |  2 +-
 src/main/cpp/socketappenderskeleton.cpp          |  2 +-
 src/main/cpp/sockethubappender.cpp               |  2 +-
 src/main/cpp/telnetappender.cpp                  |  2 +-
 src/main/cpp/threadutility.cpp                   | 66 +++++++++++----------
 src/main/include/CMakeLists.txt                  |  4 +-
 src/main/include/log4cxx/helpers/threadutility.h | 73 ++++++++++++------------
 src/site/markdown/threading.md                   |  7 ++-
 9 files changed, 87 insertions(+), 73 deletions(-)

[logging-log4cxx] 03/04: setname actually tests properly

Posted by rm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 46ad073499f147d0b3d46e8f8f0a4dc033b12738
Author: Robert Middleton <ro...@rm5248.com>
AuthorDate: Fri Aug 20 23:42:18 2021 -0400

    setname actually tests properly
---
 src/main/include/CMakeLists.txt                  | 6 +++---
 src/main/include/log4cxx/helpers/threadutility.h | 5 ++---
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/src/main/include/CMakeLists.txt b/src/main/include/CMakeLists.txt
index 9905434..ce3b4e8 100644
--- a/src/main/include/CMakeLists.txt
+++ b/src/main/include/CMakeLists.txt
@@ -95,9 +95,9 @@ if(UNIX)
     # Since this is really not a core feature and the end-user can configure
     # it anyway, we're only going to worry about linux.
     include(${LOG4CXX_SOURCE_DIR}/src/cmake/pthread/log4cxx-pthread.cmake)
-    set(HAS_PTHREAD_SETNAME ${PTHREAD_SETNAME_NP_FOUND})
-
-    message("setname np: ${PTHREAD_SETNAME_NP_FOUND}")
+    if(${PTHREAD_SETNAME_NP_FOUND})
+	set(HAS_PTHREAD_SETNAME 1)
+    endif()
 endif(UNIX)
 
 if(WIN32)
diff --git a/src/main/include/log4cxx/helpers/threadutility.h b/src/main/include/log4cxx/helpers/threadutility.h
index 4111a0d..246e284 100644
--- a/src/main/include/log4cxx/helpers/threadutility.h
+++ b/src/main/include/log4cxx/helpers/threadutility.h
@@ -56,9 +56,8 @@ public:
 
 	/**
 	 * Configure the thread functions that log4cxx will use.
-	 * Note that setting any of these parameters to nullptr will cause the
-	 * default function to be used.
-	 *
+	 * Note that setting any of these parameters to nullptr is valid,
+	 * and simply results in the callback not being called.	 *
 	 */
 	void configureThreadFunctions( pre_thread_start pre_start,
 								   thread_started started,

[logging-log4cxx] 04/04: Fixed documentation

Posted by rm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit a82c4d9584a713ab4ef51cb6ee6a804b6083b1d2
Author: Robert Middleton <ro...@rm5248.com>
AuthorDate: Fri Aug 20 23:43:31 2021 -0400

    Fixed documentation
---
 src/site/markdown/threading.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/site/markdown/threading.md b/src/site/markdown/threading.md
index 6bf373e..daed9e1 100644
--- a/src/site/markdown/threading.md
+++ b/src/site/markdown/threading.md
@@ -68,7 +68,7 @@ In order to use these callback functions, use the ThreadUtility class.  You
 can use some sample functions(not no-ops) as follows:
 
 ```
-ThreadUtility::configureThreadFunctions( ThreadUtility::preThreadBlockSignals,
+ThreadUtility::instance()->configureThreadFunctions( ThreadUtility::preThreadBlockSignals,
 					 ThreadUtility::threadStartedNameThread,
 					 ThreadUtility::postThreadUnblockSignals );
 ```

[logging-log4cxx] 01/04: Turned the thread utility into a singleton

Posted by rm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 39bbf1b3bc17b60c7fc54c7f3e20ffa18ae2770d
Author: Robert Middleton <ro...@rm5248.com>
AuthorDate: Fri Aug 20 23:19:01 2021 -0400

    Turned the thread utility into a singleton
---
 src/main/cpp/asyncappender.cpp                   |  2 +-
 src/main/cpp/filewatchdog.cpp                    |  2 +-
 src/main/cpp/socketappenderskeleton.cpp          |  2 +-
 src/main/cpp/sockethubappender.cpp               |  2 +-
 src/main/cpp/telnetappender.cpp                  |  2 +-
 src/main/cpp/threadutility.cpp                   | 66 +++++++++++++----------
 src/main/include/CMakeLists.txt                  |  2 +
 src/main/include/log4cxx/helpers/threadutility.h | 68 +++++++++++++-----------
 8 files changed, 80 insertions(+), 66 deletions(-)

diff --git a/src/main/cpp/asyncappender.cpp b/src/main/cpp/asyncappender.cpp
index c9f87ad..7ab05b7 100644
--- a/src/main/cpp/asyncappender.cpp
+++ b/src/main/cpp/asyncappender.cpp
@@ -51,7 +51,7 @@ AsyncAppender::AsyncAppender()
 	  locationInfo(false),
 	  blocking(true)
 {
-	dispatcher = ThreadUtility::createThread( LOG4CXX_STR("AyncAppend"), &AsyncAppender::dispatch, this );
+	dispatcher = ThreadUtility::instance()->createThread( LOG4CXX_STR("AyncAppend"), &AsyncAppender::dispatch, this );
 }
 
 AsyncAppender::~AsyncAppender()
diff --git a/src/main/cpp/filewatchdog.cpp b/src/main/cpp/filewatchdog.cpp
index 14942cd..3848ed3 100644
--- a/src/main/cpp/filewatchdog.cpp
+++ b/src/main/cpp/filewatchdog.cpp
@@ -93,7 +93,7 @@ void FileWatchdog::start()
 {
 	checkAndConfigure();
 
-	thread = ThreadUtility::createThread( LOG4CXX_STR("Filewatchdog"), &FileWatchdog::run, this );
+	thread = ThreadUtility::instance()->createThread( LOG4CXX_STR("Filewatchdog"), &FileWatchdog::run, this );
 }
 
 bool FileWatchdog::is_interrupted()
diff --git a/src/main/cpp/socketappenderskeleton.cpp b/src/main/cpp/socketappenderskeleton.cpp
index a310cba..e3e1375 100644
--- a/src/main/cpp/socketappenderskeleton.cpp
+++ b/src/main/cpp/socketappenderskeleton.cpp
@@ -163,7 +163,7 @@ void SocketAppenderSkeleton::fireConnector()
 	{
 		LogLog::debug(LOG4CXX_STR("Connector thread not alive: starting monitor."));
 
-		thread = ThreadUtility::createThread( LOG4CXX_STR("SocketAppend"), &SocketAppenderSkeleton::monitor, this );
+		thread = ThreadUtility::instance()->createThread( LOG4CXX_STR("SocketAppend"), &SocketAppenderSkeleton::monitor, this );
 	}
 }
 
diff --git a/src/main/cpp/sockethubappender.cpp b/src/main/cpp/sockethubappender.cpp
index 73d821a..7d83a23 100644
--- a/src/main/cpp/sockethubappender.cpp
+++ b/src/main/cpp/sockethubappender.cpp
@@ -178,7 +178,7 @@ void SocketHubAppender::append(const spi::LoggingEventPtr& event, Pool& p)
 
 void SocketHubAppender::startServer()
 {
-	thread = ThreadUtility::createThread( LOG4CXX_STR("SocketHub"), &SocketHubAppender::monitor, this );
+	thread = ThreadUtility::instance()->createThread( LOG4CXX_STR("SocketHub"), &SocketHubAppender::monitor, this );
 }
 
 void SocketHubAppender::monitor()
diff --git a/src/main/cpp/telnetappender.cpp b/src/main/cpp/telnetappender.cpp
index 9dc813a..7e1d6f5 100644
--- a/src/main/cpp/telnetappender.cpp
+++ b/src/main/cpp/telnetappender.cpp
@@ -62,7 +62,7 @@ void TelnetAppender::activateOptions(Pool& /* p */)
 		serverSocket->setSoTimeout(1000);
 	}
 
-	sh = ThreadUtility::createThread( LOG4CXX_STR("TelnetAppend"), &TelnetAppender::acceptConnections, this );
+	sh = ThreadUtility::instance()->createThread( LOG4CXX_STR("TelnetAppend"), &TelnetAppender::acceptConnections, this );
 }
 
 void TelnetAppender::setOption(const LogString& option,
diff --git a/src/main/cpp/threadutility.cpp b/src/main/cpp/threadutility.cpp
index c7990d9..165803b 100644
--- a/src/main/cpp/threadutility.cpp
+++ b/src/main/cpp/threadutility.cpp
@@ -11,35 +11,36 @@
 
 using log4cxx::ThreadUtility;
 
-log4cxx::pre_thread_start ThreadUtility::pre_start = ThreadUtility::preThreadDoNothing;
-log4cxx::thread_started ThreadUtility::thread_start = ThreadUtility::threadStartedDoNothing;
-log4cxx::post_thread_start ThreadUtility::post_start = ThreadUtility::postThreadDoNothing;
+struct ThreadUtility::priv_data{
+	priv_data(){
+		pre_start = nullptr;
+		thread_start = nullptr;
+		post_start = nullptr;
+	}
 
-ThreadUtility::ThreadUtility(){}
+	log4cxx::pre_thread_start pre_start;
+	log4cxx::thread_started thread_start;
+	log4cxx::post_thread_start post_start;
+};
 
-void ThreadUtility::configureThreadFunctions( pre_thread_start pre_start1,
-							   thread_started started,
-							   post_thread_start post_start1 ){
-	if( pre_start1 == nullptr ){
-		pre_start = ThreadUtility::preThreadDoNothing;
-	}else{
-		pre_start = pre_start1;
-	}
+ThreadUtility::ThreadUtility() :
+	m_priv( new priv_data() )
+{}
 
-	if( started == nullptr ){
-		thread_start = ThreadUtility::threadStartedDoNothing;
-	}else{
-		thread_start = started;
-	}
+ThreadUtility::~ThreadUtility(){}
 
-	if( post_start1 == nullptr ){
-		post_start = ThreadUtility::postThreadDoNothing;
-	}else{
-		post_start = pre_start1;
-	}
+std::shared_ptr<ThreadUtility> ThreadUtility::instance(){
+	static std::shared_ptr<ThreadUtility> instance( new ThreadUtility() );
+	return instance;
 }
 
-void ThreadUtility::preThreadDoNothing(){}
+void ThreadUtility::configureThreadFunctions( pre_thread_start pre_start1,
+							   thread_started started,
+							   post_thread_start post_start1 ){
+	m_priv->pre_start = pre_start1;
+	m_priv->thread_start = started;
+	m_priv->post_start = post_start1;
+}
 
 void ThreadUtility::preThreadBlockSignals(){
 #if LOG4CXX_HAS_PTHREAD_SIGMASK
@@ -51,10 +52,6 @@ void ThreadUtility::preThreadBlockSignals(){
 #endif /* LOG4CXX_HAS_PTHREAD_SIGMASK */
 }
 
-void ThreadUtility::threadStartedDoNothing(LogString,
-							std::thread::id,
-							std::thread::native_handle_type){}
-
 void ThreadUtility::threadStartedNameThread(LogString threadName,
 							 std::thread::id /*thread_id*/,
 							 std::thread::native_handle_type native_handle){
@@ -70,8 +67,6 @@ void ThreadUtility::threadStartedNameThread(LogString threadName,
 #endif
 }
 
-void ThreadUtility::postThreadDoNothing(){}
-
 void ThreadUtility::postThreadUnblockSignals(){
 #if LOG4CXX_HAS_PTHREAD_SIGMASK
 	sigset_t set;
@@ -81,3 +76,16 @@ void ThreadUtility::postThreadUnblockSignals(){
 	}
 #endif /* LOG4CXX_HAS_PTHREAD_SIGMASK */
 }
+
+
+log4cxx::pre_thread_start ThreadUtility::preStartFunction(){
+	return m_priv->pre_start;
+}
+
+log4cxx::thread_started ThreadUtility::threadStartedFunction(){
+	return m_priv->thread_start;
+}
+
+log4cxx::post_thread_start ThreadUtility::postStartFunction(){
+	return m_priv->post_start;
+}
diff --git a/src/main/include/CMakeLists.txt b/src/main/include/CMakeLists.txt
index 558eaeb..9905434 100644
--- a/src/main/include/CMakeLists.txt
+++ b/src/main/include/CMakeLists.txt
@@ -96,6 +96,8 @@ if(UNIX)
     # it anyway, we're only going to worry about linux.
     include(${LOG4CXX_SOURCE_DIR}/src/cmake/pthread/log4cxx-pthread.cmake)
     set(HAS_PTHREAD_SETNAME ${PTHREAD_SETNAME_NP_FOUND})
+
+    message("setname np: ${PTHREAD_SETNAME_NP_FOUND}")
 endif(UNIX)
 
 if(WIN32)
diff --git a/src/main/include/log4cxx/helpers/threadutility.h b/src/main/include/log4cxx/helpers/threadutility.h
index de4ffad..4111a0d 100644
--- a/src/main/include/log4cxx/helpers/threadutility.h
+++ b/src/main/include/log4cxx/helpers/threadutility.h
@@ -3,6 +3,7 @@
 
 #include <thread>
 #include <functional>
+#include <memory>
 
 #include "log4cxx/logstring.h"
 
@@ -34,79 +35,82 @@ typedef std::function<void( LogString threadName,
  */
 typedef std::function<void()> post_thread_start;
 
+class ThreadUtility;
+LOG4CXX_PTR_DEF(ThreadUtility);
+
 class LOG4CXX_EXPORT ThreadUtility {
+private:
+	ThreadUtility();
+
+	log4cxx::pre_thread_start preStartFunction();
+	log4cxx::thread_started threadStartedFunction();
+	log4cxx::post_thread_start postStartFunction();
+
+	struct priv_data;
+	std::unique_ptr<priv_data> m_priv;
+
 public:
+	~ThreadUtility();
+
+	static std::shared_ptr<ThreadUtility> instance();
+
 	/**
 	 * Configure the thread functions that log4cxx will use.
 	 * Note that setting any of these parameters to nullptr will cause the
 	 * default function to be used.
 	 *
 	 */
-	static void configureThreadFunctions( pre_thread_start pre_start,
+	void configureThreadFunctions( pre_thread_start pre_start,
 								   thread_started started,
 								   post_thread_start post_start );
 
 	/**
-	 * A pre-start thread function that does nothing
-	 */
-	static void preThreadDoNothing();
-
-	/**
 	 * A pre-start thread function that blocks signals to the new thread
 	 * (if the system has pthreads).  If the system does not have pthreads,
 	 * is equivalent to preThreadDoNothing();
 	 */
-	static void preThreadBlockSignals();
-
-	/**
-	 * A thread_started function that does nothing when the thread starts.
-	 */
-	static void threadStartedDoNothing(LogString threadName,
-								std::thread::id thread_id,
-								std::thread::native_handle_type native_handle);
+	void preThreadBlockSignals();
 
 	/**
 	 * A thread_started function that names the thread using the appropriate
 	 * system call.
 	 */
-	static void threadStartedNameThread(LogString threadName,
+	void threadStartedNameThread(LogString threadName,
 								 std::thread::id thread_id,
 								 std::thread::native_handle_type native_handle);
 
 	/**
-	 * A post-start thread function that does nothing.
-	 */
-	static void postThreadDoNothing();
-
-	/**
 	 * A post-start thread function that unblocks signals that preThreadBlockSignals
 	 * blocked before starting the thread.  If the system does not have pthreads,
 	 * is equivalent to postThreadDoNothing();
 	 */
-	static void postThreadUnblockSignals();
+	void postThreadUnblockSignals();
 
 	/**
 	 * Start a thread
 	 */
 	template<class Function, class... Args>
-	static std::thread createThread(LogString name,
+	std::thread createThread(LogString name,
 							 Function&& f,
 							 Args&&... args){
-		pre_start();
+		log4cxx::pre_thread_start pre_start = preStartFunction();
+		log4cxx::thread_started thread_start = threadStartedFunction();
+		log4cxx::post_thread_start post_start = postStartFunction();
+
+		if( pre_start ){
+			pre_start();
+		}
 		std::thread t( f, args... );
-		thread_start( name,
+		if( thread_start ){
+			thread_start( name,
 					  t.get_id(),
 					  t.native_handle() );
-		post_start();
+		}
+		if( post_start ){
+			post_start();
+		}
 		return t;
 	}
-
-private:
-	ThreadUtility();
-
-	static log4cxx::pre_thread_start pre_start;
-	static log4cxx::thread_started thread_start;
-	static log4cxx::post_thread_start post_start;
 };
 
 }

[logging-log4cxx] 02/04: Made documentation on logging threaded clearer

Posted by rm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 6e725a7883743e1b3b4c0130aa0906c36ad96b84
Author: Robert Middleton <ro...@rm5248.com>
AuthorDate: Fri Aug 20 23:26:48 2021 -0400

    Made documentation on logging threaded clearer
---
 src/site/markdown/threading.md | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/site/markdown/threading.md b/src/site/markdown/threading.md
index 045f319..6bf373e 100644
--- a/src/site/markdown/threading.md
+++ b/src/site/markdown/threading.md
@@ -22,8 +22,9 @@ Threading {#threading}
 -->
 # Threading Notes with Log4cxx
 
-Log4cxx is designed to be thread-safe under all circumstances.  However,
-this is not always the case.
+Log4cxx is designed to be thread-safe under under normal usage.  This
+means that logging itself is always thread-safe, however there are
+certain circumstances that can cause threading issues with Log4cxx.
 
 ## Unexpected Exit