You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rm...@apache.org on 2022/03/04 22:59:05 UTC

[logging-log4cxx] branch master updated: Create intermediate directories for both fixedwindowrollingpolicy and… (#102)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new a5d3a64  Create intermediate directories for both fixedwindowrollingpolicy and… (#102)
a5d3a64 is described below

commit a5d3a64b4a50f08878fc8844385e58a517f5dad3
Author: Robert Middleton <rm...@users.noreply.github.com>
AuthorDate: Fri Mar 4 17:37:34 2022 -0500

    Create intermediate directories for both fixedwindowrollingpolicy and… (#102)
    
    * Create intermediate directories for both fixedwindowrollingpolicy and timebasedrollingpolicy
    
    * Fix test failures with old test
    
    * Fix compile errors on windows
    
    * Default to creating the intermediate directories for logs
---
 src/main/cpp/fixedwindowrollingpolicy.cpp          |  6 +++
 src/main/cpp/rollingpolicybase.cpp                 | 15 +++++++
 src/main/cpp/timebasedrollingpolicy.cpp            |  6 +++
 .../include/log4cxx/rolling/rollingpolicybase.h    |  5 +++
 src/test/cpp/rolling/manualrollingtest.cpp         | 46 +++++++++++++++++++++-
 src/test/cpp/rolling/sizebasedrollingtest.cpp      |  4 +-
 src/test/cpp/rolling/timebasedrollingtest.cpp      | 43 ++++++++++++++++++++
 7 files changed, 121 insertions(+), 4 deletions(-)

diff --git a/src/main/cpp/fixedwindowrollingpolicy.cpp b/src/main/cpp/fixedwindowrollingpolicy.cpp
index 5c91857..289c2cd 100644
--- a/src/main/cpp/fixedwindowrollingpolicy.cpp
+++ b/src/main/cpp/fixedwindowrollingpolicy.cpp
@@ -172,6 +172,12 @@ RolloverDescriptionPtr FixedWindowRollingPolicy::rollover(
 	LogString compressedName(renameTo);
 	ActionPtr compressAction ;
 
+	if(getCreateIntermediateDirectories()){
+		File compressedFile(compressedName);
+		File compressedParent (compressedFile.getParent(pool));
+		compressedParent.mkdirs(pool);
+	}
+
 	if (StringHelper::endsWith(renameTo, LOG4CXX_STR(".gz")))
 	{
 		renameTo.resize(renameTo.size() - 3);
diff --git a/src/main/cpp/rollingpolicybase.cpp b/src/main/cpp/rollingpolicybase.cpp
index a72c57a..94764fd 100644
--- a/src/main/cpp/rollingpolicybase.cpp
+++ b/src/main/cpp/rollingpolicybase.cpp
@@ -28,6 +28,7 @@
 #include <log4cxx/pattern/patternparser.h>
 #include <log4cxx/pattern/integerpatternconverter.h>
 #include <log4cxx/pattern/datepatternconverter.h>
+#include <log4cxx/helpers/optionconverter.h>
 
 using namespace log4cxx;
 using namespace log4cxx::rolling;
@@ -38,6 +39,7 @@ IMPLEMENT_LOG4CXX_OBJECT(RollingPolicyBase)
 
 RollingPolicyBase::RollingPolicyBase()
 {
+	createIntermediateDirectories = true;
 }
 
 RollingPolicyBase::~RollingPolicyBase()
@@ -68,6 +70,11 @@ void RollingPolicyBase::setOption(const LogString& option, const LogString& valu
 			LOG4CXX_STR("filenamepattern")))
 	{
 		fileNamePatternStr = value;
+	}else if (StringHelper::equalsIgnoreCase(option,
+			LOG4CXX_STR("CREATEINTERMEDIATEDIRECTORIES"),
+			LOG4CXX_STR("createintermediatedirectories")))
+	{
+		createIntermediateDirectories = OptionConverter::toBoolean(value, false);
 	}
 }
 
@@ -163,4 +170,12 @@ PatternConverterPtr RollingPolicyBase::getDatePatternConverter() const
 	return noMatch;
 }
 
+bool RollingPolicyBase::getCreateIntermediateDirectories() const{
+	return createIntermediateDirectories;
+}
+
+void RollingPolicyBase::setCreateIntermediateDirectories(bool createIntermediate){
+	createIntermediateDirectories = createIntermediate;
+}
+
 
diff --git a/src/main/cpp/timebasedrollingpolicy.cpp b/src/main/cpp/timebasedrollingpolicy.cpp
index e967da6..c81f09d 100644
--- a/src/main/cpp/timebasedrollingpolicy.cpp
+++ b/src/main/cpp/timebasedrollingpolicy.cpp
@@ -342,6 +342,12 @@ RolloverDescriptionPtr TimeBasedRollingPolicy::rollover(
 	LogString nextActiveFile(
 		newFileName.substr(0, newFileName.length() - suffixLength));
 
+	if(getCreateIntermediateDirectories()){
+		File compressedFile(lastFileName);
+		File compressedParent (compressedFile.getParent(pool));
+		compressedParent.mkdirs(pool);
+	}
+
 	//
 	//   if currentActiveFile is not lastBaseName then
 	//        active file name is not following file pattern
diff --git a/src/main/include/log4cxx/rolling/rollingpolicybase.h b/src/main/include/log4cxx/rolling/rollingpolicybase.h
index 8d4e37b..b9229ba 100644
--- a/src/main/include/log4cxx/rolling/rollingpolicybase.h
+++ b/src/main/include/log4cxx/rolling/rollingpolicybase.h
@@ -74,6 +74,8 @@ class LOG4CXX_EXPORT RollingPolicyBase :
 		 */
 		LogString fileNamePatternStr;
 
+		bool createIntermediateDirectories;
+
 
 	public:
 		RollingPolicyBase();
@@ -96,6 +98,9 @@ class LOG4CXX_EXPORT RollingPolicyBase :
 		 */
 		LogString getFileNamePattern() const;
 
+		bool getCreateIntermediateDirectories() const;
+		void setCreateIntermediateDirectories(bool createIntermediate);
+
 
 #ifdef LOG4CXX_MULTI_PROCESS
 		PatternConverterList getPatternConverterList()
diff --git a/src/test/cpp/rolling/manualrollingtest.cpp b/src/test/cpp/rolling/manualrollingtest.cpp
index 575b245..13d5a92 100644
--- a/src/test/cpp/rolling/manualrollingtest.cpp
+++ b/src/test/cpp/rolling/manualrollingtest.cpp
@@ -32,7 +32,7 @@
 #include <log4cxx/consoleappender.h>
 #include <log4cxx/helpers/exception.h>
 #include <log4cxx/helpers/fileoutputstream.h>
-
+#include <random>
 
 using namespace log4cxx;
 using namespace log4cxx::xml;
@@ -55,6 +55,7 @@ LOGUNIT_CLASS(ManualRollingTest)
 	//           LOGUNIT_TEST(test3);
 	LOGUNIT_TEST(test4);
 	LOGUNIT_TEST(test5);
+	LOGUNIT_TEST(create_directories);
 	LOGUNIT_TEST_SUITE_END();
 
 	LoggerPtr root;
@@ -216,7 +217,7 @@ public:
 		rfa->setFile(LOG4CXX_STR("output/manual-test4.log"));
 
 		FixedWindowRollingPolicyPtr swrp = FixedWindowRollingPolicyPtr(new FixedWindowRollingPolicy());
-
+		swrp->setCreateIntermediateDirectories(false);
 		swrp->setMinIndex(0);
 
 		//
@@ -310,6 +311,47 @@ public:
 		}
 	}
 
