Skip to content

Commit

Permalink
MSYS2 kills threads before onexit functions so LogManager::shutdown i…
Browse files Browse the repository at this point in the history
…s required
  • Loading branch information
stephen-webb committed Dec 21, 2024
1 parent a2f7046 commit 46d6e02
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/main/cpp/logmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <log4cxx/helpers/exception.h>
#include <log4cxx/helpers/optionconverter.h>
#include <log4cxx/helpers/loglog.h>
#include <log4cxx/helpers/threadutility.h>

#include <log4cxx/spi/loggingevent.h>
#include <log4cxx/file.h>
Expand Down Expand Up @@ -203,6 +204,7 @@ LoggerList LogManager::getCurrentLoggers()
void LogManager::shutdown()
{
APRInitializer::unregisterAll();
ThreadUtility::instance()->removeAllPeriodicTasks();
getLoggerRepository()->shutdown();
}

Expand Down
45 changes: 22 additions & 23 deletions src/test/cpp/fileappendertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@
using namespace log4cxx;
using namespace log4cxx::helpers;

auto getLogger(const std::string& name = std::string()) -> LoggerPtr {
static struct log4cxx_initializer {
log4cxx_initializer() {
auto layout = std::make_shared<PatternLayout>(LOG4CXX_STR("%d %m%n"));
auto writer = std::make_shared<FileAppender>(layout, LOG4CXX_STR("output/newdir/temp.log"), false);
writer->setName(LOG4CXX_STR("FileAppender"));
writer->setBufferedIO(true);
writer->setBufferedSeconds(1);
helpers::Pool p;
writer->activateOptions(p);
LogManager::getRootLogger()->addAppender(writer);
}
~log4cxx_initializer() {
LogManager::shutdown();
}
} initAndShutdown;
return name.empty()
? LogManager::getRootLogger()
: LogManager::getLogger(name);
}


/**
*
Expand Down Expand Up @@ -85,31 +106,9 @@ LOGUNIT_CLASS(FileAppenderTest)
LOGUNIT_ASSERT(appender->isAsSevereAsThreshold(debug));
}

LoggerPtr getBufferedFileLogger()
{
LogString name = LOG4CXX_STR("test.fileappender");
auto r = LogManager::getLoggerRepository();
LoggerPtr result;
if (!(result = r->exists(name)))
{
result = r->getLogger(name);
result->setAdditivity(false);
result->setLevel(Level::getInfo());
auto layout = std::make_shared<PatternLayout>(LOG4CXX_STR("%d %m%n"));
auto writer = std::make_shared<FileAppender>(layout, LOG4CXX_STR("output/newdir/temp.log"), false);
writer->setName(LOG4CXX_STR("FileAppender"));
writer->setBufferedIO(true);
writer->setBufferedSeconds(1);
helpers::Pool p;
writer->activateOptions(p);
result->addAppender(writer);
}
return result;
}

void testBufferedOutput()
{
auto logger = getBufferedFileLogger();
auto logger = getLogger();
int requiredMsgCount = 10000;
for ( int x = 0; x < requiredMsgCount; x++ )
{
Expand Down

0 comments on commit 46d6e02

Please sign in to comment.