Skip to content

Commit

Permalink
Reduce the number of apr_stat calls
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-webb committed Dec 15, 2024
1 parent 2de970c commit 5956a8b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 86 deletions.
92 changes: 10 additions & 82 deletions src/main/cpp/multiprocessrollingfileappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,74 +37,6 @@
#include <mutex>
#include <thread>

namespace LOG4CXX_NS
{

using namespace helpers;

namespace rolling
{
/**
* Wrapper for OutputStream that will report all log file
* size changes back to the appender for file length calculations.
*/
class MultiprocessOutputStream : public OutputStream
{
/**
* Wrapped output stream.
*/
private:
OutputStreamPtr os;

/**
* Rolling file appender to inform of stream writes.
*/
MultiprocessRollingFileAppender* rfa;

public:
/**
* Constructor.
* @param os output stream to wrap.
* @param rfa rolling file appender to inform.
*/
MultiprocessOutputStream(const OutputStreamPtr& os1, MultiprocessRollingFileAppender* rfa1)
: os(os1), rfa(rfa1)
{
}

/**
* {@inheritDoc}
*/
void close(Pool& p) override
{
os->close(p);
rfa = 0;
}

/**
* {@inheritDoc}
*/
void flush(Pool& p) override
{
os->flush(p);
}

/**
* {@inheritDoc}
*/
void write(ByteBuffer& buf, Pool& p) override
{
os->write(buf, p);

if (rfa != 0)
{
rfa->setFileLength(File().setPath(rfa->getFile()).length(p));
}
}
};
} // namespace rolling
} // namespace LOG4CXX_NS

using namespace LOG4CXX_NS;
using namespace LOG4CXX_NS::rolling;
using namespace LOG4CXX_NS::helpers;
Expand Down Expand Up @@ -289,19 +221,18 @@ bool MultiprocessRollingFileAppender::synchronizedRollover(Pool& p, const Trigge
{
bool result = false;
LogString fileName = getFile();
size_t fileLength = 0;
if (!_priv->rollingPolicy)
; // can't roll without a policy
else if (isAlreadyRolled(fileName, &fileLength))
reopenFile(fileName, fileLength);
else if (isAlreadyRolled(fileName, &_priv->fileLength))
reopenFile(fileName);
else
{
MultiprocessRollingFileAppenderPriv::Lock lk(_priv, fileName);
if (!lk.hasLock())
LogLog::warn(LOG4CXX_STR("Failed to lock ") + fileName);
else if (isAlreadyRolled(fileName, &fileLength))
reopenFile(fileName, fileLength);
else if (trigger && !trigger->isTriggeringEvent(this, _priv->_event, fileName, fileLength))
else if (isAlreadyRolled(fileName, &_priv->fileLength))
reopenFile(fileName);
else if (trigger && !trigger->isTriggeringEvent(this, _priv->_event, fileName, _priv->fileLength))
;
else if (auto rollover1 = _priv->rollingPolicy->rollover(fileName, getAppend(), p))
{
Expand Down Expand Up @@ -402,14 +333,13 @@ bool MultiprocessRollingFileAppender::synchronizedRollover(Pool& p, const Trigge
/**
* re-open \c fileName (used after it has been renamed)
*/
void MultiprocessRollingFileAppender::reopenFile(const LogString& fileName, size_t fileLength)
void MultiprocessRollingFileAppender::reopenFile(const LogString& fileName)
{
closeWriter();
OutputStreamPtr os = std::make_shared<FileOutputStream>(fileName, true);
WriterPtr newWriter(createWriter(os));
setFile(fileName);
setWriter(newWriter);
_priv->fileLength = fileLength;
}

/**
Expand All @@ -419,9 +349,8 @@ void MultiprocessRollingFileAppender::subAppend(const LoggingEventPtr& event, Po
{
// The rollover check must precede actual writing. This is the
// only correct behavior for time driven triggers.
size_t fileLength = getFileLength();
LogString fileName = getFile();
if (_priv->triggeringPolicy->isTriggeringEvent(this, event, fileName, fileLength))
if (_priv->triggeringPolicy->isTriggeringEvent(this, event, fileName, _priv->fileLength))
{
//
// wrap rollover request in try block since
Expand All @@ -441,9 +370,9 @@ void MultiprocessRollingFileAppender::subAppend(const LoggingEventPtr& event, Po
_priv->errorHandler->error(msg, ex, 0);
}
}
else if (isAlreadyRolled(fileName, &fileLength))
else if (isAlreadyRolled(fileName, &_priv->fileLength))
{
reopenFile(fileName, fileLength);
reopenFile(fileName);
}

FileAppender::subAppend(event, p);
Expand All @@ -469,8 +398,7 @@ WriterPtr MultiprocessRollingFileAppender::createWriter(OutputStreamPtr& os)
msg += LOG4CXX_STR(" - Rollover synchronization will be degraded.");
_priv->errorHandler->error(msg);
}
OutputStreamPtr cos = std::make_shared<MultiprocessOutputStream>(os, this);
return FileAppender::createWriter(cos);
return FileAppender::createWriter(os);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ class LOG4CXX_EXPORT MultiprocessRollingFileAppender : public RollingFileAppende
/**
* re-open \c fileName (used after it has been renamed)
*/
void reopenFile(const LogString& fileName, size_t fileLength);

friend class MultiprocessOutputStream;

void reopenFile(const LogString& fileName);
};

LOG4CXX_PTR_DEF(MultiprocessRollingFileAppender);
Expand Down

0 comments on commit 5956a8b

Please sign in to comment.