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/10/22 13:34:58 UTC

[logging-log4cxx] 02/02: Use std::filesystem

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

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

commit 1649a22595f2bb4b13d4658efe2e2fc8741be970
Author: Robert Middleton <ro...@rm5248.com>
AuthorDate: Sat Oct 22 09:34:49 2022 -0400

    Use std::filesystem
---
 CMakeLists.txt                                         |  2 ++
 src/cmake/boost-fallback/boost-fallback.cmake          | 13 +++++++++++++
 .../boost-fallback/boost-std-configuration.h.cmake     | 18 ++++++++++++++++++
 src/cmake/boost-fallback/test-boostfilesystem.cpp      |  6 ++++++
 src/cmake/boost-fallback/test-stdfilesystem.cpp        |  5 +++++
 src/main/cpp/CMakeLists.txt                            |  4 ++++
 src/main/cpp/timebasedrollingpolicy.cpp                | 13 ++++++++++---
 src/main/include/CMakeLists.txt                        |  8 ++++++++
 8 files changed, 66 insertions(+), 3 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7accac18..f67d1c6b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -186,6 +186,7 @@ get_directory_property( SMART_PTR_IMPL DIRECTORY src/main/include DEFINITION SMA
 get_directory_property( MUTEX_IMPL DIRECTORY src/main/include DEFINITION MUTEX_IMPL )
 get_directory_property( SHARED_MUTEX_IMPL DIRECTORY src/main/include DEFINITION SHARED_MUTEX_IMPL )
 get_directory_property( ATOMIC_IMPL DIRECTORY src/main/include DEFINITION ATOMIC_IMPL )
+get_directory_property( FILESYSTEM_IMPL DIRECTORY src/main/include DEFINITION FILESYSTEM_IMPL )
 
 foreach(varName HAS_STD_LOCALE  HAS_ODBC  HAS_MBSRTOWCS  HAS_WCSTOMBS  HAS_FWIDE  HAS_LIBESMTP  HAS_SYSLOG)
   if(${varName} EQUAL 0)
@@ -265,6 +266,7 @@ message(STATUS "  mutex implementation ............ : ${MUTEX_IMPL}")
 message(STATUS "  shared_ptr implementation ....... : ${SMART_PTR_IMPL}")
 message(STATUS "  shared_mutex implementation ..... : ${SHARED_MUTEX_IMPL}")
 message(STATUS "  atomic implementation ........... : ${ATOMIC_IMPL}")
+message(STATUS "  filesystem implementation ....... : ${FILESYSTEM_IMPL}")
 
 if(BUILD_TESTING)
 message(STATUS "Applications required for tests:")
diff --git a/src/cmake/boost-fallback/boost-fallback.cmake b/src/cmake/boost-fallback/boost-fallback.cmake
index 7454c016..3a71c929 100644
--- a/src/cmake/boost-fallback/boost-fallback.cmake
+++ b/src/cmake/boost-fallback/boost-fallback.cmake
@@ -7,6 +7,7 @@
 # thread
 # mutex
 # shared_mutex
+# filesystem
 #
 # Variables set:
 # ${prefix}_
@@ -41,6 +42,10 @@
 # Smart pointer variables set:
 # STD_SHARED_PTR_FOUND - if std::shared_ptr is found
 # Boost_SHARED_PTR_FOUND - if boost::shared_ptr is found
+#
+# Filesystem variables set:
+# STD_FILESYSTEM_FOUND - if std::filesystem is found
+# Boost_FILESYSTEM_FOUND - if boost::filesystem is found
 
 include(FindThreads)
 
@@ -54,6 +59,8 @@ try_compile(STD_SHARED_PTR_FOUND "${CMAKE_BINARY_DIR}/boost-fallback-compile-tes
     "${CMAKE_CURRENT_LIST_DIR}/test-stdsharedptr.cpp")
 try_compile(STD_ATOMIC_FOUND "${CMAKE_BINARY_DIR}/boost-fallback-compile-tests"
     "${CMAKE_CURRENT_LIST_DIR}/test-stdatomic.cpp")
+try_compile(STD_FILESYSTEM_FOUND "${CMAKE_BINARY_DIR}/boost-fallback-compile-tests"
+    "${CMAKE_CURRENT_LIST_DIR}/test-stdfilesystem.cpp")
 
 # We need to have all three boost components in order to run our tests
 # Boost thread requires chrono and atomic to work
@@ -70,6 +77,12 @@ if( ${Boost_FOUND} )
         "${CMAKE_CURRENT_LIST_DIR}/test-boostatomic.cpp")
 endif( ${Boost_FOUND} )
 
