Skip to content

Commit

Permalink
Reduce logging overhead by reserving storage in the output string (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
swebb2066 authored Aug 4, 2023
1 parent cc7235a commit 610018a
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 12 deletions.
15 changes: 11 additions & 4 deletions src/main/cpp/fmtlayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ using namespace log4cxx;
using namespace log4cxx::spi;

struct FMTLayout::FMTLayoutPrivate{
FMTLayoutPrivate(){}
FMTLayoutPrivate()
: expectedPatternLength(100)
{}

FMTLayoutPrivate(const LogString& pattern) :
conversionPattern(pattern)
FMTLayoutPrivate(const LogString& pattern)
: conversionPattern(pattern)
, expectedPatternLength(100)
{}

LogString conversionPattern;

// Expected length of a formatted event excluding the message text
size_t expectedPatternLength;
};

IMPLEMENT_LOG4CXX_OBJECT(FMTLayout)
Expand Down Expand Up @@ -76,13 +82,14 @@ void FMTLayout::setOption(const LogString& option, const LogString& value)

void FMTLayout::activateOptions(helpers::Pool&)
{

m_priv->expectedPatternLength = getFormattedEventCharacterCount() * 2;
}

void FMTLayout::format(LogString& output,
const spi::LoggingEventPtr& event,
log4cxx::helpers::Pool&) const
{
output.reserve(m_priv->expectedPatternLength + event->getMessage().size());
auto locationFull = fmt::format("{}({})",
event->getLocationInformation().getFileName(),
event->getLocationInformation().getLineNumber());
Expand Down
14 changes: 12 additions & 2 deletions src/main/cpp/htmllayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,22 @@ using namespace log4cxx::spi;

struct HTMLLayout::HTMLLayoutPrivate
{
HTMLLayoutPrivate() : locationInfo(false), title(LOG4CXX_STR("Log4cxx Log Messages")),
dateFormat() {}
HTMLLayoutPrivate()
: locationInfo(false)
, title(LOG4CXX_STR("Log4cxx Log Messages"))
, dateFormat()
, expectedPatternLength(100)
{}

// Print no location info by default
bool locationInfo; //= false

LogString title;

helpers::ISO8601DateFormat dateFormat;

// Expected length of a formatted event excluding the message text
size_t expectedPatternLength;
};

IMPLEMENT_LOG4CXX_OBJECT(HTMLLayout)
Expand All @@ -53,6 +60,7 @@ HTMLLayout::HTMLLayout()
: m_priv(std::make_unique<HTMLLayoutPrivate>())
{
m_priv->dateFormat.setTimeZone(TimeZone::getGMT());
m_priv->expectedPatternLength = getFormattedEventCharacterCount() * 2;
}

HTMLLayout::~HTMLLayout() {}
Expand All @@ -71,13 +79,15 @@ void HTMLLayout::setOption(const LogString& option,
LOG4CXX_STR("LOCATIONINFO"), LOG4CXX_STR("locationinfo")))
{
setLocationInfo(OptionConverter::toBoolean(value, false));
m_priv->expectedPatternLength = getFormattedEventCharacterCount() * 2;
}
}

void HTMLLayout::format(LogString& output,
const spi::LoggingEventPtr& event,
Pool& p) const
{
output.reserve(m_priv->expectedPatternLength + event->getMessage().size());
output.append(LOG4CXX_EOL);
output.append(LOG4CXX_STR("<tr>"));
output.append(LOG4CXX_EOL);
Expand Down
9 changes: 7 additions & 2 deletions src/main/cpp/jsonlayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ struct JSONLayout::JSONLayoutPrivate
prettyPrint(false),
dateFormat(),
ppIndentL1(LOG4CXX_STR(" ")),
ppIndentL2(LOG4CXX_STR(" ")) {}
ppIndentL2(LOG4CXX_STR(" ")),
expectedPatternLength(100) {}

// Print no location info by default
bool locationInfo; //= false
Expand All @@ -49,6 +50,9 @@ struct JSONLayout::JSONLayoutPrivate

LogString ppIndentL1;
LogString ppIndentL2;

// Expected length of a formatted event excluding the message text
size_t expectedPatternLength;
};

JSONLayout::JSONLayout() :
Expand Down Expand Up @@ -85,7 +89,7 @@ LogString JSONLayout::getContentType() const

void JSONLayout::activateOptions(helpers::Pool& /* p */)
{

m_priv->expectedPatternLength = getFormattedEventCharacterCount() * 2;
}

void JSONLayout::setOption(const LogString& option, const LogString& value)
Expand All @@ -106,6 +110,7 @@ void JSONLayout::format(LogString& output,
const spi::LoggingEventPtr& event,
Pool& p) const
{
output.reserve(m_priv->expectedPatternLength + event->getMessage().size());
output.append(LOG4CXX_STR("{"));
output.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));

