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/01/15 20:39:36 UTC

[logging-log4cxx] branch LOGCXX-525 created (now cd843f1)

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

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


      at cd843f1  Create intermediate directories for both fixedwindowrollingpolicy and timebasedrollingpolicy

This branch includes the following new commits:

     new cd843f1  Create intermediate directories for both fixedwindowrollingpolicy and timebasedrollingpolicy

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[logging-log4cxx] 01/01: Create intermediate directories for both fixedwindowrollingpolicy and timebasedrollingpolicy

Posted by rm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit cd843f199ef28e0cea3d0538a88dfa03040748dd
Author: Robert Middleton <ro...@rm5248.com>
AuthorDate: Sat Jan 15 15:39:23 2022 -0500

    Create intermediate directories for both fixedwindowrollingpolicy and timebasedrollingpolicy
---
 src/main/cpp/fixedwindowrollingpolicy.cpp     |  6 +++++
 src/main/cpp/timebasedrollingpolicy.cpp       |  6 +++++
 src/test/cpp/rolling/manualrollingtest.cpp    | 37 +++++++++++++++++++++++++-
 src/test/cpp/rolling/timebasedrollingtest.cpp | 38 +++++++++++++++++++++++++++
 4 files changed, 86 insertions(+), 1 deletion(-)

diff --git a/src/main/cpp/fixedwindowrollingpolicy.cpp b/src/main/cpp/fixedwindowrollingpolicy.cpp
index 5c91857..7f0d8f5 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 ;
 
+	{
+		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/timebasedrollingpolicy.cpp b/src/main/cpp/timebasedrollingpolicy.cpp
index e967da6..b82f9f9 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));
 
+	{
+		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/test/cpp/rolling/manualrollingtest.cpp b/src/test/cpp/rolling/manualrollingtest.cpp
index 575b245..1c55503 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;
@@ -310,6 +311,40 @@ 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-");
+		filenamePattern.append( std::to_string(dist(rng)) );
+		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 + "/file-0.gz").exists(p));
+		LOGUNIT_ASSERT_EQUAL(true, File(filenamePatternPrefix + "/file-1.gz").exists(p));
+	}
+
 };
 
 
diff --git a/src/test/cpp/rolling/timebasedrollingtest.cpp b/src/test/cpp/rolling/timebasedrollingtest.cpp
index 316328e..9022bc9 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,42 @@ 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-");
+		filenamePattern.append( std::to_string(dist(rng)) );
+		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"));