Skip to content

Commit

Permalink
Join background threads when LOG4CXX_EVENTS_AT_EXIT=on
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-webb committed May 6, 2024
1 parent 78eee3f commit 7f0aeea
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
20 changes: 16 additions & 4 deletions src/main/cpp/asyncappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,22 @@ struct AsyncAppender::AsyncAppenderPriv : public AppenderSkeleton::AppenderSkele
#if LOG4CXX_EVENTS_AT_EXIT
void atExitActivated()
{
std::unique_lock<std::mutex> lock(bufferMutex);
bufferNotFull.wait(lock, [this]() -> bool
{ return buffer.empty() || closed; }
);
{
std::lock_guard<std::mutex> lock(bufferMutex);
closed = true;
}
bufferNotEmpty.notify_all();
bufferNotFull.notify_all();

if (dispatcher.joinable())
{
dispatcher.join();
}

for (auto item : appenders.getAllAppenders())
{
item->close();
}
}
#endif

Expand Down
28 changes: 26 additions & 2 deletions src/main/cpp/filewatchdog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include <functional>
#include <chrono>

#if LOG4CXX_EVENTS_AT_EXIT
#include <log4cxx/private/atexitregistry.h>
#endif

using namespace LOG4CXX_NS;
using namespace LOG4CXX_NS::helpers;

Expand All @@ -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.
*/
Expand All @@ -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<std::mutex> lock(interrupt_mutex);
interrupted = 0xFFFF;
}
interrupt.notify_all();
if (thread.joinable())
thread.join();
}
#endif
};

FileWatchdog::FileWatchdog(const File& file1)
Expand Down
2 changes: 2 additions & 0 deletions src/test/cpp/terminationtestcase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 7f0aeea

Please sign in to comment.