FileChannel: added "none" to the PURGE_AGE and PURGE_COUNT

This commit is contained in:
Marian Krivos
2012-04-29 10:58:27 +00:00
parent 53fe184bfa
commit c816693153
3 changed files with 409 additions and 372 deletions

View File

@@ -140,7 +140,7 @@ class Foundation_API FileChannel: public Channel
/// * local: Rotation strategy is based on local time. /// * local: Rotation strategy is based on local time.
/// ///
/// Archived log files can be compressed using the gzip compression /// Archived log files can be compressed using the gzip compression
/// method. Compressing can be controlled with the "compress" /// method. Compressing can be controlled with the "compression"
/// property. The following values for the "compress" property /// property. The following values for the "compress" property
/// are supported: /// are supported:
/// ///
@@ -154,6 +154,7 @@ class Foundation_API FileChannel: public Channel
/// ///
/// The purgeAge property can have the following values: /// The purgeAge property can have the following values:
/// ///
/// * "none" or "" no purging
/// * <n> [seconds] the maximum age is <n> seconds. /// * <n> [seconds] the maximum age is <n> seconds.
/// * <n> minutes: the maximum age is <n> minutes. /// * <n> minutes: the maximum age is <n> minutes.
/// * <n> hours: the maximum age is <n> hours. /// * <n> hours: the maximum age is <n> hours.
@@ -161,10 +162,10 @@ class Foundation_API FileChannel: public Channel
/// * <n> weeks: the maximum age is <n> weeks. /// * <n> weeks: the maximum age is <n> weeks.
/// * <n> months: the maximum age is <n> months, where a month has 30 days. /// * <n> months: the maximum age is <n> months, where a month has 30 days.
/// ///
/// The purgeCount property has an integer value that /// The purgeCount property has an integer value that specifies the maximum number
/// specifies the maximum number of archived log files. /// of archived log files. If the number is exceeded, archived log files are
/// If the number is exceeded, archived log files are /// deleted, starting with the oldest. When "none" or empty string are
/// deleted, starting with the oldest. /// supplied, they reset purgeCount to none (no purging).
/// ///
/// For a more lightweight file channel class, see SimpleFileChannel. /// For a more lightweight file channel class, see SimpleFileChannel.
{ {

View File

@@ -302,14 +302,28 @@ void FileChannel::setCompress(const std::string& compress)
void FileChannel::setPurgeAge(const std::string& age) void FileChannel::setPurgeAge(const std::string& age)
{ {
std::string::const_iterator it = age.begin(); delete _pPurgeStrategy;
_pPurgeStrategy = 0;
_purgeAge = "none";
if (age.empty() || 0 == icompare(age, "none"))
return;
std::string::const_iterator it = age.begin();
std::string::const_iterator end = age.end(); std::string::const_iterator end = age.end();
int n = 0; int n = 0;
while (it != end && Ascii::isSpace(*it)) ++it; while (it != end && Ascii::isSpace(*it))
while (it != end && Ascii::isDigit(*it)) { n *= 10; n += *it++ - '0'; } ++it;
while (it != end && Ascii::isSpace(*it)) ++it; while (it != end && Ascii::isDigit(*it))
{
n *= 10;
n += *it++ - '0';
}
while (it != end && Ascii::isSpace(*it))
++it;
std::string unit; std::string unit;
while (it != end && Ascii::isAlpha(*it)) unit += *it++; while (it != end && Ascii::isAlpha(*it))
unit += *it++;
Timespan::TimeDiff factor = Timespan::SECONDS; Timespan::TimeDiff factor = Timespan::SECONDS;
if (unit == "minutes") if (unit == "minutes")
@@ -321,26 +335,43 @@ void FileChannel::setPurgeAge(const std::string& age)
else if (unit == "weeks") else if (unit == "weeks")
factor = 7*Timespan::DAYS; factor = 7*Timespan::DAYS;
else if (unit == "months") else if (unit == "months")
factor = 30*Timespan::DAYS; factor = 30 * Timespan::DAYS;
else if (unit != "seconds") else if (unit != "seconds")
throw InvalidArgumentException("purgeAge", age); throw InvalidArgumentException("purgeAge", age);
delete _pPurgeStrategy; if (0 == n)
_pPurgeStrategy = new PurgeByAgeStrategy(Timespan(factor*n)); throw InvalidArgumentException("Zero is not valid purge age.");
_pPurgeStrategy = new PurgeByAgeStrategy(Timespan(factor * n));
_purgeAge = age; _purgeAge = age;
} }
void FileChannel::setPurgeCount(const std::string& count) void FileChannel::setPurgeCount(const std::string& count)
{ {
std::string::const_iterator it = count.begin();
std::string::const_iterator end = count.end();
int n = 0;
while (it != end && Ascii::isSpace(*it)) ++it;
while (it != end && Ascii::isDigit(*it)) { n *= 10; n += *it++ - '0'; }
while (it != end && Ascii::isSpace(*it)) ++it;
delete _pPurgeStrategy; delete _pPurgeStrategy;
_pPurgeStrategy = 0;
_purgeAge = "none";
if (count.empty() || 0 == icompare(count, "none"))
return;
int n = 0;
std::string::const_iterator it = count.begin();
std::string::const_iterator end = count.end();
while (it != end && Ascii::isSpace(*it))
++it;
while (it != end && Ascii::isDigit(*it))
{
n *= 10;
n += *it++ - '0';
}
while (it != end && Ascii::isSpace(*it))
++it;
if (0 == n)
throw InvalidArgumentException("Zero is not valid purge count.");
_pPurgeStrategy = new PurgeByCountStrategy(n); _pPurgeStrategy = new PurgeByCountStrategy(n);
_purgeCount = count; _purgeCount = count;
} }

View File

@@ -72,9 +72,14 @@ public:
static CppUnit::Test* suite(); static CppUnit::Test* suite();
private: private:
template <class D> std::string rotation(TimeRotation rtype) const; template <class D> std::string rotation(TimeRotation rtype) const;
void remove(const std::string& baseName); void remove(const std::string& baseName);
std::string filename() const; std::string filename() const;
void purgeAge(const std::string& purgeAge);
void noPurgeAge(const std::string& purgeAge);
void purgeCount(const std::string& pc);
void noPurgeCount(const std::string& pc);
}; };