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/04/07 23:18:52 UTC

[logging-log4cxx] branch master updated: Turn the threadname into a thread_local variable (#60)

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

rmiddleton 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 aaaea6a  Turn the threadname into a thread_local variable (#60)
aaaea6a is described below

commit aaaea6aadf3fda0d2e0511d7034305cf1e8b9353
Author: Robert Middleton <rm...@users.noreply.github.com>
AuthorDate: Wed Apr 7 19:18:44 2021 -0400

    Turn the threadname into a thread_local variable (#60)
    
    * turn thread name into thread_local
    
    * Add check to log4cxx_private to see if thread_local is supported
    
    * Use macro to switch between using thread_local keyword or not
---
 src/main/cpp/loggingevent.cpp                         | 15 +++++++++++----
 src/main/include/log4cxx/private/log4cxx_private.h.in | 10 ++++++++++
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/main/cpp/loggingevent.cpp b/src/main/cpp/loggingevent.cpp
index 58d24f2..98056d5 100644
--- a/src/main/cpp/loggingevent.cpp
+++ b/src/main/cpp/loggingevent.cpp
@@ -222,6 +222,11 @@ LoggingEvent::KeySet LoggingEvent::getPropertyKeySet() const
 const LogString LoggingEvent::getCurrentThreadName()
 {
 #if APR_HAS_THREADS
+	LOG4CXX_THREAD_LOCAL LogString thread_name;
+	if( thread_name.size() ){
+		return thread_name;
+	}
+
 #if defined(_WIN32)
 	char result[20];
 	DWORD threadId = GetCurrentThreadId();
@@ -232,12 +237,14 @@ const LogString LoggingEvent::getCurrentThreadName()
 	char result[sizeof(apr_os_thread_t) * 3 + 10];
 	apr_os_thread_t threadId = apr_os_thread_current();
 	apr_snprintf(result, sizeof(result), LOG4CXX_APR_THREAD_FMTSPEC, (void*) &threadId);
-#endif
-	LOG4CXX_DECODE_CHAR(str, (const char*) result);
-	return str;
+#endif /* _WIN32 */
+
+	log4cxx::helpers::Transcoder::decode(reinterpret_cast<const char*>(result), thread_name);
+
+	return thread_name;
 #else
 	return LOG4CXX_STR("0x00000000");
-#endif
+#endif /* APR_HAS_THREADS */
 }
 
 
diff --git a/src/main/include/log4cxx/private/log4cxx_private.h.in b/src/main/include/log4cxx/private/log4cxx_private.h.in
index b1d5958..280f21f 100644
--- a/src/main/include/log4cxx/private/log4cxx_private.h.in
+++ b/src/main/include/log4cxx/private/log4cxx_private.h.in
@@ -53,6 +53,16 @@
 #define LOG4CXX_WIN32_THREAD_FMTSPEC "0x%.8x"
 #define LOG4CXX_APR_THREAD_FMTSPEC "0x%pt"
 
+#ifdef __BORLANDC__
+/*
+ * embarcadero/RAD Studio compilers don't support thread_local:
+ * http://docwiki.embarcadero.com/RADStudio/Sydney/en/Modern_C%2B%2B_Language_Features_Compliance_Status
+ */
+#define LOG4CXX_THREAD_LOCAL
+#else
+#define LOG4CXX_THREAD_LOCAL thread_local
+#endif
+
 #if defined(_MSC_VER)
 #define LOG4CXX_MEMSET_IOS_BASE 1
 #endif