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 2023/01/20 23:20:30 UTC
[logging-log4cxx] branch master updated: Allow use on Window Server 2016 (issue #180). (#181)
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 ae966e6e Allow use on Window Server 2016 (issue #180). (#181)
ae966e6e is described below
commit ae966e6e93e3c38a55bf423aae1502329ba2ffb3
Author: Stephen Webb <st...@ieee.org>
AuthorDate: Sat Jan 21 10:20:25 2023 +1100
Allow use on Window Server 2016 (issue #180). (#181)
Put more than the first character of thread name into the event log on Windows.
---
src/main/cpp/loggingevent.cpp | 32 +++++++++++++++++-----
src/main/cpp/threadutility.cpp | 27 +++++++++++++-----
src/main/include/CMakeLists.txt | 7 -----
.../include/log4cxx/private/log4cxx_private.h.in | 2 --
src/test/cpp/helpers/threadutilitytestcase.cpp | 29 ++++++++++++++++++++
src/test/cpp/pattern/patternparsertestcase.cpp | 2 +-
src/test/resources/witness/autoConfigureTest.log | 1 -
src/test/resources/witness/threadnametestcase.1 | 1 +
8 files changed, 76 insertions(+), 25 deletions(-)
diff --git a/src/main/cpp/loggingevent.cpp b/src/main/cpp/loggingevent.cpp
index daf89500..cc7f7479 100644
--- a/src/main/cpp/loggingevent.cpp
+++ b/src/main/cpp/loggingevent.cpp
@@ -374,13 +374,31 @@ const LogString& LoggingEvent::getCurrentThreadUserName()
}
log4cxx::helpers::Transcoder::decode(reinterpret_cast<const char*>(result), thread_name);
-#elif LOG4CXX_HAS_GETTHREADDESCRIPTION
- PWSTR result;
- HANDLE threadId = GetCurrentThread();
- if( GetThreadDescription( threadId, &result ) == 0 ){
- // Success
- log4cxx::helpers::Transcoder::decode(reinterpret_cast<const char*>(result), thread_name);
- LocalFree(result);
+#elif WIN32
+ typedef HRESULT (WINAPI *TGetThreadDescription)(HANDLE, PWSTR*);
+ static struct initialiser
+ {
+ HMODULE hKernelBase;
+ TGetThreadDescription GetThreadDescription;
+ initialiser()
+ : hKernelBase(GetModuleHandleA("KernelBase.dll"))
+ , GetThreadDescription(nullptr)
+ {
+ if (hKernelBase)
+ GetThreadDescription = reinterpret_cast<TGetThreadDescription>(GetProcAddress(hKernelBase, "GetThreadDescription"));
+ }
+ } win32func;
+ if (win32func.GetThreadDescription)
+ {
+ PWSTR result = 0;
+ HRESULT hr = win32func.GetThreadDescription(GetCurrentThread(), &result);
+ if (SUCCEEDED(hr))
+ {
+ std::wstring wresult = result;
+ LOG4CXX_DECODE_UNICHAR(decoded, wresult);
+ LocalFree(result);
+ thread_name = decoded;
+ }
}else{
thread_name = LOG4CXX_STR("(noname)");
}
diff --git a/src/main/cpp/threadutility.cpp b/src/main/cpp/threadutility.cpp
index 877b06ea..a3c59737 100644
--- a/src/main/cpp/threadutility.cpp
+++ b/src/main/cpp/threadutility.cpp
@@ -25,7 +25,7 @@
#include <signal.h>
#include <mutex>
-#if LOG4CXX_HAS_SETTHREADDESCRIPTION
+#if WIN32
#include <windows.h>
#include <processthreadsapi.h>
#endif
@@ -139,13 +139,26 @@ void ThreadUtility::threadStartedNameThread(LogString threadName,
if (pthread_setname_np(static_cast<pthread_t>(nativeHandle), sthreadName.c_str()) < 0) {
LOGLOG_ERROR(LOG4CXX_STR("unable to set thread name"));
}
-#elif LOG4CXX_HAS_SETTHREADDESCRIPTION
- LOG4CXX_ENCODE_WCHAR(wthreadName, threadName);
- HRESULT hr = SetThreadDescription(static_cast<HANDLE>(nativeHandle), wthreadName.c_str());
- if(FAILED(hr)){
- LOGLOG_ERROR( LOG4CXX_STR("unable to set thread name") );
+#elif WIN32
+ typedef HRESULT (WINAPI *TSetThreadDescription)(HANDLE, PCWSTR);
+ static struct initialiser
+ {
+ HMODULE hKernelBase;
+ TSetThreadDescription SetThreadDescription;
+ initialiser()
+ : hKernelBase(GetModuleHandleA("KernelBase.dll"))
+ , SetThreadDescription(nullptr)
+ {
+ if (hKernelBase)
+ SetThreadDescription = reinterpret_cast<TSetThreadDescription>(GetProcAddress(hKernelBase, "SetThreadDescription"));
+ }
+ } win32Func;
+ if (win32Func.SetThreadDescription)
+ {
+ LOG4CXX_ENCODE_WCHAR(wthreadName, threadName);
+ if(FAILED(win32Func.SetThreadDescription(static_cast<HANDLE>(nativeHandle), wthreadName.c_str())))
+ LOGLOG_ERROR( LOG4CXX_STR("unable to set thread name") );
}
-
#endif
}
diff --git a/src/main/include/CMakeLists.txt b/src/main/include/CMakeLists.txt
index 59982cf3..62f61bd4 100644
--- a/src/main/include/CMakeLists.txt
+++ b/src/main/include/CMakeLists.txt
@@ -146,11 +146,6 @@ if(UNIX)
endif()
endif(UNIX)
-if(WIN32)
- CHECK_SYMBOL_EXISTS(SetThreadDescription "windows.h;processthreadsapi.h" HAS_SETTHREADDESCRIPTION)
- CHECK_SYMBOL_EXISTS(GetThreadDescription "windows.h;processthreadsapi.h" HAS_GETTHREADDESCRIPTION)
-endif(WIN32)
-
foreach(varName
HAS_THREAD_LOCAL
HAS_STD_LOCALE
@@ -163,8 +158,6 @@ foreach(varName
HAS_PTHREAD_SIGMASK
HAS_PTHREAD_SETNAME
HAS_PTHREAD_GETNAME
- HAS_SETTHREADDESCRIPTION
- HAS_GETTHREADDESCRIPTION
)
if(${varName} EQUAL 0)
continue()
diff --git a/src/main/include/log4cxx/private/log4cxx_private.h.in b/src/main/include/log4cxx/private/log4cxx_private.h.in
index 9eb5ec23..61e8e7ac 100644
--- a/src/main/include/log4cxx/private/log4cxx_private.h.in
+++ b/src/main/include/log4cxx/private/log4cxx_private.h.in
@@ -60,8 +60,6 @@
#define LOG4CXX_HAS_PTHREAD_SIGMASK @HAS_PTHREAD_SIGMASK@
#define LOG4CXX_HAS_PTHREAD_SETNAME @HAS_PTHREAD_SETNAME@
#define LOG4CXX_HAS_PTHREAD_GETNAME @HAS_PTHREAD_GETNAME@
-#define LOG4CXX_HAS_SETTHREADDESCRIPTION @HAS_SETTHREADDESCRIPTION@
-#define LOG4CXX_HAS_GETTHREADDESCRIPTION @HAS_GETTHREADDESCRIPTION@
#define LOG4CXX_HAS_THREAD_LOCAL @HAS_THREAD_LOCAL@
#endif
diff --git a/src/test/cpp/helpers/threadutilitytestcase.cpp b/src/test/cpp/helpers/threadutilitytestcase.cpp
index 0460b08b..effe1eda 100644
--- a/src/test/cpp/helpers/threadutilitytestcase.cpp
+++ b/src/test/cpp/helpers/threadutilitytestcase.cpp
@@ -16,7 +16,12 @@
*/
#include "../logunit.h"
+#include "util/compare.h"
#include <log4cxx/helpers/threadutility.h>
+#include <log4cxx/helpers/loglog.h>
+#include <log4cxx/patternlayout.h>
+#include <log4cxx/fileappender.h>
+#include <log4cxx/logmanager.h>
using namespace log4cxx;
using namespace log4cxx::helpers;
@@ -28,7 +33,18 @@ LOGUNIT_CLASS(ThreadUtilityTest)
LOGUNIT_TEST(testNullFunctions);
LOGUNIT_TEST(testCustomFunctions);
LOGUNIT_TEST(testDefaultFunctions);
+#if LOG4CXX_HAS_PTHREAD_SETNAME || defined(WIN32)
+ LOGUNIT_TEST(testThreadNameLogging);
+#endif
LOGUNIT_TEST_SUITE_END();
+#ifdef _DEBUG
+ struct Fixture
+ {
+ Fixture() {
+ LogLog::setInternalDebugging(true);
+ }
+ } suiteFixture;
+#endif
public:
void testNullFunctions()
@@ -86,6 +102,19 @@ public:
t.join();
}
+ void testThreadNameLogging()
+ {
+ auto layout = std::make_shared<PatternLayout>(LOG4CXX_STR("%T %m%n"));
+ LogString logFileName(LOG4CXX_STR("output/threadnametestcase.log"));
+ AppenderPtr appender(new FileAppender(layout, logFileName, false));
+ auto logger = LogManager::getRootLogger();
+ logger->addAppender(appender);
+ std::thread t = ThreadUtility::instance()->createThread( LOG4CXX_STR("FooName"), [logger]() {
+ LOG4CXX_DEBUG(logger, "Test message");
+ });
+ t.join();
+ LOGUNIT_ASSERT(Compare::compare(logFileName, LOG4CXX_FILE("witness/threadnametestcase.1")));
+ }
};
diff --git a/src/test/cpp/pattern/patternparsertestcase.cpp b/src/test/cpp/pattern/patternparsertestcase.cpp
index 61f42310..3b41a83e 100644
--- a/src/test/cpp/pattern/patternparsertestcase.cpp
+++ b/src/test/cpp/pattern/patternparsertestcase.cpp
@@ -91,7 +91,7 @@ public:
if( pthread_setname_np( pthread_self(), sthreadName.c_str() ) < 0 ){
LOGLOG_ERROR( LOG4CXX_STR("unable to set thread name") );
}
-#elif LOG4CXX_HAS_SETTHREADDESCRIPTION
+#elif WIN32
LOG4CXX_ENCODE_WCHAR(wthreadName, threadName);
HRESULT hr = SetThreadDescription(GetCurrentThread(), wthreadName.c_str());
if(FAILED(hr)){
diff --git a/src/test/resources/witness/autoConfigureTest.log b/src/test/resources/witness/autoConfigureTest.log
deleted file mode 100644
index d35848f3..00000000
--- a/src/test/resources/witness/autoConfigureTest.log
+++ /dev/null
@@ -1 +0,0 @@
-This is some expected ouput
diff --git a/src/test/resources/witness/threadnametestcase.1 b/src/test/resources/witness/threadnametestcase.1
new file mode 100644
index 00000000..431f7ada
--- /dev/null
+++ b/src/test/resources/witness/threadnametestcase.1
@@ -0,0 +1 @@
+FooName Test message