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

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

commit 155fe3cb9bce0f774aae9c4ef1c7415182698d63
Author: Stephen Webb <[email protected]>
AuthorDate: Fri Oct 3 10:31:58 2025 +1000

    A configurator is responsible for the LoggerRepository state
---
 src/examples/cpp/console.cpp         | 1 +
 src/main/cpp/hierarchy.cpp           | 8 ++++----
 src/main/include/log4cxx/hierarchy.h | 6 +++++-
 src/test/cpp/benchmark/benchmark.cpp | 1 +
 src/test/cpp/levelchangetestcase.cpp | 2 ++
 src/test/cpp/terminationtestcase.cpp | 1 +
 6 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/src/examples/cpp/console.cpp b/src/examples/cpp/console.cpp
index d0688a42..8a3d9571 100644
--- a/src/examples/cpp/console.cpp
+++ b/src/examples/cpp/console.cpp
@@ -38,6 +38,7 @@ static void configure(bool err)
             , err ? ConsoleAppender::getSystemErr() : 
ConsoleAppender::getSystemOut()
             );
         r->getRootLogger()->addAppender(appender);
+        r->setConfigured(true);
     });
 }
 
diff --git a/src/main/cpp/hierarchy.cpp b/src/main/cpp/hierarchy.cpp
index b081a7d9..3945d78e 100644
--- a/src/main/cpp/hierarchy.cpp
+++ b/src/main/cpp/hierarchy.cpp
@@ -49,7 +49,7 @@ struct Hierarchy::HierarchyPrivate
 
        helpers::Pool pool;
        mutable std::recursive_mutex mutex;
-       mutable std::mutex configuredMutex;
+       mutable std::recursive_mutex configuredMutex;
        bool configured;
        bool emittedNoAppenderWarning;
        bool emittedNoResourceBundleWarning;
@@ -303,7 +303,7 @@ bool Hierarchy::isDisabled(int level) const
 
 void Hierarchy::ensureIsConfigured(std::function<void()> configurator)
 {
-       std::lock_guard<std::mutex> lock(m_priv->configuredMutex);
+       std::lock_guard<std::recursive_mutex> lock(m_priv->configuredMutex);
        if (!m_priv->configured && m_priv->alreadyTriedMethod != 
configurator.target_type().name())
        {
                configurator();
@@ -446,14 +446,14 @@ void Hierarchy::updateChildren(const Logger* parent)
 
 void Hierarchy::setConfigured(bool newValue)
 {
-       std::unique_lock<std::mutex> lock(m_priv->configuredMutex, 
std::try_to_lock);
+       std::unique_lock<std::recursive_mutex> lock(m_priv->configuredMutex, 
std::try_to_lock);
        if (lock.owns_lock()) // Not being auto-configured?
                m_priv->configured = newValue;
 }
 
 bool Hierarchy::isConfigured()
 {
-       std::lock_guard<std::mutex> lock(m_priv->configuredMutex); // Blocks 
while auto-configuration is active
+       std::lock_guard<std::recursive_mutex> lock(m_priv->configuredMutex); // 
Blocks while auto-configuration is active
        return m_priv->configured;
 }
 
diff --git a/src/main/include/log4cxx/hierarchy.h 
b/src/main/include/log4cxx/hierarchy.h
index 55c27fcf..9bfae700 100644
--- a/src/main/include/log4cxx/hierarchy.h
+++ b/src/main/include/log4cxx/hierarchy.h
@@ -86,8 +86,12 @@ class LOG4CXX_EXPORT Hierarchy : public spi::LoggerRepository
 #else
                void removeHierarchyEventListener(const 
spi::HierarchyEventListenerPtr& listener) override;
 #endif
+
                /**
-                * Call \c configurator if not yet configured.
+                * Call \c configurator if not yet configured and \c 
configurator has not already been tried.
+                * If \c configurator succeeds, it must call 
LoggerRepository::setConfigured
+                * to ensure subsequent calls to 
LoggerRepository::ensureIsConfigured
+                * from overwriting the configuration.
                 */
                void ensureIsConfigured(std::function<void()> configurator) 
override;
 
diff --git a/src/test/cpp/benchmark/benchmark.cpp 
b/src/test/cpp/benchmark/benchmark.cpp
index 3de48d53..85a348c2 100644
--- a/src/test/cpp/benchmark/benchmark.cpp
+++ b/src/test/cpp/benchmark/benchmark.cpp
@@ -173,6 +173,7 @@ public: // Class methods
                        auto writer = 
std::make_shared<NullWriterAppender>(std::make_shared<PatternLayout>(LOG4CXX_STR("%m%n")));
                        writer->setName(LOG4CXX_STR("NullAppender"));
                        r->getRootLogger()->addAppender(writer);
+                       r->setConfigured(true);
                        });
        }
 
diff --git a/src/test/cpp/levelchangetestcase.cpp 
b/src/test/cpp/levelchangetestcase.cpp
index c935d930..8befde16 100644
--- a/src/test/cpp/levelchangetestcase.cpp
+++ b/src/test/cpp/levelchangetestcase.cpp
@@ -59,6 +59,7 @@ LoggerPtr getLogger(const LogString& name = LogString())
        r->ensureIsConfigured([r]()
                {
                        
r->getRootLogger()->addAppender(std::make_shared<CountingAppender>());
+                       r->setConfigured(true);
                });
        return name.empty() ? r->getRootLogger() : r->getLogger(name);
 }
@@ -110,6 +111,7 @@ public:
        void testLevelChange()
        {
                auto appender = 
dynamic_cast<CountingAppender*>(getLogger()->getAppender(LOG4CXX_STR("counter")).get());
+               
LOGUNIT_ASSERT(LogManager::getLoggerRepository()->isConfigured());
                LOGUNIT_ASSERT(appender);
 
                auto myLogger = getLogger(LOG4CXX_STR("Controller"));
diff --git a/src/test/cpp/terminationtestcase.cpp 
b/src/test/cpp/terminationtestcase.cpp
index 548d979f..17205ed7 100644
--- a/src/test/cpp/terminationtestcase.cpp
+++ b/src/test/cpp/terminationtestcase.cpp
@@ -42,6 +42,7 @@ public:
                        auto asyncAppender = std::make_shared<AsyncAppender>();
                        asyncAppender->addAppender(vectorAppender);
                        r->getRootLogger()->addAppender(asyncAppender);
+                       r->setConfigured(true);
                        }
                );
        }

Reply via email to