This is an automated email from the ASF dual-hosted git repository. rmiddleton pushed a commit to branch LOGCXX-574 in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git
commit 2df0dcdfe759e1800332d1b48e5f12e9458fe0fa Author: Robert Middleton <[email protected]> AuthorDate: Mon Dec 26 15:00:58 2022 -0500 LOGCXX-574 Provide a list of files/directories to look in for files --- src/main/cpp/defaultconfigurator.cpp | 37 ++++++++++++++++++++++++++ src/main/include/log4cxx/defaultconfigurator.h | 30 +++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/src/main/cpp/defaultconfigurator.cpp b/src/main/cpp/defaultconfigurator.cpp index 0fdd99e7..a8bdddb7 100644 --- a/src/main/cpp/defaultconfigurator.cpp +++ b/src/main/cpp/defaultconfigurator.cpp @@ -22,6 +22,8 @@ #include <log4cxx/helpers/loglog.h> #include <log4cxx/helpers/optionconverter.h> #include <log4cxx/helpers/stringhelper.h> +#include <log4cxx/xml/domconfigurator.h> +#include <log4cxx/propertyconfigurator.h> using namespace log4cxx; using namespace log4cxx::spi; @@ -150,6 +152,41 @@ int DefaultConfigurator::getConfigurationWatchDelay() return milliseconds; } +log4cxx::spi::ConfigurationStatus DefaultConfigurator::tryLoadFile(const LogString& filename){ + if(helpers::StringHelper::endsWith(filename, ".xml")){ + return log4cxx::xml::DOMConfigurator::configure(filename); + }else if(helpers::StringHelper::endsWith(filename, ".properties")){ + return log4cxx::PropertyConfigurator::configure(filename); + } + + return log4cxx::spi::ConfigurationStatus::NotConfigured; +} + +std::tuple<log4cxx::spi::ConfigurationStatus,LogString> +DefaultConfigurator::configureFromFile(const std::vector<LogString>& directories, const std::vector<LogString>& filenames){ + log4cxx::helpers::Pool pool; + + for( LogString dir : directories ){ + for( LogString fname : filenames ){ + LogString canidate_str = dir + "/" + fname; + File candidate(canidate_str); + + LogString debugMsg = LOG4CXX_STR("Checking file "); + debugMsg.append(canidate_str); + LogLog::debug(debugMsg); + if (candidate.exists(pool)) + { + log4cxx::spi::ConfigurationStatus configStatus = tryLoadFile(canidate_str); + if( configStatus == log4cxx::spi::ConfigurationStatus::Configured ){ + return {configStatus, canidate_str}; + } + LogLog::debug("Unable to load file: trying next"); + } + } + } + + return {log4cxx::spi::ConfigurationStatus::NotConfigured, LogString()}; +} diff --git a/src/main/include/log4cxx/defaultconfigurator.h b/src/main/include/log4cxx/defaultconfigurator.h index 59561274..caceac20 100644 --- a/src/main/include/log4cxx/defaultconfigurator.h +++ b/src/main/include/log4cxx/defaultconfigurator.h @@ -20,6 +20,7 @@ #include <log4cxx/spi/configurator.h> #include <log4cxx/spi/loggerrepository.h> +#include <tuple> namespace log4cxx { @@ -70,10 +71,39 @@ class LOG4CXX_EXPORT DefaultConfigurator */ static void setConfigurationWatchSeconds(int seconds); + /** + * Configure Log4cxx from a file. This method will attempt to load the configuration files in the + * directories given. + * + * For example, if we want a configuration file named 'myapp-logging.xml' with the default location + * for this file in /etc/myapp, but to have this overriden by a file in /usr/local/etc/myapp, we would + * call this function as follows: + * + * configureFromFile( { "/usr/local/etc/myapp", "/etc/myapp" }, { "myapp-logging.xml" ); + * + * This will then search for files in the following order: + * + * <pre> + * /usr/local/etc/myapp/myapp-logging.xml + * /etc/myapp/myapp-logging.xml + * </pre> + * + * The status of configuring Log4cxx as well as the eventual filename used is returned. If a file exists + * but it is not able to be used to configure Log4cxx, the next file in the list will be tried until + * a valid configuration file is found or the end of the list is reached. + * + * @param directories The directories to look in. + * @param filenamse The names of the files to look for + * @return The status of the configuration, and the filename loaded(if a file was found). + */ + static std::tuple<log4cxx::spi::ConfigurationStatus,LogString> configureFromFile(const std::vector<LogString>& directories, + const std::vector<LogString>& filenames); + private: static const LogString getConfigurationFileName(); static const LogString getConfiguratorClass(); static int getConfigurationWatchDelay(); + static log4cxx::spi::ConfigurationStatus tryLoadFile(const LogString& filename); }; // class DefaultConfigurator } // namespace log4cxx