Expand Down
17 changes: 17 additions & 0 deletions src/main/cpp/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,20 @@ LogString Layout::getContentType() const
void Layout::appendHeader(LogString&, log4cxx::helpers::Pool&) {}

void Layout::appendFooter(LogString&, log4cxx::helpers::Pool&) {}

/**
* The expected length of a formatted event excluding the message text
*/
size_t Layout::getFormattedEventCharacterCount() const
{
auto exampleEvent = std::make_shared<spi::LoggingEvent>
( LOG4CXX_STR("example.logger")
, Level::getDebug()
, LOG4CXX_LOCATION
, LogString()
);
LogString text;
Pool pool;
format(text, exampleEvent, pool);
return text.size();
}
15 changes: 12 additions & 3 deletions src/main/cpp/patternlayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ using namespace log4cxx::pattern;

struct PatternLayout::PatternLayoutPrivate
{
PatternLayoutPrivate() {}
PatternLayoutPrivate(const LogString& pattern) :
conversionPattern(pattern) {}
PatternLayoutPrivate()
: expectedPatternLength(100)
{}
PatternLayoutPrivate(const LogString& pattern)
: conversionPattern(pattern)
, expectedPatternLength(100)
{}

/**
* Conversion pattern.
Expand All @@ -82,6 +86,9 @@ struct PatternLayout::PatternLayoutPrivate
LogString m_infoColor = LOG4CXX_STR("\\x1B[32m"); //green
LogString m_debugColor = LOG4CXX_STR("\\x1B[36m"); //cyan;
LogString m_traceColor = LOG4CXX_STR("\\x1B[34m"); //blue;

// Expected length of a formatted event excluding the message text
size_t expectedPatternLength;
};

IMPLEMENT_LOG4CXX_OBJECT(PatternLayout)
Expand Down Expand Up @@ -115,6 +122,7 @@ void PatternLayout::format(LogString& output,
const spi::LoggingEventPtr& event,
Pool& pool) const
{
output.reserve(m_priv->expectedPatternLength + event->getMessage().size());
std::vector<FormattingInfoPtr>::const_iterator formatterIter =
m_priv->patternFields.begin();

Expand Down Expand Up @@ -199,6 +207,7 @@ void PatternLayout::activateOptions(Pool&)
m_priv->patternConverters.push_back(eventConverter);
}
}
m_priv->expectedPatternLength = getFormattedEventCharacterCount() * 2;
}

#define RULES_PUT(spec, cls) \
Expand Down
13 changes: 12 additions & 1 deletion src/main/cpp/xmllayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,26 @@ using namespace log4cxx::xml;

struct XMLLayout::XMLLayoutPrivate
{
XMLLayoutPrivate() : locationInfo(false), properties(false) {}
XMLLayoutPrivate()
: locationInfo(false)
, properties(false)
, expectedPatternLength(100)
{}

// Print no location info by default
bool locationInfo; //= false
bool properties; // = false

// Expected length of a formatted event excluding the message text
size_t expectedPatternLength;
};

IMPLEMENT_LOG4CXX_OBJECT(XMLLayout)

XMLLayout::XMLLayout()
: m_priv(std::make_unique<XMLLayoutPrivate>())
{
m_priv->expectedPatternLength = getFormattedEventCharacterCount() * 2;
}

XMLLayout::~XMLLayout() {}
Expand All @@ -56,18 +64,21 @@ void XMLLayout::setOption(const LogString& option,
if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("LOCATIONINFO"), LOG4CXX_STR("locationinfo")))
{
setLocationInfo(OptionConverter::toBoolean(value, false));
m_priv->expectedPatternLength = getFormattedEventCharacterCount() * 2;
}

if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("PROPERTIES"), LOG4CXX_STR("properties")))
{
setProperties(OptionConverter::toBoolean(value, false));
m_priv->expectedPatternLength = getFormattedEventCharacterCount() * 2;
}
}

void XMLLayout::format(LogString& output,
const spi::LoggingEventPtr& event,
Pool& p) const
{
output.reserve(m_priv->expectedPatternLength + event->getMessage().size());
output.append(LOG4CXX_STR("<log4j:event logger=\""));
Transform::appendEscapingTags(output, event->getLoggerName());
output.append(LOG4CXX_STR("\" timestamp=\""));
Expand Down
6 changes: 6 additions & 0 deletions src/main/include/log4cxx/layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ class LOG4CXX_EXPORT Layout :
The other layouts return <code>false</code>.
*/
virtual bool ignoresThrowable() const = 0;

protected:
/**
* The expected length of a formatted event excluding the message text
*/
size_t getFormattedEventCharacterCount() const;
};
LOG4CXX_PTR_DEF(Layout);
}
Expand Down

0 comments on commit 610018a

Please sign in to comment.