You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by sw...@apache.org on 2025/01/19 05:45:18 UTC

(logging-log4cxx) branch multiprocessappender_wchar_support created (now 8fc3723f)

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

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


      at 8fc3723f Remove duplicated code

This branch includes the following new commits:

     new 5aebece8 Support wchar_t logchar in MultiprocessRollingFileAppender
     new 8fc3723f Remove duplicated code

The 2 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/02: Support wchar_t logchar in MultiprocessRollingFileAppender

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

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

commit 5aebece8cb742661f92374d13cff01a225468c9a
Author: Stephen Webb <sw...@gmail.com>
AuthorDate: Sun Jan 19 15:25:24 2025 +1100

    Support wchar_t logchar in MultiprocessRollingFileAppender
---
 src/main/cpp/exception.cpp                       | 15 +++----
 src/main/cpp/timebasedrollingpolicy.cpp          | 54 ++++++++++++++----------
 src/main/include/log4cxx/helpers/exception.h     |  2 +-
 src/test/cpp/rolling/multiprocessrollingtest.cpp | 13 +++---
 4 files changed, 46 insertions(+), 38 deletions(-)

diff --git a/src/main/cpp/exception.cpp b/src/main/cpp/exception.cpp
index e45c3294..0ef952ab 100644
--- a/src/main/cpp/exception.cpp
+++ b/src/main/cpp/exception.cpp
@@ -154,7 +154,7 @@ IOException::IOException(log4cxx_status_t stat)
 }
 
 IOException::IOException(const LogString& type, log4cxx_status_t stat)
-	: Exception(formatMessage(type, stat))
+	: Exception(makeMessage(type, stat))
 {
 }
 
@@ -177,10 +177,10 @@ IOException& IOException::operator=(const IOException& src)
 
 LogString IOException::formatMessage(log4cxx_status_t stat)
 {
-	return formatMessage(LOG4CXX_STR("IO Exception"), stat);
+	return makeMessage(LOG4CXX_STR("IO Exception"), stat);
 }
 
-LogString IOException::formatMessage(const LogString& type, log4cxx_status_t stat)
+LogString Exception::makeMessage(const LogString& type, log4cxx_status_t stat)
 {
 	LogString s = type;
 	char err_buff[1024] = {0};
@@ -243,9 +243,9 @@ PoolException& PoolException::operator=(const PoolException& src)
 	return *this;
 }
 
-LogString PoolException::formatMessage(log4cxx_status_t)
+LogString PoolException::formatMessage(log4cxx_status_t stat)
 {
-	return LOG4CXX_STR("Pool exception");
+	return makeMessage(LOG4CXX_STR("Pool exception"), stat);
 }
 
 
@@ -322,10 +322,7 @@ ThreadException& ThreadException::operator=(const ThreadException& src)
 
 LogString ThreadException::formatMessage(log4cxx_status_t stat)
 {
-	LogString s(LOG4CXX_STR("Thread exception: stat = "));
-	Pool p;
-	StringHelper::toString(stat, p, s);
-	return s;
+	return makeMessage(LOG4CXX_STR("Thread exception"), stat);
 }
 
 IllegalMonitorStateException::IllegalMonitorStateException(const LogString& msg1)
diff --git a/src/main/cpp/timebasedrollingpolicy.cpp b/src/main/cpp/timebasedrollingpolicy.cpp
index bb1d7ba1..6128b861 100644
--- a/src/main/cpp/timebasedrollingpolicy.cpp
+++ b/src/main/cpp/timebasedrollingpolicy.cpp
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
+#define NOMINMAX /* tell windows to not define min/max macros */
 #include <log4cxx/log4cxx.h>
 #include <log4cxx/logstring.h>
 #include <log4cxx/rolling/timebasedrollingpolicy.h>
@@ -27,6 +27,7 @@
 #include <log4cxx/rolling/zipcompressaction.h>
 #include <log4cxx/helpers/stringhelper.h>
 #include <log4cxx/helpers/optionconverter.h>
+#include <log4cxx/helpers/transcoder.h>
 #include <log4cxx/fileappender.h>
 #include <iostream>
 #include <apr_mmap.h>
@@ -127,11 +128,11 @@ bool TimeBasedRollingPolicy::isMapFileEmpty(LOG4CXX_NS::helpers::Pool& pool)
 
 	if (st != APR_SUCCESS)
 	{
-		LogLog::warn(LOG4CXX_STR("apr_stat failed."));
+		LogLog::warn(helpers::Exception::makeMessage(LOG4CXX_STR("apr_stat"), st));
 	}
 
 	if (st == APR_SUCCESS && (0 == finfo.size ||
-		(m_priv->_mmap && 0 == *static_cast<char*>(m_priv->_mmap->mm))))
+		(m_priv->_mmap && 0 == *static_cast<logchar*>(m_priv->_mmap->mm))))
 	{
 		return true;
 	}
