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 2819df0e Use threadId for an unnamed thread when the layout requires a 
thread name (#321)
2819df0e is described below

commit 2819df0eb700a2023481a94c94b9a8afe4c3ec50
Author: Stephen Webb <stephen.w...@ieee.org>
AuthorDate: Wed Dec 27 10:59:51 2023 +1100

    Use threadId for an unnamed thread when the layout requires a thread name 
(#321)
---
 src/main/cpp/loggingevent.cpp | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)

diff --git a/src/main/cpp/loggingevent.cpp b/src/main/cpp/loggingevent.cpp
index 129d196f..c647fd67 100644
--- a/src/main/cpp/loggingevent.cpp
+++ b/src/main/cpp/loggingevent.cpp
@@ -354,8 +354,8 @@ const LogString& LoggingEvent::getCurrentThreadName()
        thread_local LogString thread_id_string;
 #else
        using ListItem = std::pair<ThreadIdType, LogString>;
-       static WideLife<std::list<ListItem>> thread_id_map;
-       static WideLife<std::mutex> mutex;
+       static std::list<ListItem> thread_id_map;
+       static std::mutex mutex;
        std::lock_guard<std::mutex> lock(mutex);
        auto pThreadId = std::find_if(thread_id_map.begin(), thread_id_map.end()
                , [threadId](const ListItem& item) { return threadId == 
item.first; });
@@ -371,13 +371,13 @@ const LogString& LoggingEvent::getCurrentThreadName()
 #if defined(_WIN32)
        char result[20];
        apr_snprintf(result, sizeof(result), LOG4CXX_WIN32_THREAD_FMTSPEC, 
threadId);
-       Transcoder::decode(reinterpret_cast<const char*>(result), 
thread_id_string);
+       thread_id_string = Transcoder::decode(result);
 #elif LOG4CXX_HAS_PTHREAD_SELF
        // pthread_t encoded in HEX takes needs as many characters
        // as two times the size of the type, plus an additional null byte.
        char result[sizeof(pthread_t) * 3 + 10];
        apr_snprintf(result, sizeof(result), LOG4CXX_APR_THREAD_FMTSPEC, 
(void*) &threadId);
-       Transcoder::decode(reinterpret_cast<const char*>(result), 
thread_id_string);
+       thread_id_string = Transcoder::decode(result);
 #else
        thread_id_string = LOG4CXX_STR("0x00000000");
 #endif
@@ -398,11 +398,10 @@ const LogString& LoggingEvent::getCurrentThreadUserName()
 #if LOG4CXX_HAS_PTHREAD_GETNAME
        char result[16];
        pthread_t current_thread = pthread_self();
-       if( pthread_getname_np( current_thread, result, sizeof(result) ) < 0 ){
-               thread_name = LOG4CXX_STR("(noname)");
-       }
-
-       LOG4CXX_NS::helpers::Transcoder::decode(reinterpret_cast<const 
char*>(result), thread_name);
+       if (pthread_getname_np(current_thread, result, sizeof(result)) < 0 || 0 
== result[0])
+               thread_name = getCurrentThreadName();
+       else
+               thread_name = Transcoder::decode(result);
 #elif WIN32
        typedef HRESULT (WINAPI *TGetThreadDescription)(HANDLE, PWSTR*);
        static struct initialiser
@@ -421,18 +420,18 @@ const LogString& LoggingEvent::getCurrentThreadUserName()
        {
                PWSTR result = 0;
                HRESULT hr = win32func.GetThreadDescription(GetCurrentThread(), 
&result);
-               if (SUCCEEDED(hr))
+               if (SUCCEEDED(hr) && result)
                {
                        std::wstring wresult = result;
                        LOG4CXX_DECODE_WCHAR(decoded, wresult);
                        LocalFree(result);
                        thread_name = decoded;
                }
-       }else{
-               thread_name = LOG4CXX_STR("(noname)");
        }
+       if (thread_name.empty())
+               thread_name = getCurrentThreadName();
 #else
-       thread_name = LOG4CXX_STR("(noname)");
+       thread_name = getCurrentThreadName();
 #endif
        return thread_name;
 }

Reply via email to