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

swebb2066 pushed a commit to branch ostream_logging_performance
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git

commit 23e41c4253e89e52531c3f93fb0da5dd83c59210
Author: Stephen Webb <[email protected]>
AuthorDate: Mon Nov 7 14:44:00 2022 +1100

    Reduce std::ostream logging overhead
---
 src/main/cpp/domconfigurator.cpp                 |   8 --
 src/main/cpp/messagebuffer.cpp                   | 125 ++++++-----------------
 src/main/cpp/propertyconfigurator.cpp            |   8 --
 src/main/include/log4cxx/helpers/messagebuffer.h |   2 -
 4 files changed, 31 insertions(+), 112 deletions(-)

diff --git a/src/main/cpp/domconfigurator.cpp b/src/main/cpp/domconfigurator.cpp
index 17178caa..4786e842 100644
--- a/src/main/cpp/domconfigurator.cpp
+++ b/src/main/cpp/domconfigurator.cpp
@@ -1038,14 +1038,6 @@ void DOMConfigurator::parse(
                m_priv->repository->setThreshold(thresholdStr);
        }
 
-       LogString strstrValue = subst(getAttribute(utf8Decoder, element, 
STRINGSTREAM_ATTR));
-       LogLog::debug(LOG4CXX_STR("Stringstream =\"") + strstrValue + 
LOG4CXX_STR("\"."));
-
-       if (!strstrValue.empty() && strstrValue != NULL_STRING)
-       {
-               MessageBufferUseStaticStream();
-       }
-
        LogString threadSignalValue = subst(getAttribute(utf8Decoder, element, 
THREAD_CONFIG_ATTR));
 
        if ( !threadSignalValue.empty() && threadSignalValue != NULL_STRING )
diff --git a/src/main/cpp/messagebuffer.cpp b/src/main/cpp/messagebuffer.cpp
index eaaa7bac..75e46c12 100644
--- a/src/main/cpp/messagebuffer.cpp
+++ b/src/main/cpp/messagebuffer.cpp
@@ -22,22 +22,13 @@
 #endif
 #include <log4cxx/helpers/messagebuffer.h>
 #include <log4cxx/helpers/transcoder.h>
+#if !defined(LOG4CXX)
+       #define LOG4CXX 1
+#endif
+#include <log4cxx/private/log4cxx_private.h>
 
 using namespace log4cxx::helpers;
 
