added %L modifier to PatternFormatter to switch to local time; some style fixes

This commit is contained in:
Guenter Obiltschnig 2014-09-10 08:32:25 +02:00
parent c9590ff7d4
commit a815e0a90e
2 changed files with 31 additions and 18 deletions

View File

@ -71,6 +71,7 @@ class Foundation_API PatternFormatter: public Formatter
/// * %F - message date/time fractional seconds/microseconds (000000 - 999999) /// * %F - message date/time fractional seconds/microseconds (000000 - 999999)
/// * %z - time zone differential in ISO 8601 format (Z or +NN.NN) /// * %z - time zone differential in ISO 8601 format (Z or +NN.NN)
/// * %Z - time zone differential in RFC format (GMT or +NNNN) /// * %Z - time zone differential in RFC format (GMT or +NNNN)
/// * %L - convert time to local time (must be specified before any date/time specifier; does not itself output anything)
/// * %E - epoch time (UTC, seconds since midnight, January 1, 1970) /// * %E - epoch time (UTC, seconds since midnight, January 1, 1970)
/// * %v[width] - the message source (%s) but text length is padded/cropped to 'width' /// * %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 /// * %[name] - the value of the message parameter with the given name

View File

@ -35,14 +35,14 @@ const std::string PatternFormatter::PROP_TIMES = "times";
PatternFormatter::PatternFormatter(): PatternFormatter::PatternFormatter():
_localTime(false), _localTime(false),
_localTimeOffset(0) _localTimeOffset(Timestamp::resolution()*(Timezone::utcOffset() + Timezone::dst()))
{ {
} }
PatternFormatter::PatternFormatter(const std::string& format): PatternFormatter::PatternFormatter(const std::string& format):
_localTime(false), _localTime(false),
_localTimeOffset(0), _localTimeOffset(Timestamp::resolution()*(Timezone::utcOffset() + Timezone::dst())),
_pattern(format) _pattern(format)
{ {
ParsePattern(); ParsePattern();
@ -57,7 +57,8 @@ PatternFormatter::~PatternFormatter()
void PatternFormatter::format(const Message& msg, std::string& text) void PatternFormatter::format(const Message& msg, std::string& text)
{ {
Timestamp timestamp = msg.getTime(); Timestamp timestamp = msg.getTime();
if (_localTime) bool localTime = _localTime;
if (localTime)
{ {
timestamp += _localTimeOffset; timestamp += _localTimeOffset;
} }
@ -99,8 +100,8 @@ void PatternFormatter::format(const Message& msg, std::string& text)
case 'i': NumberFormatter::append0(text, dateTime.millisecond(), 3); break; case 'i': NumberFormatter::append0(text, dateTime.millisecond(), 3); break;
case 'c': NumberFormatter::append(text, dateTime.millisecond()/100); break; case 'c': NumberFormatter::append(text, dateTime.millisecond()/100); break;
case 'F': NumberFormatter::append0(text, dateTime.millisecond()*1000 + dateTime.microsecond(), 6); 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::tzdISO(localTime ? Timezone::tzd() : DateTimeFormatter::UTC)); break;
case 'Z': text.append(DateTimeFormatter::tzdRFC(_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 'E': NumberFormatter::append(text, msg.getTime().epochTime()); break;
case 'v': case 'v':
if (ip->length > msg.getSource().length()) //append spaces if (ip->length > msg.getSource().length()) //append spaces
@ -119,6 +120,14 @@ void PatternFormatter::format(const Message& msg, std::string& text)
{ {
} }
break; break;
case 'L':
if (!localTime)
{
localTime = true;
timestamp += _localTimeOffset;
dateTime = timestamp;
}
break;
} }
} }
} }
@ -128,7 +137,7 @@ void PatternFormatter::ParsePattern()
_patternActions.clear(); _patternActions.clear();
std::string::const_iterator it = _pattern.begin(); std::string::const_iterator it = _pattern.begin();
std::string::const_iterator end = _pattern.end(); std::string::const_iterator end = _pattern.end();
PatternAction end_act; PatternAction endAct;
while (it != end) while (it != end)
{ {
if (*it == '%') if (*it == '%')
@ -136,8 +145,8 @@ void PatternFormatter::ParsePattern()
if (++it != end) if (++it != end)
{ {
PatternAction act; PatternAction act;
act.prepend = end_act.prepend; act.prepend = endAct.prepend;
end_act.prepend.clear(); endAct.prepend.clear();
if (*it == '[') if (*it == '[')
{ {
@ -172,11 +181,13 @@ void PatternFormatter::ParsePattern()
} }
else else
{ {
end_act.prepend += *it++; endAct.prepend += *it++;
} }
} }
if( end_act.prepend.size()) if (endAct.prepend.size())
_patternActions.push_back(end_act); {
_patternActions.push_back(endAct);
}
} }
@ -190,11 +201,12 @@ void PatternFormatter::setProperty(const std::string& name, const std::string& v
else if (name == PROP_TIMES) else if (name == PROP_TIMES)
{ {
_localTime = (value == "local"); _localTime = (value == "local");
_localTimeOffset = Timestamp::resolution()*( Timezone::utcOffset() + Timezone::dst() );
} }
else else
{
Formatter::setProperty(name, value); Formatter::setProperty(name, value);
} }
}
std::string PatternFormatter::getProperty(const std::string& name) const std::string PatternFormatter::getProperty(const std::string& name) const