PatternFormatter specific-length source and optimizations.

Optimize the pattern formatter by doing time-zone calculations
once and using it's offset. Parse the format string once and
setup a vector of the fields wanted so parsing the %[name] is
much faster.

Add ability to easily enforce a specific length for the source
fields which is nicer for reading log files.
This commit is contained in:
Patrick White 2012-11-28 15:09:10 -05:00
parent 2cc47b5c02
commit b4aa3cedfa
2 changed files with 131 additions and 53 deletions

View File

@ -44,6 +44,7 @@
#include "Poco/Formatter.h"
#include "Poco/Message.h"
#include <vector>
namespace Poco {
@ -91,6 +92,7 @@ class Foundation_API PatternFormatter: public Formatter
/// * %z - time zone differential in ISO 8601 format (Z or +NN.NN)
/// * %Z - time zone differential in RFC format (GMT or +NNNN)
/// * %E - epoch time (UTC, seconds since midnight, January 1, 1970)
/// * %v[width] - the message source (%s) but text length is padded/cropped to 'width'
/// * %[name] - the value of the message parameter with the given name
/// * %% - percent sign
@ -138,8 +140,27 @@ protected:
/// Returns a string for the given priority value.
private:
bool _localTime;
std::string _pattern;
struct PatternAction
{
PatternAction(): key(0), length(0) {}
char key;
int length;
std::string property;
std::string prepend;
};
std::vector<PatternAction> _patternActions;
bool _localTime;
Timestamp::TimeDiff _localTimeOffset;
std::string _pattern;
void ParsePattern();
/// Will parse the _pattern string into the vector of PatternActions,
/// which contains the message key, any text that needs to be written first
/// a proprety in case of %[] and required length.
};

View File

