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

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

commit 1d992e60b8becc3b6f7a0a16908315b31374d186
Author: Robert Middleton <[email protected]>
AuthorDate: Sun Jan 9 14:23:39 2022 -0500

    LOGCXX-102 Parse rolling file appender options from properties file
---
 src/main/cpp/propertyconfigurator.cpp              | 37 +++++++++-
 .../include/log4cxx/rolling/rollingfileappender.h  |  2 +-
 src/test/cpp/rolling/CMakeLists.txt                |  1 +
 .../rolling/rollingfileappenderpropertiestest.cpp  | 85 ++++++++++++++++++++++
 .../rollingFileAppenderFromProperties.properties   | 30 ++++++++
 5 files changed, 153 insertions(+), 2 deletions(-)

diff --git a/src/main/cpp/propertyconfigurator.cpp 
b/src/main/cpp/propertyconfigurator.cpp
index 49b998a..65efadc 100644
--- a/src/main/cpp/propertyconfigurator.cpp
+++ b/src/main/cpp/propertyconfigurator.cpp
@@ -36,6 +36,7 @@
 #include <log4cxx/helpers/fileinputstream.h>
 #include <log4cxx/helpers/loader.h>
 #include <log4cxx/helpers/threadutility.h>
+#include <log4cxx/rolling/rollingfileappender.h>
 
 #define LOG4CXX 1
 #include <log4cxx/helpers/aprinitializer.h>
@@ -45,7 +46,7 @@ using namespace log4cxx;
 using namespace log4cxx::spi;
 using namespace log4cxx::helpers;
 using namespace log4cxx::config;
-
+using namespace log4cxx::rolling;
 
 #if APR_HAS_THREADS
 #include <log4cxx/helpers/filewatchdog.h>
