This is an automated email from the ASF dual-hosted git repository. swebb2066 pushed a commit to branch improve_error_handler in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git
commit 294da14db2c6c79dfd284f1135ab88f123e1315c Author: Stephen Webb <[email protected]> AuthorDate: Thu Jan 11 12:08:03 2024 +1100 Make the FallbackErrorHandler more usable in unit tests --- src/main/cpp/fallbackerrorhandler.cpp | 6 ++++++ src/main/include/log4cxx/helpers/onlyonceerrorhandler.h | 4 ++++ src/main/include/log4cxx/spi/errorhandler.h | 7 +++++++ src/main/include/log4cxx/varia/fallbackerrorhandler.h | 9 +++++++++ src/test/cpp/asyncappendertestcase.cpp | 3 ++- src/test/cpp/varia/errorhandlertestcase.cpp | 2 ++ 6 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/cpp/fallbackerrorhandler.cpp b/src/main/cpp/fallbackerrorhandler.cpp index a544fd83..39c2ac9d 100644 --- a/src/main/cpp/fallbackerrorhandler.cpp +++ b/src/main/cpp/fallbackerrorhandler.cpp @@ -37,6 +37,7 @@ struct FallbackErrorHandler::FallbackErrorHandlerPrivate AppenderWeakPtr backup; AppenderWeakPtr primary; std::vector<LoggerPtr> loggers; + bool errorReported = false; }; FallbackErrorHandler::FallbackErrorHandler() @@ -91,6 +92,7 @@ void FallbackErrorHandler::error(const LogString& message, + l->getName()); l->addAppender(backupLocked); } + m_priv->errorReported = true; } void FallbackErrorHandler::setAppender(const AppenderPtr& primary1) @@ -124,3 +126,7 @@ void FallbackErrorHandler::setOption(const LogString&, const LogString&) { } +bool FallbackErrorHandler::errorReported() const +{ + return m_priv->errorReported; +} diff --git a/src/main/include/log4cxx/helpers/onlyonceerrorhandler.h b/src/main/include/log4cxx/helpers/onlyonceerrorhandler.h index 20824b21..52ac5108 100644 --- a/src/main/include/log4cxx/helpers/onlyonceerrorhandler.h +++ b/src/main/include/log4cxx/helpers/onlyonceerrorhandler.h @@ -112,7 +112,11 @@ class LOG4CXX_EXPORT OnlyOnceErrorHandler : /** Has an error been reported? */ +#if 15 < LOG4CXX_ABI_VERSION + bool errorReported() const override; +#else bool errorReported() const; +#endif }; } // namespace helpers } // namespace log4cxx diff --git a/src/main/include/log4cxx/spi/errorhandler.h b/src/main/include/log4cxx/spi/errorhandler.h index fa1ab076..89683123 100644 --- a/src/main/include/log4cxx/spi/errorhandler.h +++ b/src/main/include/log4cxx/spi/errorhandler.h @@ -113,6 +113,13 @@ class LOG4CXX_EXPORT ErrorHandler : public virtual OptionHandler Set the appender to fallback upon in case of failure. */ virtual void setBackupAppender(const AppenderPtr& appender) = 0; + +#if 15 < LOG4CXX_ABI_VERSION + /** + Has an error been reported? + */ + virtual bool errorReported() const = 0; +#endif }; LOG4CXX_PTR_DEF(ErrorHandler); diff --git a/src/main/include/log4cxx/varia/fallbackerrorhandler.h b/src/main/include/log4cxx/varia/fallbackerrorhandler.h index 2d5d29ae..a2ac5b6b 100644 --- a/src/main/include/log4cxx/varia/fallbackerrorhandler.h +++ b/src/main/include/log4cxx/varia/fallbackerrorhandler.h @@ -102,6 +102,15 @@ class LOG4CXX_EXPORT FallbackErrorHandler : Set the backup appender. */ void setBackupAppender(const AppenderPtr& backup) override; + + /** + Has an error been reported? + */ +#if 15 < LOG4CXX_ABI_VERSION + bool errorReported() const override; +#else + bool errorReported() const; +#endif }; LOG4CXX_PTR_DEF(FallbackErrorHandler); diff --git a/src/test/cpp/asyncappendertestcase.cpp b/src/test/cpp/asyncappendertestcase.cpp index 22c32860..6b7e2d24 100644 --- a/src/test/cpp/asyncappendertestcase.cpp +++ b/src/test/cpp/asyncappendertestcase.cpp @@ -247,7 +247,8 @@ class AsyncAppenderTestCase : public AppenderSkeletonTestCase LOG4CXX_INFO(root, "Message"); std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) ); - LOG4CXX_INFO(root, "Message"); + LOGUNIT_ASSERT(errorHandler->errorReported()); + LOG4CXX_INFO(root, "Message"); auto& v = vectorAppender->getVector(); LOGUNIT_ASSERT(0 < v.size()); } diff --git a/src/test/cpp/varia/errorhandlertestcase.cpp b/src/test/cpp/varia/errorhandlertestcase.cpp index 4f4007e1..c8cf517f 100644 --- a/src/test/cpp/varia/errorhandlertestcase.cpp +++ b/src/test/cpp/varia/errorhandlertestcase.cpp @@ -79,6 +79,7 @@ public: LOGUNIT_ASSERT(eh != 0); common(); + LOGUNIT_ASSERT(eh->errorReported()); std::string TEST1_PAT = "FALLBACK - (root|test) - Message {0-9}"; @@ -117,6 +118,7 @@ public: LOGUNIT_ASSERT(eh != 0); eh->setLogger(logger); common(); + LOGUNIT_ASSERT(eh->errorReported()); std::string TEST1_PAT = "FALLBACK - (root|test) - Message {0-9}";
