Skip to content

Commit

Permalink
Generalize and document the ThreadSpecificData interface
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-webb committed Jul 23, 2024
1 parent 58a00e2 commit a36dedb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
16 changes: 10 additions & 6 deletions src/main/cpp/threadspecificdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@
using namespace LOG4CXX_NS;
using namespace LOG4CXX_NS::helpers;

struct ThreadSpecificData::OtherData : public std::pair<LogString, LogString>
{
};

struct ThreadSpecificData::ThreadSpecificDataPrivate{
ThreadSpecificDataPrivate()
: pNames(std::make_shared<NameData>())
: pOtherData(std::make_shared<OtherData>())
{}
NDC::Stack ndcStack;
MDC::Map mdcMap;

std::shared_ptr<NameData> pNames;
std::shared_ptr<OtherData> pOtherData;

#if !LOG4CXX_LOGCHAR_IS_UNICHAR && !LOG4CXX_LOGCHAR_IS_WCHAR
std::basic_ostringstream<logchar> logchar_stringstream;
Expand Down Expand Up @@ -78,17 +82,17 @@ MDC::Map& ThreadSpecificData::getMap()

LogString& ThreadSpecificData::getThreadIdString()
{
return getCurrentData()->m_priv->pNames->first;
return getCurrentData()->m_priv->pOtherData->first;
}

LogString& ThreadSpecificData::getThreadName()
{
return getCurrentData()->m_priv->pNames->second;
return getCurrentData()->m_priv->pOtherData->second;
}

auto ThreadSpecificData::getThreadNames() -> NameDataPtr
auto ThreadSpecificData::getOtherData() -> OtherDataPtr
{
return getCurrentData()->m_priv->pNames;
return getCurrentData()->m_priv->pOtherData;
}

#if !LOG4CXX_LOGCHAR_IS_UNICHAR && !LOG4CXX_LOGCHAR_IS_WCHAR
Expand Down
16 changes: 11 additions & 5 deletions src/main/include/log4cxx/helpers/threadspecificdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class LOG4CXX_EXPORT ThreadSpecificData

/**
* Gets current thread specific data.
* @return thread specific data, may be null.
* @return a pointer that is never null.
*/
static ThreadSpecificData* getCurrentData();
/**
Expand All @@ -48,7 +48,7 @@ class LOG4CXX_EXPORT ThreadSpecificData

static void put(const LogString& key, const LogString& val);
static void push(const LogString& val);
static void inherit(const LOG4CXX_NS::NDC::Stack& stack);
static void inherit(const NDC::Stack& stack);

NDC::Stack& getStack();
MDC::Map& getMap();
Expand All @@ -61,9 +61,15 @@ class LOG4CXX_EXPORT ThreadSpecificData
static LogString& getThreadIdString();
static LogString& getThreadName();

using NameData = std::pair<LogString, LogString>;
using NameDataPtr = std::shared_ptr<NameData>;
static NameDataPtr getThreadNames();
/**
* A reference counted pointer to thread specific strings.
*
* String references will remain valid
* for the lifetime of this pointer (i.e. even after thread termination).
*/
struct OtherData;
using OtherDataPtr = std::shared_ptr<OtherData>;
static OtherDataPtr getOtherData();
private:
#if !LOG4CXX_LOGCHAR_IS_UNICHAR && !LOG4CXX_LOGCHAR_IS_WCHAR
static std::basic_ostringstream<logchar>& getStream(const logchar&);
Expand Down

0 comments on commit a36dedb

Please sign in to comment.