diff --git a/src/main/cpp/asyncappender.cpp b/src/main/cpp/asyncappender.cpp index 95f5ece28..5f4b3d2de 100644 --- a/src/main/cpp/asyncappender.cpp +++ b/src/main/cpp/asyncappender.cpp @@ -135,10 +135,22 @@ struct AsyncAppender::AsyncAppenderPriv : public AppenderSkeleton::AppenderSkele #if LOG4CXX_EVENTS_AT_EXIT void atExitActivated() { - std::unique_lock lock(bufferMutex); - bufferNotFull.wait(lock, [this]() -> bool - { return buffer.empty() || closed; } - ); + { + std::lock_guard lock(bufferMutex); + closed = true; + } + bufferNotEmpty.notify_all(); + bufferNotFull.notify_all(); + + if (dispatcher.joinable()) + { + dispatcher.join(); + } + + for (auto item : appenders.getAllAppenders()) + { + item->close(); + } } #endif diff --git a/src/main/cpp/filewatchdog.cpp b/src/main/cpp/filewatchdog.cpp index 742423b53..cfb4f6a15 100644 --- a/src/main/cpp/filewatchdog.cpp +++ b/src/main/cpp/filewatchdog.cpp @@ -25,6 +25,10 @@ #include #include +#if LOG4CXX_EVENTS_AT_EXIT +#include +#endif + using namespace LOG4CXX_NS; using namespace LOG4CXX_NS::helpers; @@ -33,8 +37,11 @@ long FileWatchdog::DEFAULT_DELAY = 60000; struct FileWatchdog::FileWatchdogPrivate{ FileWatchdogPrivate(const File& file1) : file(file1), delay(DEFAULT_DELAY), lastModif(0), - warnedAlready(false), interrupted(0), thread(){} - + warnedAlready(false), interrupted(0), thread() +#if LOG4CXX_EVENTS_AT_EXIT + , atExitRegistryRaii([this]{atExitActivated();}) +#endif + {} /** The name of the file to observe for changes. */ @@ -51,6 +58,23 @@ struct FileWatchdog::FileWatchdogPrivate{ std::thread thread; std::condition_variable interrupt; std::mutex interrupt_mutex; + +#if LOG4CXX_EVENTS_AT_EXIT + helpers::AtExitRegistry::Raii atExitRegistryRaii; +#endif + +#if LOG4CXX_EVENTS_AT_EXIT + void atExitActivated() + { + { + std::lock_guard lock(interrupt_mutex); + interrupted = 0xFFFF; + } + interrupt.notify_all(); + if (thread.joinable()) + thread.join(); + } +#endif }; FileWatchdog::FileWatchdog(const File& file1) diff --git a/src/test/cpp/terminationtestcase.cpp b/src/test/cpp/terminationtestcase.cpp index eca9e2f04..4c62bb923 100644 --- a/src/test/cpp/terminationtestcase.cpp +++ b/src/test/cpp/terminationtestcase.cpp @@ -51,7 +51,9 @@ LOGUNIT_CLASS(TerminationTestCase) static struct initializer { initializer() { setDefaultAppender(); } +#if !LOG4CXX_EVENTS_AT_EXIT ~initializer() { LogManager::shutdown(); } +#endif } x; auto r = LogManager::getLoggerRepository(); return name.empty() ? r->getLogger(name) : r->getRootLogger();