This is an automated email from the ASF dual-hosted git repository. swebb2066 pushed a commit to branch prevent_stack_overflow in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git
commit c177a65b2b980fcfc5414f1bd969e58c64113839 Author: Stephen Webb <[email protected]> AuthorDate: Tue Aug 25 11:48:58 2026 +1000 Using default configuration, BufferedIO and a watchdog caused recursive configuration --- src/test/cpp/CMakeLists.txt | 1 + src/test/cpp/defaultconfiguratortest.cpp | 78 ++++++++++++++++++++++ .../resources/input/defaultconfiguratortest.xml | 28 ++++++++ 3 files changed, 107 insertions(+) diff --git a/src/test/cpp/CMakeLists.txt b/src/test/cpp/CMakeLists.txt index 913a5512..e3d82518 100644 --- a/src/test/cpp/CMakeLists.txt +++ b/src/test/cpp/CMakeLists.txt @@ -49,6 +49,7 @@ set(ALL_LOG4CXX_TESTS asyncappendertestcase asyncappenderracestress consoleappendertestcase + defaultconfiguratortest decodingtest encodingtest fileappendertest diff --git a/src/test/cpp/defaultconfiguratortest.cpp b/src/test/cpp/defaultconfiguratortest.cpp new file mode 100644 index 00000000..999c9d55 --- /dev/null +++ b/src/test/cpp/defaultconfiguratortest.cpp @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "logunit.h" +#include <log4cxx/logmanager.h> +#include <log4cxx/defaultconfigurator.h> +#include <log4cxx/basicconfigurator.h> + +using namespace log4cxx; + +namespace +{ + auto GetLogger(const std::string& name) -> LoggerPtr + { + static struct log4cxx_initializer + { + log4cxx_initializer() + { + // Check every 5 seconds for configuration file changes + DefaultConfigurator::setConfigurationWatchSeconds(5); + + // Look for a configuration file in the current working directory + // and the same directory as the program + std::vector<LogString> paths + { LOG4CXX_STR("input") + , LOG4CXX_STR("${PROGRAM_FILE_PATH.PARENT_PATH}") + }; + std::vector<LogString> names + { LOG4CXX_STR("${PROGRAM_FILE_PATH.STEM}.xml") + , LOG4CXX_STR("${PROGRAM_FILE_PATH.STEM}.properties") + }; + auto status = spi::ConfigurationStatus::NotConfigured; + auto selectedPath = LogString(); + std::tie(status, selectedPath) = DefaultConfigurator::configureFromFile(paths, names); + if (status == spi::ConfigurationStatus::NotConfigured) + BasicConfigurator::configure(); // Send events to the console + } + ~log4cxx_initializer() + { + LogManager::shutdown(); + } + } initialiser; + return name.empty() + ? LogManager::getRootLogger() + : LogManager::getLogger(name); + } + + auto logger = GetLogger("com.test"); +} + +LOGUNIT_CLASS(DefaultConfiguratorTest) +{ + LOGUNIT_TEST_SUITE(DefaultConfiguratorTest); + LOGUNIT_TEST(test1); + LOGUNIT_TEST_SUITE_END(); +public: + + void test1() + { + LOGUNIT_ASSERT(logger); + LOGUNIT_ASSERT(logger->isDebugEnabled()); + } +}; + +LOGUNIT_TEST_SUITE_REGISTRATION(DefaultConfiguratorTest); diff --git a/src/test/resources/input/defaultconfiguratortest.xml b/src/test/resources/input/defaultconfiguratortest.xml new file mode 100644 index 00000000..ac4ee5b4 --- /dev/null +++ b/src/test/resources/input/defaultconfiguratortest.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true"> + + <appender name="FileAppender" class="org.apache.log4j.RollingFileAppender"> + <param name="file" value="${PROGRAM_FILE_PATH.PARENT_PATH}/${PROGRAM_FILE_PATH.STEM}.log" /> + <param name="BufferedIO" value="true" /> + <layout class="org.apache.log4j.PatternLayout"> + <param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] %c %-5p - %m%n" /> + </layout> + <rollingPolicy class="org.apache.log4j.rolling.FixedWindowRollingPolicy"> + <param name="fileNamePattern" value="${PROGRAM_FILE_PATH.PARENT_PATH}/${PROGRAM_FILE_PATH.STEM}.%i.log"/> + <param name="minIndex" value="0"/> + </rollingPolicy> + <triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy"> + <param name="maxFileSize" value="4MB" /> + </triggeringPolicy> + </appender> + + <root> + <priority value="info" /> + <appender-ref ref="FileAppender"/> + </root> + + <logger name="com" > + <priority value="debug"/> + </logger> + +</log4j:configuration> \ No newline at end of file
