28
28
#endif
29
29
#include < log4cxx/helpers/aprinitializer.h>
30
30
#include < log4cxx/helpers/threadspecificdata.h>
31
- #include < log4cxx/helpers/transcoder.h>
32
-
33
- #include < apr_portable.h>
34
- #include < apr_strings.h>
35
- #include < log4cxx/helpers/stringhelper.h>
36
31
#include < log4cxx/helpers/bytebuffer.h>
37
- #include < log4cxx/logger.h>
38
- #include < log4cxx/private/log4cxx_private.h>
39
32
#include < log4cxx/helpers/date.h>
40
33
#include < log4cxx/helpers/optional.h>
41
- #include < thread>
42
34
43
35
using namespace LOG4CXX_NS ;
44
36
using namespace LOG4CXX_NS ::spi;
45
37
using namespace LOG4CXX_NS ::helpers;
46
38
47
39
struct LoggingEvent ::LoggingEventPrivate
48
40
{
49
- LoggingEventPrivate () :
41
+ LoggingEventPrivate (const ThreadSpecificData::NamePairPtr& p = ThreadSpecificData::getCurrentData()-> getNames () ) :
50
42
timeStamp(0 ),
51
- threadName (getCurrentThreadName()),
52
- threadUserName (getCurrentThreadUserName())
43
+ #if LOG4CXX_ABI_VERSION <= 15
44
+ threadName (p->idString),
45
+ threadUserName(p->threadName),
46
+ #endif
47
+ pNames (p)
53
48
{
54
49
}
55
50
@@ -58,29 +53,38 @@ struct LoggingEvent::LoggingEventPrivate
58
53
, const LevelPtr& level1
59
54
, const LocationInfo& locationInfo1
60
55
, LogString&& message1
56
+ , const ThreadSpecificData::NamePairPtr& p = ThreadSpecificData::getCurrentData()->getNames ()
61
57
) :
62
58
logger(logger1),
63
59
level(level1),
64
60
message(std::move(message1)),
65
61
timeStamp(Date::currentTime()),
66
62
locationInfo(locationInfo1),
67
- threadName (getCurrentThreadName()),
68
- threadUserName (getCurrentThreadUserName()),
69
- chronoTimeStamp (std::chrono::microseconds(timeStamp))
63
+ #if LOG4CXX_ABI_VERSION <= 15
64
+ threadName (p->idString),
65
+ threadUserName(p->threadName),
66
+ #endif
67
+ chronoTimeStamp (std::chrono::microseconds(timeStamp)),
68
+ pNames(p)
70
69
{
71
70
}
72
71
73
72
LoggingEventPrivate (
74
73
const LogString& logger1, const LevelPtr& level1,
75
- const LogString& message1, const LocationInfo& locationInfo1) :
74
+ const LogString& message1, const LocationInfo& locationInfo1,
75
+ const ThreadSpecificData::NamePairPtr& p = ThreadSpecificData::getCurrentData()->getNames()
76
+ ) :
76
77
logger(logger1),
77
78
level(level1),
78
79
message(message1),
79
80
timeStamp(Date::currentTime()),
80
81
locationInfo(locationInfo1),
81
- threadName (getCurrentThreadName()),
82
- threadUserName (getCurrentThreadUserName()),
83
- chronoTimeStamp (std::chrono::microseconds(timeStamp))
82
+ #if LOG4CXX_ABI_VERSION <= 15
83
+ threadName (p->idString),
84
+ threadUserName(p->threadName),
85
+ #endif
86
+ chronoTimeStamp (std::chrono::microseconds(timeStamp)),
87
+ pNames(p)
84
88
{
85
89
}
86
90
@@ -138,6 +142,7 @@ struct LoggingEvent::LoggingEventPrivate
138
142
const spi::LocationInfo locationInfo;
139
143
140
144
145
+ #if LOG4CXX_ABI_VERSION <= 15
141
146
/* * The identifier of thread in which this logging event
142
147
was generated.
143
148
*/
@@ -149,24 +154,25 @@ struct LoggingEvent::LoggingEventPrivate
149
154
* systems or SetThreadDescription on Windows.
150
155
*/
151
156
const LogString& threadUserName;
157
+ #endif
152
158
153
159
std::chrono::time_point<std::chrono::system_clock> chronoTimeStamp;
154
160
155
161
/* *
156
162
* This ensures the above string references remain valid
157
163
* for the lifetime of this LoggingEvent (i.e. even after thread termination).
158
164
*/
159
- ThreadSpecificData::OtherDataPtr pNames = ThreadSpecificData::getCurrentData()-> getOtherData () ;
165
+ ThreadSpecificData::NamePairPtr pNames;
160
166
161
- /* *
162
- * Used to hold the diagnostic context when the lifetime
163
- * of this LoggingEvent exceeds the duration of the logging request.
164
- */
165
167
struct DiagnosticContext
166
168
{
167
169
Optional<NDC::DiagnosticContext> ctx;
168
170
MDC::Map map;
169
171
};
172
+ /* *
173
+ * Used to hold the diagnostic context when the lifetime
174
+ * of this LoggingEvent exceeds the duration of the logging request.
175
+ */
170
176
mutable std::unique_ptr<DiagnosticContext> dc;
171
177
};
172
178
@@ -209,7 +215,7 @@ LoggingEvent::~LoggingEvent()
209
215
210
216
const LogString& LoggingEvent::getThreadUserName () const
211
217
{
212
- return m_priv->threadUserName ;
218
+ return m_priv->pNames -> threadName ;
213
219
}
214
220
215
221
bool LoggingEvent::getNDC (LogString& dest) const
@@ -311,82 +317,6 @@ LoggingEvent::KeySet LoggingEvent::getPropertyKeySet() const
311
317
return set;
312
318
}
313
319
314
-
315
- const LogString& LoggingEvent::getCurrentThreadName ()
316
- {
317
- LogString& thread_id_string = ThreadSpecificData::getThreadIdString ();
318
- if ( !thread_id_string.empty () )
319
- {
320
- return thread_id_string;
321
- }
322
-
323
- #if LOG4CXX_HAS_PTHREAD_SELF && !(defined(_WIN32) && defined(_LIBCPP_VERSION))
324
- // pthread_t encoded in HEX takes needs as many characters
325
- // as two times the size of the type, plus an additional null byte.
326
- auto threadId = pthread_self ();
327
- char result[sizeof (pthread_t ) * 3 + 10 ];
328
- apr_snprintf (result, sizeof (result), LOG4CXX_APR_THREAD_FMTSPEC, (void *) &threadId);
329
- thread_id_string = Transcoder::decode (result);
330
- #elif defined(_WIN32)
331
- char result[20 ];
332
- apr_snprintf (result, sizeof (result), LOG4CXX_WIN32_THREAD_FMTSPEC, GetCurrentThreadId ());
333
- thread_id_string = Transcoder::decode (result);
334
- #else
335
- std::stringstream ss;
336
- ss << std::hex << " 0x" << std::this_thread::get_id ();
337
- thread_id_string = Transcoder::decode (ss.str ().c_str ());
338
- #endif
339
- return thread_id_string;
340
- }
341
-
342
- const LogString& LoggingEvent::getCurrentThreadUserName ()
343
- {
344
- LogString& thread_name = ThreadSpecificData::getThreadName ();
345
- if ( !thread_name.empty () ){
346
- return thread_name;
347
- }
348
-
349
- #if LOG4CXX_HAS_PTHREAD_GETNAME && !(defined(_WIN32) && defined(_LIBCPP_VERSION))
350
- char result[16 ];
351
- pthread_t current_thread = pthread_self ();
352
- if (pthread_getname_np (current_thread, result, sizeof (result)) < 0 || 0 == result[0 ])
353
- thread_name = getCurrentThreadName ();
354
- else
355
- thread_name = Transcoder::decode (result);
356
- #elif defined(_WIN32)
357
- typedef HRESULT (WINAPI *TGetThreadDescription)(HANDLE, PWSTR*);
358
- static struct initialiser
359
- {
360
- HMODULE hKernelBase;
361
- TGetThreadDescription GetThreadDescription;
362
- initialiser ()
363
- : hKernelBase(GetModuleHandleA(" KernelBase.dll" ))
364
- , GetThreadDescription(nullptr )
365
- {
366
- if (hKernelBase)
367
- GetThreadDescription = reinterpret_cast <TGetThreadDescription>(GetProcAddress (hKernelBase, " GetThreadDescription" ));
368
- }
369
- } win32func;
370
- if (win32func.GetThreadDescription )
371
- {
372
- PWSTR result = 0 ;
373
- HRESULT hr = win32func.GetThreadDescription (GetCurrentThread (), &result);
374
- if (SUCCEEDED (hr) && result)
375
- {
376
- std::wstring wresult = result;
377
- LOG4CXX_DECODE_WCHAR (decoded, wresult);
378
- LocalFree (result);
379
- thread_name = decoded;
380
- }
381
- }
382
- if (thread_name.empty ())
383
- thread_name = getCurrentThreadName ();
384
- #else
385
- thread_name = getCurrentThreadName ();
386
- #endif
387
- return thread_name;
388
- }
389
-
390
320
void LoggingEvent::setProperty (const LogString& key, const LogString& value)
391
321
{
392
322
if (m_priv->properties == 0 )
@@ -419,7 +349,7 @@ const LogString& LoggingEvent::getRenderedMessage() const
419
349
420
350
const LogString& LoggingEvent::getThreadName () const
421
351
{
422
- return m_priv->threadName ;
352
+ return m_priv->pNames -> idString ;
423
353
}
424
354
425
355
log4cxx_time_t LoggingEvent::getTimeStamp () const
0 commit comments