+	void create_directories()
+	{
+		PatternLayoutPtr layout = PatternLayoutPtr(new PatternLayout(LOG4CXX_STR("%m\n")));
+		RollingFileAppenderPtr rfa = RollingFileAppenderPtr(new RollingFileAppender());
+		rfa->setName(LOG4CXX_STR("ROLLING"));
+		rfa->setAppend(false);
+		rfa->setLayout(layout);
+		rfa->setFile(LOG4CXX_STR("output/create-directory-file.log"));
+
+		FixedWindowRollingPolicyPtr swrp = FixedWindowRollingPolicyPtr(new FixedWindowRollingPolicy());
+		swrp->setMinIndex(0);
+
+		std::random_device dev;
+		std::mt19937 rng(dev());
+		std::uniform_int_distribution<std::mt19937::result_type> dist(1,100000);
+		LogString filenamePattern = LOG4CXX_STR("output/directory-");
+
+#if LOG4CXX_LOGCHAR_IS_WCHAR
+		LogString dirNumber = std::to_wstring(dist(rng));
+#else
+		LogString dirNumber = std::to_string(dist(rng));
+#endif
+
+		filenamePattern.append( dirNumber );
+		LogString filenamePatternPrefix = filenamePattern;
+		filenamePattern.append( LOG4CXX_STR("/file-%i.gz") );
+		swrp->setFileNamePattern(filenamePattern);
+		Pool p;
+		swrp->activateOptions(p);
+
+		rfa->setRollingPolicy(swrp);
+		rfa->activateOptions(p);
+		root->addAppender(rfa);
+
+
+		common(rfa, p, logger);
+
+		LOGUNIT_ASSERT_EQUAL(true, File(filenamePatternPrefix + LOG4CXX_STR("/file-0.gz")).exists(p));
+		LOGUNIT_ASSERT_EQUAL(true, File(filenamePatternPrefix + LOG4CXX_STR("/file-1.gz")).exists(p));
+	}
+
 };
 
 