@@ -428,6 +429,8 @@ AppenderPtr PropertyConfigurator::parseAppender(
        // Appender was not previously initialized.
        LogString prefix = APPENDER_PREFIX + appenderName;
        LogString layoutPrefix = prefix + LOG4CXX_STR(".layout");
+       LogString rollingPolicyKey = prefix + LOG4CXX_STR(".rollingPolicy");
+       LogString triggeringPolicyKey = prefix + 
LOG4CXX_STR(".triggeringPolicy");
 
        std::shared_ptr<Object> obj =
                OptionConverter::instantiateByKey(
@@ -468,6 +471,38 @@ AppenderPtr PropertyConfigurator::parseAppender(
                        }
                }
 
+               RollingFileAppenderPtr rolling = 
log4cxx::cast<rolling::RollingFileAppender>(appender);
+               if(rolling)
+               {
+                       RollingPolicyPtr rollingPolicy;
+                       std::shared_ptr<Object> rolling_obj =
+                               OptionConverter::instantiateByKey(
+                                       props, rollingPolicyKey, 
RollingPolicy::getStaticClass(), 0);
+                       rollingPolicy = log4cxx::cast<RollingPolicy>( 
rolling_obj );
+                       if(rollingPolicy)
+                       {
+                               rolling->setRollingPolicy(rollingPolicy);
+
+                               LogLog::debug((LogString) LOG4CXX_STR("Parsing 
rolling policy options for \"")
+                                       + appenderName + LOG4CXX_STR("\"."));
+                               PropertySetter::setProperties(rollingPolicy, 
props, rollingPolicyKey + LOG4CXX_STR("."), p);
+                       }
+
+                       TriggeringPolicyPtr triggeringPolicy;
+                       std::shared_ptr<Object> triggering_obj =
+                               OptionConverter::instantiateByKey(
+                                       props, triggeringPolicyKey, 
TriggeringPolicy::getStaticClass(), 0);
+                       triggeringPolicy = log4cxx::cast<TriggeringPolicy>( 
triggering_obj );
+                       if(triggeringPolicy)
+                       {
+                               rolling->setTriggeringPolicy(triggeringPolicy);
+
+                               LogLog::debug((LogString) LOG4CXX_STR("Parsing 
triggering policy options for \"")
+                                       + appenderName + LOG4CXX_STR("\"."));
+                               PropertySetter::setProperties(triggeringPolicy, 
props, triggeringPolicyKey + LOG4CXX_STR("."), p);
+                       }
+               }
+
                //configureOptionHandler((OptionHandler) appender, prefix + 
_T("."), props);
                PropertySetter::setProperties(appender, props, prefix + 
LOG4CXX_STR("."), p);
                LogLog::debug((LogString) LOG4CXX_STR("Parsed \"")
diff --git a/src/main/include/log4cxx/rolling/rollingfileappender.h 
b/src/main/include/log4cxx/rolling/rollingfileappender.h
index 29ce553..ce178ac 100644
--- a/src/main/include/log4cxx/rolling/rollingfileappender.h
+++ b/src/main/include/log4cxx/rolling/rollingfileappender.h
@@ -57,7 +57,7 @@ namespace rolling
     &lt;/layout>
   &lt;/appender>
 
-  &lt;root">
+  &lt;root>
     &lt;appender-ref ref="ROLL"/>
   &lt;/root>
 
diff --git a/src/test/cpp/rolling/CMakeLists.txt 
b/src/test/cpp/rolling/CMakeLists.txt
index 8d972e5..81242fd 100644
--- a/src/test/cpp/rolling/CMakeLists.txt
+++ b/src/test/cpp/rolling/CMakeLists.txt
@@ -24,6 +24,7 @@ set(ROLLING_TESTS
     obsoleterollingfileappendertest
     sizebasedrollingtest
     timebasedrollingtest
+    rollingfileappenderpropertiestest
 )
 foreach(fileName  IN LISTS ROLLING_TESTS)
     add_executable(${fileName} "${fileName}.cpp")
diff --git a/src/test/cpp/rolling/rollingfileappenderpropertiestest.cpp 
b/src/test/cpp/rolling/rollingfileappenderpropertiestest.cpp
new file mode 100644
index 0000000..6426baa
--- /dev/null
+++ b/src/test/cpp/rolling/rollingfileappenderpropertiestest.cpp
@@ -0,0 +1,85 @@
+/*
+ * 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 "../util/compare.h"
+#include "../insertwide.h"
+#include "../logunit.h"
+#include <apr_time.h>
+#include <log4cxx/logmanager.h>
+#include <log4cxx/xml/domconfigurator.h>
+#include <log4cxx/patternlayout.h>
+#include <log4cxx/rolling/fixedwindowrollingpolicy.h>
+#include <log4cxx/rolling/sizebasedtriggeringpolicy.h>
+#include <log4cxx/filter/levelrangefilter.h>
+#include <log4cxx/helpers/pool.h>
+#include <log4cxx/logger.h>
+#include <log4cxx/propertyconfigurator.h>
+#include <log4cxx/rolling/rollingfileappender.h>
+#include <log4cxx/helpers/stringhelper.h>
+#include <log4cxx/consoleappender.h>
+#include <log4cxx/helpers/exception.h>
+#include <log4cxx/helpers/fileoutputstream.h>
+
+
+using namespace log4cxx;
+using namespace log4cxx::xml;
+using namespace log4cxx::filter;
+using namespace log4cxx::helpers;
+using namespace log4cxx::rolling;
+
+/**
+ *
+ */
+LOGUNIT_CLASS(RollingFileAppenderPropertiesTest)
+{
+       LOGUNIT_TEST_SUITE(RollingFileAppenderPropertiesTest);
+       LOGUNIT_TEST(testRollingFromProperties);
+       LOGUNIT_TEST_SUITE_END();
+
+public:
+       void setUp()
+       {
+       }
+
+       void tearDown()
+       {
+       }
+
+       void testRollingFromProperties(){
+               // Load the properties from the file and make sure that the 
configured values are
+               // what we expect
+               
PropertyConfigurator::configure(LOG4CXX_FILE("input/rolling/rollingFileAppenderFromProperties.properties"));
+
+               AppenderPtr appender = 
LogManager::getRootLogger()->getAppender("FILE");
+               LOGUNIT_ASSERT(appender);
+
+               RollingFileAppenderPtr rfa = 
cast<RollingFileAppender>(appender);
+               LOGUNIT_ASSERT(rfa);
+
+               FixedWindowRollingPolicyPtr fixedWindowRolling = 
cast<FixedWindowRollingPolicy>(rfa->getRollingPolicy());
+               LOGUNIT_ASSERT(fixedWindowRolling);
+
+               SizeBasedTriggeringPolicyPtr sizeBasedPolicy = 
cast<SizeBasedTriggeringPolicy>(rfa->getTriggeringPolicy());
+               LOGUNIT_ASSERT(sizeBasedPolicy);
+
+               LOGUNIT_ASSERT_EQUAL(3, fixedWindowRolling->getMaxIndex());
+               LOGUNIT_ASSERT_EQUAL(100, 
static_cast<int>(sizeBasedPolicy->getMaxFileSize()));
+       }
+
+};
+
+
+LOGUNIT_TEST_SUITE_REGISTRATION(RollingFileAppenderPropertiesTest);
diff --git 
a/src/test/resources/input/rolling/rollingFileAppenderFromProperties.properties 
b/src/test/resources/input/rolling/rollingFileAppenderFromProperties.properties
new file mode 100644
index 0000000..7766df6
--- /dev/null
+++ 
b/src/test/resources/input/rolling/rollingFileAppenderFromProperties.properties
@@ -0,0 +1,30 @@
+# 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.
+#
+log4j.rootLogger=DEBUG, FILE
+log4j.appender.FILE=RollingFileAppender
+log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
+log4j.appender.FILE.layout.ConversionPattern=%m%n
+log4j.appender.FILE.file=output/rollingFileFromProperties-test.log
+log4j.appender.FILE.rollingPolicy=FixedWindowRollingPolicy
+log4j.appender.FILE.rollingPolicy.MaxIndex=3
+log4j.appender.FILE.rollingPolicy.FileNamePattern=output/rollingFileFromProperties-test-%i.log
+log4j.appender.FILE.triggeringPolicy=SizeBasedTriggeringPolicy
+log4j.appender.FILE.triggeringPolicy.MaxFileSize=100
+
+#  Prevent internal log4j DEBUG messages from polluting the output.
+log4j.logger.org.apache.log4j.PropertyConfigurator=INFO
+log4j.logger.org.apache.log4j.config.PropertySetter=INFO
+log4j.logger.org.apache.log4j.FileAppender=INFO

Reply via email to