fixed log rotation issue

This commit is contained in:
Guenter Obiltschnig
2008-09-18 14:18:44 +00:00
parent be8daabe3c
commit b5b35cb173
5 changed files with 65 additions and 32 deletions

View File

@@ -1,7 +1,7 @@
//
// RotateStrategy.cpp
//
// $Id: //poco/svn/Foundation/src/RotateStrategy.cpp#2 $
// $Id: //poco/1.3/Foundation/src/RotateStrategy.cpp#2 $
//
// Library: Foundation
// Package: Logging
@@ -35,6 +35,10 @@
#include "Poco/RotateStrategy.h"
#include "Poco/FileStream.h"
#include "Poco/DateTimeParser.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/DateTimeFormat.h"
namespace Poco {
@@ -60,7 +64,12 @@ RotateStrategy::~RotateStrategy()
//
RotateByIntervalStrategy::RotateByIntervalStrategy(const Timespan& span): _span(span)
const std::string RotateByIntervalStrategy::ROTATE_TEXT("# Log file created/rotated ");
RotateByIntervalStrategy::RotateByIntervalStrategy(const Timespan& span):
_span(span),
_lastRotate(0)
{
if (span.totalMicroseconds() <= 0) throw InvalidArgumentException("time span must be greater than zero");
}
@@ -73,8 +82,31 @@ RotateByIntervalStrategy::~RotateByIntervalStrategy()
bool RotateByIntervalStrategy::mustRotate(LogFile* pFile)
{
if (_lastRotate == 0 || pFile->size() == 0)
{
if (pFile->size() != 0)
{
Poco::FileInputStream istr(pFile->path());
std::string tag;
std::getline(istr, tag);
if (tag.compare(0, ROTATE_TEXT.size(), ROTATE_TEXT) == 0)
{
std::string timestamp(tag, ROTATE_TEXT.size());
int tzd;
_lastRotate = DateTimeParser::parse(DateTimeFormat::RFC1036_FORMAT, timestamp, tzd).timestamp();
}
else _lastRotate = pFile->creationDate();
}
else
{
_lastRotate.update();
std::string tag(ROTATE_TEXT);
tag += DateTimeFormatter::format(_lastRotate, DateTimeFormat::RFC1036_FORMAT);
pFile->write(tag);
}
}
Timestamp now;
return _span <= now - pFile->creationDate();
return _span <= now - _lastRotate;
}