-static bool gMessageBufferUseStaticStream = false;
-
-namespace log4cxx
-{
-namespace helpers
-{
-void MessageBufferUseStaticStream()
-{
-       gMessageBufferUseStaticStream = true;
-}
-}
-}
-
 template <typename T>
 void ResetStream(std::basic_ostringstream<T>& stream)
 {
@@ -62,35 +53,13 @@ struct CharMessageBuffer::CharMessageBufferPrivate{
 
 CharMessageBuffer::CharMessageBuffer() : 
m_priv(std::make_unique<CharMessageBufferPrivate>())
 {
-
-#if defined(STATIC_STRINGSTREAM)
-
-       if (gMessageBufferUseStaticStream)
-       {
-               thread_local static char ossBuf[8192];
-               thread_local static std::basic_ostringstream<char> sStream;
-               thread_local static bool inited = false;
-
-               if (!inited)
-               {
-                       inited = true;
-                       sStream.rdbuf()->pubsetbuf(ossBuf, 8192);
-
-                       ResetStream(sStream);
-               }
-
-               m_priv->stream = &sStream;
-       }
-
-#endif
 }
 
 CharMessageBuffer::~CharMessageBuffer()
 {
-       if (!gMessageBufferUseStaticStream)
-       {
-               delete m_priv->stream;
-       }
+#if !LOG4CXX_HAS_THREAD_LOCAL
+       delete m_priv->stream;
+#endif
 }
 
 CharMessageBuffer& CharMessageBuffer::operator<<(const 
std::basic_string<char>& msg)
@@ -149,10 +118,14 @@ CharMessageBuffer& CharMessageBuffer::operator<<(const 
char msg)
 
 CharMessageBuffer::operator std::basic_ostream<char>& ()
 {
-       if (m_priv->stream == 0)
+       if (!m_priv->stream)
        {
+#if LOG4CXX_HAS_THREAD_LOCAL
+               thread_local static std::basic_ostringstream<char> sStream;
+               m_priv->stream = &sStream;
+#else
                m_priv->stream = new std::basic_ostringstream<char>();
-
+#endif
                if (!m_priv->buf.empty())
                {
                        *m_priv->stream << m_priv->buf;
@@ -248,35 +221,13 @@ struct WideMessageBuffer::WideMessageBufferPrivate{
 WideMessageBuffer::WideMessageBuffer() :
        m_priv(std::make_unique<WideMessageBufferPrivate>())
 {
-
-#if defined(STATIC_STRINGSTREAM)
-
-       if (gMessageBufferUseStaticStream)
-       {
-               thread_local static wchar_t ossBuf[8192];
-               thread_local static std::basic_ostringstream<wchar_t> sStream;
-               thread_local static bool inited = false;
-
-               if (!inited)
-               {
-                       inited = true;
-                       sStream.rdbuf()->pubsetbuf(ossBuf, 8192);
-
-                       ResetStream(sStream);
-               }
-
-               m_priv->stream = &sStream;
-       }
-
-#endif
 }
 
 WideMessageBuffer::~WideMessageBuffer()
 {
-       if (!gMessageBufferUseStaticStream)
-       {
-               delete m_priv->stream;
-       }
+#if !LOG4CXX_HAS_THREAD_LOCAL
+       delete m_priv->stream;
+#endif
 }
 
 WideMessageBuffer& WideMessageBuffer::operator<<(const 
std::basic_string<wchar_t>& msg)
@@ -336,10 +287,14 @@ WideMessageBuffer& WideMessageBuffer::operator<<(const 
wchar_t msg)
 
 WideMessageBuffer::operator std::basic_ostream<wchar_t>& ()
 {
-       if (m_priv->stream == 0)
+       if (!m_priv->stream)
        {
+#if LOG4CXX_HAS_THREAD_LOCAL
+               thread_local static std::basic_ostringstream<wchar_t> sStream;
+               m_priv->stream = &sStream;
+#else
                m_priv->stream = new std::basic_ostringstream<wchar_t>();
-
+#endif
                if (!m_priv->buf.empty())
                {
                        *m_priv->stream << m_priv->buf;
@@ -623,35 +578,13 @@ struct UniCharMessageBuffer::UniCharMessageBufferPrivate {
 UniCharMessageBuffer::UniCharMessageBuffer() :
        m_priv(std::make_unique<UniCharMessageBufferPrivate>())
 {
-
-#if defined(STATIC_STRINGSTREAM)
-
-       if (gMessageBufferUseStaticStream)
-       {
-               thread_local static log4cxx::UniChar ossBuf[8192];
-               thread_local static std::basic_ostringstream<log4cxx::UniChar> 
sStream;
-               thread_local static bool inited = false;
-
-               if (!inited)
-               {
-                       inited = true;
-                       sStream.rdbuf()->pubsetbuf(ossBuf, 8192);
-
-                       ResetStream(sStream);
-               }
-
-               m_priv->stream = &sStream;
-       }
-
-#endif
 }
 
 UniCharMessageBuffer::~UniCharMessageBuffer()
 {
-       if (!gMessageBufferUseStaticStream)
-       {
-               delete m_priv->stream;
-       }
+#if !LOG4CXX_HAS_THREAD_LOCAL
+       delete m_priv->stream;
+#endif
 }
 
 
@@ -714,8 +647,12 @@ UniCharMessageBuffer::operator 
UniCharMessageBuffer::uostream& ()
 {
        if (!m_priv->stream)
        {
-               m_priv->stream = new std::basic_ostringstream<UniChar>();
-
+#if LOG4CXX_HAS_THREAD_LOCAL
+               thread_local static std::basic_ostringstream<log4cxx::UniChar> 
sStream;
+               m_priv->stream = &sStream;
+#else
+               m_priv->stream = new 
std::basic_ostringstream<log4cxx::UniChar>();
+#endif
                if (!m_priv->buf.empty())
                {
                        *m_priv->stream << m_priv->buf;
diff --git a/src/main/cpp/propertyconfigurator.cpp 
b/src/main/cpp/propertyconfigurator.cpp
index 4083a78a..c9dd0888 100644
--- a/src/main/cpp/propertyconfigurator.cpp
+++ b/src/main/cpp/propertyconfigurator.cpp
@@ -178,14 +178,6 @@ void 
PropertyConfigurator::doConfigure(helpers::Properties& properties,
                        + LOG4CXX_STR("]."));
        }
 
-       static const LogString 
STRINGSTREAM_KEY(LOG4CXX_STR("log4j.stringstream"));
-       LogString strstrValue(properties.getProperty(STRINGSTREAM_KEY));
-
-       if (strstrValue == LOG4CXX_STR("static"))
-       {
-               MessageBufferUseStaticStream();
-       }
-
        LogString 
threadConfigurationValue(properties.getProperty(LOG4CXX_STR("log4j.threadConfiguration")));
 
        if ( threadConfigurationValue == LOG4CXX_STR("NoConfiguration") )
diff --git a/src/main/include/log4cxx/helpers/messagebuffer.h 
b/src/main/include/log4cxx/helpers/messagebuffer.h
index 9624722a..285bd5d4 100644
--- a/src/main/include/log4cxx/helpers/messagebuffer.h
+++ b/src/main/include/log4cxx/helpers/messagebuffer.h
@@ -34,8 +34,6 @@ namespace log4cxx
 namespace helpers
 {
 
-void MessageBufferUseStaticStream();
-
 typedef std::ios_base& (*ios_base_manip)(std::ios_base&);
 
 /**

Reply via email to