+find_package(Boost COMPONENTS filesystem)
+if( ${Boost_FOUND} )
+    try_compile(Boost_FILESYSTEM_FOUND "${CMAKE_BINARY_DIR}/boost-fallback-compile-tests"
+        "${CMAKE_CURRENT_LIST_DIR}/test-boostfilesystem.cpp")
+endif( ${Boost_FOUND} )
+
 # Link the target with the appropriate boost libraries(if required)
 function(boostfallback_link target)
     if(NOT ${STD_THREAD_FOUND})
diff --git a/src/cmake/boost-fallback/boost-std-configuration.h.cmake b/src/cmake/boost-fallback/boost-std-configuration.h.cmake
index cd0d8f7c..1e5dcf5a 100644
--- a/src/cmake/boost-fallback/boost-std-configuration.h.cmake
+++ b/src/cmake/boost-fallback/boost-std-configuration.h.cmake
@@ -3,6 +3,8 @@
 
 #cmakedefine01 STD_SHARED_MUTEX_FOUND
 #cmakedefine01 Boost_SHARED_MUTEX_FOUND
+#cmakedefine01 STD_FILESYSTEM_FOUND
+#cmakedefine01 Boost_FILESYSTEM_FOUND
 
 #if STD_SHARED_MUTEX_FOUND
 #include <shared_mutex>
@@ -20,4 +22,20 @@ namespace ${NAMESPACE_ALIAS} {
 }
 #endif
 
+#if STD_FILESYSTEM_FOUND
+#include <filesystem>
+namespace ${NAMESPACE_ALIAS} {
+namespace filesystem {
+    typedef std::filesystem::path path;
+}
+}
+#elif Boost_FILESYSTEM_FOUND
+#include <boost/filesystem.hpp>
+namespace ${NAMESPACE_ALIAS} {
+namespace filesystem {
+    typedef boost::filesystem::path path;
+}
+}
+#endif
+
 #endif /* BOOST_STD_CONFIGURATION_H */
diff --git a/src/cmake/boost-fallback/test-boostfilesystem.cpp b/src/cmake/boost-fallback/test-boostfilesystem.cpp
new file mode 100644
index 00000000..137f8c96
--- /dev/null
+++ b/src/cmake/boost-fallback/test-boostfilesystem.cpp
@@ -0,0 +1,6 @@
+#include <boost/filesystem.hpp>
+
+int main(int argc, char** argv){
+	boost::filesystem::path p;
+}
+
diff --git a/src/cmake/boost-fallback/test-stdfilesystem.cpp b/src/cmake/boost-fallback/test-stdfilesystem.cpp
new file mode 100644
index 00000000..6c45325f
--- /dev/null
+++ b/src/cmake/boost-fallback/test-stdfilesystem.cpp
@@ -0,0 +1,5 @@
+#include <filesystem>
+
+int main(int argc, char** argv){
+	std::filesystem::path p;
+}
diff --git a/src/main/cpp/CMakeLists.txt b/src/main/cpp/CMakeLists.txt
index 8837f1da..e1fa6ba4 100644
--- a/src/main/cpp/CMakeLists.txt
+++ b/src/main/cpp/CMakeLists.txt
@@ -193,6 +193,10 @@ set_target_properties(log4cxx PROPERTIES
   SOVERSION ${LIBLOG4CXX_LIB_SOVERSION}
 )
 boostfallback_link(log4cxx)