@ -43,6 +43,7 @@
#include "Poco/Timestamp.h"
#include "Poco/Timezone.h"
#include "Poco/Environment.h"
#include "Poco/NumberParser.h"
namespace Poco {
@ -53,15 +54,18 @@ const std::string PatternFormatter::PROP_TIMES = "times";
PatternFormatter::PatternFormatter():
_localTime(false)
_localTime(false),
_localTimeOffset(0)
{
}
PatternFormatter::PatternFormatter(const std::string& format):
_localTime(false),
_localTimeOffset(0),
_pattern(format)
{
ParsePattern();
}
@ -75,86 +79,139 @@ void PatternFormatter::format(const Message& msg, std::string& text)
Timestamp timestamp = msg.getTime();
if (_localTime)
{
timestamp += Timezone::utcOffset()*Timestamp::resolution();
timestamp += Timezone::dst()*Timestamp::resolution();
timestamp += _localTimeOffset;
}
DateTime dateTime = timestamp;
for (std::vector<PatternAction>::iterator ip = _patternActions.begin(); ip != _patternActions.end(); ++ip)
{
text.append(ip->prepend);
switch (ip->key)
{
case 's': text.append(msg.getSource()); break;
case 't': text.append(msg.getText()); break;
case 'l': NumberFormatter::append(text, (int) msg.getPriority()); break;
case 'p': text.append(getPriorityName((int) msg.getPriority())); break;
case 'q': text += getPriorityName((int) msg.getPriority()).at(0); break;
case 'P': NumberFormatter::append(text, msg.getPid()); break;
case 'T': text.append(msg.getThread()); break;
case 'I': NumberFormatter::append(text, msg.getTid()); break;
case 'N': text.append(Environment::nodeName()); break;
case 'U': text.append(msg.getSourceFile() ? msg.getSourceFile() : ""); break;
case 'u': NumberFormatter::append(text, msg.getSourceLine()); break;
case 'w': text.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()], 0, 3); break;
case 'W': text.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()]); break;
case 'b': text.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1], 0, 3); break;
case 'B': text.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1]); break;
case 'd': NumberFormatter::append0(text, dateTime.day(), 2); break;
case 'e': NumberFormatter::append(text, dateTime.day()); break;
case 'f': NumberFormatter::append(text, dateTime.day(), 2); break;
case 'm': NumberFormatter::append0(text, dateTime.month(), 2); break;
case 'n': NumberFormatter::append(text, dateTime.month()); break;
case 'o': NumberFormatter::append(text, dateTime.month(), 2); break;
case 'y': NumberFormatter::append0(text, dateTime.year() % 100, 2); break;
case 'Y': NumberFormatter::append0(text, dateTime.year(), 4); break;
case 'H': NumberFormatter::append0(text, dateTime.hour(), 2); break;
case 'h': NumberFormatter::append0(text, dateTime.hourAMPM(), 2); break;
case 'a': text.append(dateTime.isAM() ? "am" : "pm"); break;
case 'A': text.append(dateTime.isAM() ? "AM" : "PM"); break;
case 'M': NumberFormatter::append0(text, dateTime.minute(), 2); break;
case 'S': NumberFormatter::append0(text, dateTime.second(), 2); break;
case 'i': NumberFormatter::append0(text, dateTime.millisecond(), 3); break;
case 'c': NumberFormatter::append(text, dateTime.millisecond()/100); break;
case 'F': NumberFormatter::append0(text, dateTime.millisecond()*1000 + dateTime.microsecond(), 6); break;
case 'z': text.append(DateTimeFormatter::tzdISO(_localTime ? Timezone::tzd() : DateTimeFormatter::UTC)); break;
case 'Z': text.append(DateTimeFormatter::tzdRFC(_localTime ? Timezone::tzd() : DateTimeFormatter::UTC)); break;
case 'E': NumberFormatter::append(text, msg.getTime().epochTime()); break;
case 'v':
if (ip->length > msg.getSource().length()) //append spaces
text.append(msg.getSource()).append(ip->length - msg.getSource().length(), ' ');
else if (ip->length && ip->length < msg.getSource().length()) // crop
text.append(msg.getSource(), msg.getSource().length()-ip->length, ip->length);
else
text.append(msg.getSource());
break;
case 'x':
try
{
text.append(msg[ip->property]);
}
catch (...)
{
}
break;
}
}
}
void PatternFormatter::ParsePattern()
{
_patternActions.clear();
std::string::const_iterator it = _pattern.begin();
std::string::const_iterator end = _pattern.end();
PatternAction end_act;
while (it != end)
{
if (*it == '%')
{
if (++it != end)
{
switch (*it)
{
case 's': text.append(msg.getSource()); break;
case 't': text.append(msg.getText()); break;
case 'l': NumberFormatter::append(text, (int) msg.getPriority()); break;
case 'p': text.append(getPriorityName((int) msg.getPriority())); break;
case 'q': text += getPriorityName((int) msg.getPriority()).at(0); break;
case 'P': NumberFormatter::append(text, msg.getPid()); break;
case 'T': text.append(msg.getThread()); break;
case 'I': NumberFormatter::append(text, msg.getTid()); break;
case 'N': text.append(Environment::nodeName()); break;
case 'U': text.append(msg.getSourceFile() ? msg.getSourceFile() : ""); break;
case 'u': NumberFormatter::append(text, msg.getSourceLine()); break;
case 'w': text.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()], 0, 3); break;
case 'W': text.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()]); break;
case 'b': text.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1], 0, 3); break;
case 'B': text.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1]); break;
case 'd': NumberFormatter::append0(text, dateTime.day(), 2); break;
case 'e': NumberFormatter::append(text, dateTime.day()); break;
case 'f': NumberFormatter::append(text, dateTime.day(), 2); break;
case 'm': NumberFormatter::append0(text, dateTime.month(), 2); break;
case 'n': NumberFormatter::append(text, dateTime.month()); break;
case 'o': NumberFormatter::append(text, dateTime.month(), 2); break;
case 'y': NumberFormatter::append0(text, dateTime.year() % 100, 2); break;
case 'Y': NumberFormatter::append0(text, dateTime.year(), 4); break;
case 'H': NumberFormatter::append0(text, dateTime.hour(), 2); break;
case 'h': NumberFormatter::append0(text, dateTime.hourAMPM(), 2); break;
case 'a': text.append(dateTime.isAM() ? "am" : "pm"); break;
case 'A': text.append(dateTime.isAM() ? "AM" : "PM"); break;
case 'M': NumberFormatter::append0(text, dateTime.minute(), 2); break;
case 'S': NumberFormatter::append0(text, dateTime.second(), 2); break;
case 'i': NumberFormatter::append0(text, dateTime.millisecond(), 3); break;
case 'c': NumberFormatter::append(text, dateTime.millisecond()/100); break;
case 'F': NumberFormatter::append0(text, dateTime.millisecond()*1000 + dateTime.microsecond(), 6); break;
case 'z': text.append(DateTimeFormatter::tzdISO(_localTime ? Timezone::tzd() : DateTimeFormatter::UTC)); break;
case 'Z': text.append(DateTimeFormatter::tzdRFC(_localTime ? Timezone::tzd() : DateTimeFormatter::UTC)); break;
case 'E': NumberFormatter::append(text, msg.getTime().epochTime()); break;
case '[':
PatternAction act;
act.prepend = end_act.prepend;
end_act.prepend.clear();
if(*it == '[')
{
act.key='x';
++it;
std::string prop;
while (it != end && *it != ']') prop += *it++;
if (it == end) --it;
try
{
text.append(msg[prop]);
}
catch (...)
{
}
break;
act.property = prop;
}
default: text += *it;
else
{
act.key=*it;
if ((it+1) != end && *(it+1) == '[')
{
it+=2;
std::string number;
while (it != end && *it != ']') number += *it++;
if (it == end) --it;
try
{
act.length = NumberParser::parse(number);
}
catch(...)
{
}
}
}
_patternActions.push_back(act);
++it;
}
}
else text += *it++;
else
{
end_act.prepend += *it++;
}
}
if( end_act.prepend.size())
_patternActions.push_back(end_act);
}
void PatternFormatter::setProperty(const std::string& name, const std::string& value)
{
if (name == PROP_PATTERN)
{
_pattern = value;
ParsePattern();
}
else if (name == PROP_TIMES)
{
_localTime = (value == "local");
_localTimeOffset = Timestamp::resolution()*( Timezone::utcOffset() + Timezone::dst() );
}
else
Formatter::setProperty(name, value);
}