From b879b2bf70c80d40e2ea64ce69b3ae960571a054 Mon Sep 17 00:00:00 2001 From: Stephen Webb Date: Thu, 16 May 2024 12:22:37 +1000 Subject: [PATCH] Warn about each instance of a mis-configured appender (#385) --- src/main/cpp/appenderskeleton.cpp | 31 ++++++++++++++ src/main/cpp/writerappender.cpp | 42 ++++--------------- .../log4cxx/private/appenderskeleton_priv.h | 6 +++ .../log4cxx/private/writerappender_priv.h | 3 ++ 4 files changed, 49 insertions(+), 33 deletions(-) diff --git a/src/main/cpp/appenderskeleton.cpp b/src/main/cpp/appenderskeleton.cpp index 878a89dc2..3a5e74958 100644 --- a/src/main/cpp/appenderskeleton.cpp +++ b/src/main/cpp/appenderskeleton.cpp @@ -131,6 +131,37 @@ void AppenderSkeleton::doAppendImpl(const spi::LoggingEventPtr& event, Pool& poo append(event, pool1); } +bool AppenderSkeleton::AppenderSkeletonPrivate::checkNotClosed() +{ + if (this->closed) + { + if (!this->warnedClosed) + { + LogLog::warn(LOG4CXX_STR("Not allowed to write to a closed appender.")); + this->warnedClosed = true; + } + return false; + } + return true; +} + +bool AppenderSkeleton::AppenderSkeletonPrivate::checkLayout() +{ + if (!this->layout) + { + if (!this->warnedNoLayout) + { + this->errorHandler->error + ( LogString(LOG4CXX_STR("No layout set for the appender named [")) + + this->name + LOG4CXX_STR("].") + ); + this->warnedNoLayout = true; + } + return false; + } + return true; +} + void AppenderSkeleton::setErrorHandler(const spi::ErrorHandlerPtr errorHandler1) { std::lock_guard lock(m_priv->mutex); diff --git a/src/main/cpp/writerappender.cpp b/src/main/cpp/writerappender.cpp index 5751f2556..b1c5a6899 100644 --- a/src/main/cpp/writerappender.cpp +++ b/src/main/cpp/writerappender.cpp @@ -107,46 +107,22 @@ void WriterAppender::append(const spi::LoggingEventPtr& event, Pool& pool1) value false is returned. */ bool WriterAppender::checkEntryConditions() const { - static bool warnedClosed = false; - static bool warnedNoWriter = false; - static bool warnedNoLayout = false; - - if (_priv->closed) - { - if (!warnedClosed) - { - LogLog::warn(LOG4CXX_STR("Not allowed to write to a closed appender.")); - warnedClosed = true; - } - - return false; - } + return _priv->checkWriter() && _priv->checkLayout() && _priv->checkNotClosed(); +} - if (_priv->writer == 0) +bool WriterAppender::WriterAppenderPriv::checkWriter() +{ + if (this->writer == 0) { - if (!warnedNoWriter) + if (!this->warnedNoWriter) { - _priv->errorHandler->error( + this->errorHandler->error( LogString(LOG4CXX_STR("No output stream or file set for the appender named [")) + - _priv->name + LOG4CXX_STR("].")); - warnedNoWriter = true; - } - - return false; - } - - if (_priv->layout == 0) - { - if (!warnedNoLayout) - { - _priv->errorHandler->error( - LogString(LOG4CXX_STR("No layout set for the appender named [")) + - _priv->name + LOG4CXX_STR("].")); - warnedNoLayout = true; + this->name + LOG4CXX_STR("].")); + this->warnedNoWriter = true; } return false; } - return true; } diff --git a/src/main/include/log4cxx/private/appenderskeleton_priv.h b/src/main/include/log4cxx/private/appenderskeleton_priv.h index b9660cf3d..cba7bec45 100644 --- a/src/main/include/log4cxx/private/appenderskeleton_priv.h +++ b/src/main/include/log4cxx/private/appenderskeleton_priv.h @@ -70,6 +70,12 @@ struct AppenderSkeleton::AppenderSkeletonPrivate LOG4CXX_NS::helpers::Pool pool; mutable std::recursive_mutex mutex; + + bool warnedClosed = false; + bool checkNotClosed(); + + bool warnedNoLayout = false; + bool checkLayout(); }; } diff --git a/src/main/include/log4cxx/private/writerappender_priv.h b/src/main/include/log4cxx/private/writerappender_priv.h index 752c1c3c5..30f52eea4 100644 --- a/src/main/include/log4cxx/private/writerappender_priv.h +++ b/src/main/include/log4cxx/private/writerappender_priv.h @@ -101,6 +101,9 @@ struct WriterAppender::WriterAppenderPriv : public AppenderSkeleton::AppenderSke #if LOG4CXX_EVENTS_AT_EXIT helpers::AtExitRegistry::Raii atExitRegistryRaii; #endif + + bool warnedNoWriter = false; + bool checkWriter(); }; }