+get_directory_property( FILESYSTEM_IMPL DIRECTORY "${CMAKE_SOURCE_DIR}/src/main/include" DEFINITION FILESYSTEM_IMPL )
+if("${FILESYSTEM_IMPL}" STREQUAL "std::filesystem" )
+  target_link_libraries(log4cxx PUBLIC $<$<AND:$<CXX_COMPILER_ID:GNU>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,9.0>>:stdc++fs>)
+endif()
 
 if(LOG4CXX_ABI_CHECK)
     message("Getting dependencies for ABI compatability check...")
diff --git a/src/main/cpp/timebasedrollingpolicy.cpp b/src/main/cpp/timebasedrollingpolicy.cpp
index ec2d6a64..15ca7563 100644
--- a/src/main/cpp/timebasedrollingpolicy.cpp
+++ b/src/main/cpp/timebasedrollingpolicy.cpp
@@ -30,8 +30,8 @@
 #include <log4cxx/helpers/stringhelper.h>
 #include <log4cxx/helpers/optionconverter.h>
 #include <log4cxx/fileappender.h>
+#include <log4cxx/boost-std-configuration.h>
 #include <iostream>
-#include <libgen.h>
 
 using namespace log4cxx;
 using namespace log4cxx::rolling;
@@ -169,8 +169,10 @@ const std::string TimeBasedRollingPolicy::createFile(const std::string& fileName
 		snprintf(szUid, MAX_FILE_LEN, "%u", uid);
 	}
 
-	// TODO this should probably be using boost::filesystem or std::filesystem
-	return std::string(::dirname(szDirName)) + "/." + ::basename(szBaseName) + szUid + suffix;
+	log4cxx::filesystem::path path(fileName);
+	std::string newFilename = path.filename().string() + szUid + suffix;
+	log4cxx::filesystem::path retval = path.parent_path() / newFilename;
+	return retval.string();
 }
 
 int TimeBasedRollingPolicy::createMMapFile(const std::string& fileName, log4cxx::helpers::Pool& pool)
@@ -218,7 +220,10 @@ int TimeBasedRollingPolicy::lockMMapFile(int type)
 	{
 		LogLog::warn(LOG4CXX_STR("apr_file_lock for mmap failed."));
 	}
+
+	return stat;
 }
+
 int TimeBasedRollingPolicy::unLockMMapFile()
 {
 	apr_status_t stat = apr_file_unlock(m_priv->_lock_file);
@@ -227,6 +232,8 @@ int TimeBasedRollingPolicy::unLockMMapFile()
 	{
 		LogLog::warn(LOG4CXX_STR("apr_file_unlock for mmap failed."));
 	}
+
+	return stat;
 }
 
 TimeBasedRollingPolicy::TimeBasedRollingPolicy() :
diff --git a/src/main/include/CMakeLists.txt b/src/main/include/CMakeLists.txt
index caba4629..499ef852 100644
--- a/src/main/include/CMakeLists.txt
+++ b/src/main/include/CMakeLists.txt
@@ -206,6 +206,14 @@ else()
     set( ATOMIC_IMPL "NONE" )
 endif()
 
+if( ${STD_FILESYSTEM_FOUND} AND NOT ${PREFER_BOOST} )
+	set( FILESYSTEM_IMPL "std::filesystem" )
+elseif( ${Boost_FILESYSTEM_FOUND} )
+	set( FILESYSTEM_IMPL "boost::filesystem" )
+else()
+	set( FILESYSTEM_IMPL "NONE" )
+endif()
+
 # Configure both our private header and our public header
 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/log4cxx/private/log4cxx_private.h.in
                ${CMAKE_CURRENT_BINARY_DIR}/log4cxx/private/log4cxx_private.h