@@ -145,14 +146,16 @@ void TimeBasedRollingPolicy::initMMapFile(const LogString& lastFileName, LOG4CXX
 
 	if (!m_priv->_mmap)
 	{
-		iRet = createMMapFile(std::string(m_priv->_fileNamePattern), pool);
+		LOG4CXX_ENCODE_CHAR(mapFile, m_priv->_fileNamePattern);
+		iRet = createMMapFile(mapFile, pool);
 	}
 
 	if (!iRet && isMapFileEmpty(pool))
 	{
 		lockMMapFile(APR_FLOCK_EXCLUSIVE);
 		memset(m_priv->_mmap->mm, 0, MAX_FILE_LEN);
-		memcpy(m_priv->_mmap->mm, std::string(lastFileName).c_str(), std::string(lastFileName).size());
+		size_t byteCount = std::min(MAX_FILE_LEN - sizeof (logchar), sizeof (logchar) * lastFileName.size());
+		memcpy(m_priv->_mmap->mm, lastFileName.c_str(), byteCount);
 		unLockMMapFile();
 	}
 }
@@ -177,8 +180,11 @@ int TimeBasedRollingPolicy::createMMapFile(const std::string& fileName, LOG4CXX_
 
 	if (stat != APR_SUCCESS)
 	{
-		std::string err(std::string("open mmap file failed. ") + std::string(strerror(errno)) + ". Check the privilege or try to remove " + m_priv->_mapFileName + " if exist.");
-		LogLog::warn(LOG4CXX_STR(err.c_str()));
+		LogString msg = helpers::Exception::makeMessage(LOG4CXX_STR("apr_file_open"), stat);
+		msg += LOG4CXX_STR(". Check the privilege or try to remove [");
+		helpers::Transcoder::decode(m_priv->_mapFileName, msg);
+		msg += LOG4CXX_STR("] if it exists.");
+		LogLog::warn(msg);
 		return -1;
 	}
 
@@ -188,7 +194,7 @@ int TimeBasedRollingPolicy::createMMapFile(const std::string& fileName, LOG4CXX_
 
 		if (stat != APR_SUCCESS)
 		{
-			LogLog::warn(LOG4CXX_STR("apr_file_trunc failed."));
+			LogLog::warn(helpers::Exception::makeMessage(LOG4CXX_STR("apr_file_trunc"), stat));
 			apr_file_close(m_priv->_file_map);
 			return -1;
 		}
@@ -198,7 +204,7 @@ int TimeBasedRollingPolicy::createMMapFile(const std::string& fileName, LOG4CXX_
 
 	if (stat != APR_SUCCESS)
 	{
-		LogLog::warn(LOG4CXX_STR("mmap failed."));
+		LogLog::warn(helpers::Exception::makeMessage(LOG4CXX_STR("apr_mmap_create"), stat));
 		apr_file_close(m_priv->_file_map);
 		return -1;
 	}
@@ -212,7 +218,7 @@ int TimeBasedRollingPolicy::lockMMapFile(int type)
 
 	if (stat != APR_SUCCESS)
 	{
-		LogLog::warn(LOG4CXX_STR("apr_file_lock for mmap failed."));
+		LogLog::warn(helpers::Exception::makeMessage(LOG4CXX_STR("apr_file_lock for mmap"), stat));
 	}
 
 	return stat;
@@ -224,7 +230,7 @@ int TimeBasedRollingPolicy::unLockMMapFile()
 
 	if (stat != APR_SUCCESS)
 	{
-		LogLog::warn(LOG4CXX_STR("apr_file_unlock for mmap failed."));
+		LogLog::warn(helpers::Exception::makeMessage(LOG4CXX_STR("apr_file_unlock for mmap"), stat));
 	}
 
 	return stat;
@@ -278,7 +284,7 @@ void TimeBasedRollingPolicy::activateOptions(LOG4CXX_NS::helpers::Pool& pool)
 
 	if (dtc == NULL)
 	{
-		throw IllegalStateException();
+		throw NullPointerException(LOG4CXX_STR("DatePatternConverter"));
 	}
 
 	LogString buf;
@@ -379,12 +385,15 @@ RolloverDescriptionPtr TimeBasedRollingPolicy::rollover(
 
 			if (!m_priv->_lock_file)
 			{
-				const std::string lockname = createFile(std::string(m_priv->_fileNamePattern), LOCK_FILE_SUFFIX, m_priv->_mmapPool);
+				LOG4CXX_ENCODE_CHAR(mapFile, m_priv->_fileNamePattern);
+				const std::string lockname = createFile(mapFile, LOCK_FILE_SUFFIX, m_priv->_mmapPool);
 				apr_status_t stat = apr_file_open(&m_priv->_lock_file, lockname.c_str(), APR_CREATE | APR_READ | APR_WRITE, APR_OS_DEFAULT, m_priv->_mmapPool.getAPRPool());
 
 				if (stat != APR_SUCCESS)
 				{
-					LogLog::warn(lockname + LOG4CXX_STR(": apr_file_open failed."));
+					LOG4CXX_DECODE_CHAR(msg, lockname);
+					msg += LOG4CXX_STR(": apr_file_open");
+					LogLog::warn(helpers::Exception::makeMessage(msg, stat));
 				}
 			}
 
@@ -395,7 +404,7 @@ RolloverDescriptionPtr TimeBasedRollingPolicy::rollover(
 		if (m_priv->_mmap && !isMapFileEmpty(m_priv->_mmapPool))
 		{
 			lockMMapFile(APR_FLOCK_SHARED);
-			LogString mapLastFile((char*)m_priv->_mmap->mm);
+			LogString mapLastFile(static_cast<logchar*>(m_priv->_mmap->mm));
 			m_priv->lastFileName = mapLastFile;
 			unLockMMapFile();
 		}
@@ -462,7 +471,8 @@ RolloverDescriptionPtr TimeBasedRollingPolicy::rollover(
 		{
 			lockMMapFile(APR_FLOCK_EXCLUSIVE);
 			memset(m_priv->_mmap->mm, 0, MAX_FILE_LEN);
-			memcpy(m_priv->_mmap->mm, std::string(newFileName).c_str(), std::string(newFileName).size());
+			size_t byteCount = std::min(MAX_FILE_LEN - sizeof (logchar), sizeof (logchar) * newFileName.size());
+			memcpy(m_priv->_mmap->mm, newFileName.c_str(), byteCount);
 			unLockMMapFile();
 		}
 		else
@@ -489,16 +499,14 @@ bool TimeBasedRollingPolicy::isTriggeringEvent(
 		if (m_priv->bRefreshCurFile && m_priv->_mmap && !isMapFileEmpty(m_priv->_mmapPool))
 		{
 			lockMMapFile(APR_FLOCK_SHARED);
-			LogString mapCurrent((char*)m_priv->_mmap->mm);
+			LogString mapCurrent(static_cast<logchar*>(m_priv->_mmap->mm));
 			unLockMMapFile();
 			LogString mapCurrentBase(mapCurrent.substr(0, mapCurrent.length() - m_priv->suffixLength));
 
 			if (!mapCurrentBase.empty() && mapCurrentBase != filename)
 			{
-				const FileAppender* fappend = reinterpret_cast<const FileAppender*>(appender->cast(FileAppender::getStaticClass()));
-				if( fappend ){
-					const_cast<FileAppender*>(fappend)->setFile(mapCurrentBase);
-				}
+				if (auto fappend = dynamic_cast<FileAppender*>(appender))
+					fappend->setFile(mapCurrentBase);
 			}
 		}
 
@@ -542,7 +550,7 @@ bool TimeBasedRollingPolicy::isLastFileNameUnchanged()
 		if (m_priv->_mmap)
 		{
 			lockMMapFile(APR_FLOCK_SHARED);
-			LogString mapCurrent((char*)m_priv->_mmap->mm);
+			LogString mapCurrent(static_cast<logchar*>(m_priv->_mmap->mm));
 			unLockMMapFile();
 			result = (mapCurrent == m_priv->lastFileName);
 		}
@@ -561,7 +569,7 @@ void TimeBasedRollingPolicy::loadLastFileName()
 		if (m_priv->_mmap)
 		{
 			lockMMapFile(APR_FLOCK_SHARED);
-			LogString mapLastFile((char*)m_priv->_mmap->mm);
+			LogString mapLastFile(static_cast<logchar*>(m_priv->_mmap->mm));
 			unLockMMapFile();
 			if (!mapLastFile.empty())
 				m_priv->lastFileName = mapLastFile;
diff --git a/src/main/include/log4cxx/helpers/exception.h b/src/main/include/log4cxx/helpers/exception.h
index 08f48d16..c80d1f22 100644
--- a/src/main/include/log4cxx/helpers/exception.h
+++ b/src/main/include/log4cxx/helpers/exception.h
@@ -42,6 +42,7 @@ class LOG4CXX_EXPORT Exception : public ::std::exception
 		Exception(const Exception& src);
 		Exception& operator=(const Exception& src);
 		const char* what() const throw();
+		static LogString makeMessage(const LogString& type, log4cxx_status_t stat);
 	private:
 		enum { MSG_SIZE = 128 };
 		char msg[MSG_SIZE + 1];
@@ -97,7 +98,6 @@ class LOG4CXX_EXPORT IOException : public Exception
 		IOException& operator=(const IOException&);
 	private:
 		static LogString formatMessage(log4cxx_status_t stat);
-		static LogString formatMessage(const LogString& type, log4cxx_status_t stat);
 };
 
 class LOG4CXX_EXPORT MissingResourceException : public Exception
diff --git a/src/test/cpp/rolling/multiprocessrollingtest.cpp b/src/test/cpp/rolling/multiprocessrollingtest.cpp
index 7b12dba2..cf365f3d 100644
--- a/src/test/cpp/rolling/multiprocessrollingtest.cpp
+++ b/src/test/cpp/rolling/multiprocessrollingtest.cpp
@@ -23,6 +23,7 @@
 #include <log4cxx/rolling/multiprocessrollingfileappender.h>
 #include <log4cxx/rolling/sizebasedtriggeringpolicy.h>
 #include <log4cxx/helpers/strftimedateformat.h>
+#include <log4cxx/helpers/transcoder.h>
 #include <log4cxx/helpers/date.h>
 #include <log4cxx/helpers/fileoutputstream.h>
 #include <log4cxx/helpers/loglog.h>
@@ -108,7 +109,7 @@ public:
 	 */
 	void test2()
 	{
-		std::string expectedPrefix = LOG4CXX_STR("multiprocess-2-");
+		std::string expectedPrefix{ "multiprocess-2-" };
 		// remove any previously generated files
 		std::filesystem::path outputDir("output/rolling");
 		if (!exists(outputDir))
@@ -136,14 +137,16 @@ public:
 		}
 
 		// Count rolled files
+		LOG4CXX_DECODE_CHAR(expectedPrefixLS, expectedPrefix);
 		helpers::Pool p;
-		helpers::StrftimeDateFormat("%Y-%m-%d").format(expectedPrefix, helpers::Date::currentTime(), p);
+		helpers::StrftimeDateFormat(LOG4CXX_STR("%Y-%m-%d")).format(expectedPrefixLS, helpers::Date::currentTime(), p);
+		LOG4CXX_ENCODE_CHAR(rolledPrefix, expectedPrefixLS);
 		int fileCount = 0;
 		for (auto const& dir_entry : std::filesystem::directory_iterator{"output/rolling"})
 		{
-			LogString filename(dir_entry.path().filename().string());
-			if (expectedPrefix.size() < filename.size() &&
-				filename.substr(0, expectedPrefix.size()) == expectedPrefix)
+			std::string filename(dir_entry.path().filename().string());
+			if (rolledPrefix.size() < filename.size() &&
+				filename.substr(0, rolledPrefix.size()) == rolledPrefix)
 				++fileCount;
 		}
 		LOGUNIT_ASSERT(1 < fileCount);


(logging-log4cxx) 02/02: Remove duplicated code

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

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

commit 8fc3723f0967d1cebcbd6928178a86118143d22b
Author: Stephen Webb <sw...@gmail.com>
AuthorDate: Sun Jan 19 16:40:14 2025 +1100

    Remove duplicated code
---
 src/test/cpp/net/socketappendertestcase.cpp | 18 +-----------------
 1 file changed, 1 insertion(+), 17 deletions(-)

diff --git a/src/test/cpp/net/socketappendertestcase.cpp b/src/test/cpp/net/socketappendertestcase.cpp
index b64c1ee0..2d5accdd 100644
--- a/src/test/cpp/net/socketappendertestcase.cpp
+++ b/src/test/cpp/net/socketappendertestcase.cpp
@@ -160,23 +160,7 @@ class SocketAppenderTestCase : public AppenderSkeletonTestCase
 			}
 			if (helpers::LogLog::isDebugEnabled())
 			{
-				LogString msg = LOG4CXX_STR("apr_socket_recv terminated");
-				char err_buff[1024] = {0};
-				apr_strerror(status, err_buff, sizeof(err_buff));
-				if (0 == err_buff[0] || 0 == strncmp(err_buff, "APR does not understand", 23))
-				{
-					msg.append(LOG4CXX_STR(": error code "));
-					helpers::Pool p;
-					helpers::StringHelper::toString(status, p, msg);
-				}
-				else
-				{
-					msg.append(LOG4CXX_STR(" - "));
-					std::string sMsg = err_buff;
-					LOG4CXX_DECODE_CHAR(lsMsg, sMsg);
-					msg.append(lsMsg);
-				}
-				helpers::LogLog::debug(msg);
+				helpers::LogLog::debug(helpers::Exception::makeMessage(LOG4CXX_STR("apr_socket_recv terminated"), status));
 			}
 			incomingSocket->close();
 			serverSocket->close();