From 27546ececd0ee4e8712bc50ee6e97185ebf73da1 Mon Sep 17 00:00:00 2001 From: Stephen Webb Date: Tue, 12 Sep 2023 17:48:50 +1000 Subject: [PATCH 1/3] Revert to console output when configuration file is missing in documented configuration examples --- src/examples/cpp/com/foo/config-qt.cpp | 4 +++- src/examples/cpp/com/foo/config2.cpp | 4 +++- src/examples/cpp/com/foo/config3.cpp | 5 ++++- src/main/include/log4cxx/basicconfigurator.h | 9 +++++---- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/examples/cpp/com/foo/config-qt.cpp b/src/examples/cpp/com/foo/config-qt.cpp index 648890874..41d5eca12 100644 --- a/src/examples/cpp/com/foo/config-qt.cpp +++ b/src/examples/cpp/com/foo/config-qt.cpp @@ -15,6 +15,7 @@ * limitations under the License. */ #include "config-qt.h" +#include #include #include #include @@ -51,7 +52,8 @@ void ConfigureLogging() { #if defined(_DEBUG) log4cxx::helpers::LogLog::setInternalDebugging(true); #endif - log4cxx::qt::Configuration::configureFromFileAndWatch(paths, names); + if (log4cxx::qt::Configuration::configureFromFileAndWatch(paths, names) == log4cxx::spi::ConfigurationStatus::NotConfigured) + log4cxx::BasicConfigurator::configure(); // Send events to the console } // Retrieve the \c name logger pointer. diff --git a/src/examples/cpp/com/foo/config2.cpp b/src/examples/cpp/com/foo/config2.cpp index 2c25f8af0..8228e3743 100644 --- a/src/examples/cpp/com/foo/config2.cpp +++ b/src/examples/cpp/com/foo/config2.cpp @@ -1,4 +1,5 @@ #include "com/foo/config.h" +#include #include #include @@ -7,7 +8,8 @@ namespace com { namespace foo { auto getLogger(const std::string& name) -> LoggerPtr { static struct log4cxx_initializer { log4cxx_initializer() { - log4cxx::PropertyConfigurator::configure("MyApp.properties"); + if (log4cxx::PropertyConfigurator::configure("MyApp.properties") == log4cxx::spi::ConfigurationStatus::NotConfigured) + log4cxx::BasicConfigurator::configure(); // Send events to the console } ~log4cxx_initializer() { log4cxx::LogManager::shutdown(); diff --git a/src/examples/cpp/com/foo/config3.cpp b/src/examples/cpp/com/foo/config3.cpp index d38ae43c3..349740517 100644 --- a/src/examples/cpp/com/foo/config3.cpp +++ b/src/examples/cpp/com/foo/config3.cpp @@ -17,6 +17,7 @@ #include "config.h" #include #include +#include #include #include #include @@ -136,8 +137,10 @@ void SelectConfigurationFile() { } } if (extension[i]) // Found a configuration file? - break; + return; } + // Configuration file not found - send events to the console + BasicConfigurator::configure(); } } // namespace diff --git a/src/main/include/log4cxx/basicconfigurator.h b/src/main/include/log4cxx/basicconfigurator.h index 59bf032da..edecfadee 100644 --- a/src/main/include/log4cxx/basicconfigurator.h +++ b/src/main/include/log4cxx/basicconfigurator.h @@ -29,10 +29,11 @@ class Appender; typedef std::shared_ptr AppenderPtr; /** -Use this class to quickly configure the package. -

For file based configuration see -PropertyConfigurator. For XML based configuration see -DOMConfigurator. +Use BasicConfigurator (static) methods to configure Log4cxx +when not using a configuration file. + +For key=value format configuration see PropertyConfigurator. +For XML format configuration see xml::DOMConfigurator. */ class LOG4CXX_EXPORT BasicConfigurator { From 7d5aed1f3ad3ce564d9ba5de622a3e84f675e2f3 Mon Sep 17 00:00:00 2001 From: Stephen Webb Date: Thu, 14 Sep 2023 15:43:23 +1000 Subject: [PATCH 2/3] Fix Qt example --- src/examples/cpp/com/foo/config-qt.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/examples/cpp/com/foo/config-qt.cpp b/src/examples/cpp/com/foo/config-qt.cpp index 41d5eca12..d4dda03e5 100644 --- a/src/examples/cpp/com/foo/config-qt.cpp +++ b/src/examples/cpp/com/foo/config-qt.cpp @@ -52,7 +52,10 @@ void ConfigureLogging() { #if defined(_DEBUG) log4cxx::helpers::LogLog::setInternalDebugging(true); #endif - if (log4cxx::qt::Configuration::configureFromFileAndWatch(paths, names) == log4cxx::spi::ConfigurationStatus::NotConfigured) + log4cxx::spi::ConfigurationStatus status = {}; + QString selectedPath; + std::tie(status, selectedPath) = log4cxx::qt::Configuration::configureFromFileAndWatch(paths, names); + if (status == log4cxx::spi::ConfigurationStatus::NotConfigured) log4cxx::BasicConfigurator::configure(); // Send events to the console } From 635bb543a2632561d9d5011f78bf4104175a66c2 Mon Sep 17 00:00:00 2001 From: Stephen Webb Date: Thu, 14 Sep 2023 15:59:19 +1000 Subject: [PATCH 3/3] Improve Qt example --- src/examples/cpp/com/foo/config-qt.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/examples/cpp/com/foo/config-qt.cpp b/src/examples/cpp/com/foo/config-qt.cpp index d4dda03e5..68c5fdde8 100644 --- a/src/examples/cpp/com/foo/config-qt.cpp +++ b/src/examples/cpp/com/foo/config-qt.cpp @@ -52,8 +52,8 @@ void ConfigureLogging() { #if defined(_DEBUG) log4cxx::helpers::LogLog::setInternalDebugging(true); #endif - log4cxx::spi::ConfigurationStatus status = {}; - QString selectedPath; + auto status = log4cxx::spi::ConfigurationStatus::NotConfigured; + auto selectedPath = QString(); std::tie(status, selectedPath) = log4cxx::qt::Configuration::configureFromFileAndWatch(paths, names); if (status == log4cxx::spi::ConfigurationStatus::NotConfigured) log4cxx::BasicConfigurator::configure(); // Send events to the console