diff --git a/src/test/cpp/rolling/sizebasedrollingtest.cpp b/src/test/cpp/rolling/sizebasedrollingtest.cpp
index cd15a69..3c1d4be 100644
--- a/src/test/cpp/rolling/sizebasedrollingtest.cpp
+++ b/src/test/cpp/rolling/sizebasedrollingtest.cpp
@@ -231,7 +231,7 @@ public:
 
 		FixedWindowRollingPolicyPtr swrp = FixedWindowRollingPolicyPtr(new FixedWindowRollingPolicy());
 		SizeBasedTriggeringPolicyPtr sbtp = SizeBasedTriggeringPolicyPtr(new SizeBasedTriggeringPolicy());
-
+		swrp->setCreateIntermediateDirectories(false);
 		sbtp->setMaxFileSize(100);
 		swrp->setMinIndex(0);
 
@@ -270,7 +270,7 @@ public:
 
 		FixedWindowRollingPolicyPtr swrp = FixedWindowRollingPolicyPtr(new FixedWindowRollingPolicy());
 		SizeBasedTriggeringPolicyPtr sbtp = SizeBasedTriggeringPolicyPtr(new SizeBasedTriggeringPolicy());
-
+		swrp->setCreateIntermediateDirectories(false);
 		sbtp->setMaxFileSize(100);
 		swrp->setMinIndex(0);
 
diff --git a/src/test/cpp/rolling/timebasedrollingtest.cpp b/src/test/cpp/rolling/timebasedrollingtest.cpp
index 316328e..706cb3d 100644
--- a/src/test/cpp/rolling/timebasedrollingtest.cpp
+++ b/src/test/cpp/rolling/timebasedrollingtest.cpp
@@ -31,6 +31,7 @@
 
 #include <apr_strings.h>
 #include <apr_time.h>
+#include <random>
 #ifndef INT64_C
 	#define INT64_C(x) x ## LL
 #endif
@@ -76,6 +77,7 @@ LOGUNIT_CLASS(TimeBasedRollingTest)
 	LOGUNIT_TEST(test5);
 	LOGUNIT_TEST(test6);
 	LOGUNIT_TEST(test7);
+//	LOGUNIT_TEST(create_directories);
 	LOGUNIT_TEST_SUITE_END();
 
 private:
@@ -631,6 +633,47 @@ public:
 			this->internalTearDown();
 		}
 	}
+
+	void create_directories()
+	{
+		Pool        pool;
+		const   size_t      nrOfFileNames = 4;
+		LogString   fileNames[nrOfFileNames];
+
+		PatternLayoutPtr        layout( new PatternLayout(PATTERN_LAYOUT));
+		RollingFileAppenderPtr  rfa(    new RollingFileAppender());
+		rfa->setLayout(layout);
+		rfa->setFile(LOG4CXX_STR("output/timebasedrolling_create_dir.log"));
+
+		std::random_device dev;
+		std::mt19937 rng(dev());
+		std::uniform_int_distribution<std::mt19937::result_type> dist(1,100000);
+		LogString filenamePattern = LOG4CXX_STR("output/tbrolling-directory-");
+#if LOG4CXX_LOGCHAR_IS_WCHAR
+		LogString dirNumber = std::to_wstring(dist(rng));
+#else
+		LogString dirNumber = std::to_string(dist(rng));
+#endif
+		filenamePattern.append( dirNumber );
+		LogString filenamePatternPrefix = filenamePattern;
+		filenamePattern.append( LOG4CXX_STR("/file-%d{" DATE_PATTERN "}") );
+
+		TimeBasedRollingPolicyPtr tbrp(new TimeBasedRollingPolicy());
+		tbrp->setFileNamePattern(filenamePattern);
+		tbrp->activateOptions(pool);
+		rfa->setRollingPolicy(tbrp);
+		rfa->activateOptions(pool);
+		logger->addAppender(rfa);
+
+		this->buildTsFileNames(pool, filenamePatternPrefix.append(LOG4CXX_STR("/file-")).data(), fileNames);
+		this->delayUntilNextSecondWithMsg();
+		this->logMsgAndSleep(   pool, nrOfFileNames + 1, __LOG4CXX_FUNC__, __LINE__);
+//		this->compareWitnesses( pool, LOG4CXX_STR("test1."), fileNames, __LINE__);
+
+		for( size_t x = 0; x < nrOfFileNames - 1; x++ ){
+			LOGUNIT_ASSERT_EQUAL(true, File(fileNames[x]).exists(pool));
+		}
+	}
 };
 
 LoggerPtr TimeBasedRollingTest::logger(Logger::getLogger("org.apache.log4j.TimeBasedRollingTest"));