| 1 | // |
| 2 | // RotateStrategy.h |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: Logging |
| 6 | // Module: FileChannel |
| 7 | // |
| 8 | // Definition of the RotateStrategy class and subclasses. |
| 9 | // |
| 10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 11 | // and Contributors. |
| 12 | // |
| 13 | // SPDX-License-Identifier: BSL-1.0 |
| 14 | // |
| 15 | |
| 16 | |
| 17 | #ifndef Foundation_RotateStrategy_INCLUDED |
| 18 | #define Foundation_RotateStrategy_INCLUDED |
| 19 | |
| 20 | |
| 21 | #include "Poco/Foundation.h" |
| 22 | #include "Poco/Timespan.h" |
| 23 | #include "Poco/Timestamp.h" |
| 24 | #include "Poco/Exception.h" |
| 25 | #include "Poco/LogFile.h" |
| 26 | #include "Poco/StringTokenizer.h" |
| 27 | #include "Poco/DateTimeParser.h" |
| 28 | #include "Poco/NumberParser.h" |
| 29 | |
| 30 | |
| 31 | namespace Poco { |
| 32 | |
| 33 | |
| 34 | class Foundation_API RotateStrategy |
| 35 | /// The RotateStrategy is used by LogFile to determine when |
| 36 | /// a file must be rotated. |
| 37 | { |
| 38 | public: |
| 39 | RotateStrategy(); |
| 40 | virtual ~RotateStrategy(); |
| 41 | |
| 42 | virtual bool mustRotate(LogFile* pFile) = 0; |
| 43 | /// Returns true if the given log file must |
| 44 | /// be rotated, false otherwise. |
| 45 | |
| 46 | private: |
| 47 | RotateStrategy(const RotateStrategy&); |
| 48 | RotateStrategy& operator = (const RotateStrategy&); |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | template <class DT> |
| 53 | class RotateAtTimeStrategy: public RotateStrategy |
| 54 | /// The file is rotated at specified [day,][hour]:minute |
| 55 | { |
| 56 | public: |
| 57 | RotateAtTimeStrategy(const std::string& rtime): |
| 58 | _day(-1), |
| 59 | _hour(-1), |
| 60 | _minute(0) |
| 61 | { |
| 62 | if (rtime.empty()) |
| 63 | throw InvalidArgumentException("Rotation time must be specified." ); |
| 64 | |
| 65 | if ((rtime.find(',') != rtime.npos) && (rtime.find(':') == rtime.npos)) |
| 66 | throw InvalidArgumentException("Invalid rotation time specified." ); |
| 67 | |
| 68 | StringTokenizer timestr(rtime, ",:" , StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); |
| 69 | int index = 0; |
| 70 | |
| 71 | switch (timestr.count()) |
| 72 | { |
| 73 | case 3: // day,hh:mm |
| 74 | { |
| 75 | std::string::const_iterator it = timestr[index].begin(); |
| 76 | _day = DateTimeParser::parseDayOfWeek(it, timestr[index].end()); |
| 77 | ++index; |
| 78 | } |
| 79 | case 2: // hh:mm |
| 80 | _hour = NumberParser::parse(timestr[index]); |
| 81 | ++index; |
| 82 | case 1: // mm |
| 83 | _minute = NumberParser::parse(timestr[index]); |
| 84 | break; |
| 85 | default: |
| 86 | throw InvalidArgumentException("Invalid rotation time specified." ); |
| 87 | } |
| 88 | getNextRollover(); |
| 89 | } |
| 90 | |
| 91 | ~RotateAtTimeStrategy() |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | bool mustRotate(LogFile* /*pFile*/) |
| 96 | { |
| 97 | if (DT() >= _threshold) |
| 98 | { |
| 99 | getNextRollover(); |
| 100 | return true; |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | void getNextRollover() |
| 107 | { |
| 108 | Timespan tsp(0, 0, 1, 0, 1000); // 0,00:01:00.001 |
| 109 | do |
| 110 | { |
| 111 | _threshold += tsp; |
| 112 | } |
| 113 | while (!(_threshold.minute() == _minute && |
| 114 | (-1 == _hour || _threshold.hour() == _hour) && |
| 115 | (-1 == _day || _threshold.dayOfWeek() == _day))); |
| 116 | // round to :00.0 seconds |
| 117 | _threshold.assign(_threshold.year(), _threshold.month(), _threshold.day(), _threshold.hour(), _threshold.minute()); |
| 118 | } |
| 119 | |
| 120 | DT _threshold; |
| 121 | int _day; |
| 122 | int _hour; |
| 123 | int _minute; |
| 124 | }; |
| 125 | |
| 126 | |
| 127 | class Foundation_API RotateByIntervalStrategy: public RotateStrategy |
| 128 | /// The file is rotated when the log file |
| 129 | /// exceeds a given age. |
| 130 | /// |
| 131 | /// For this to work reliably across all platforms and file systems |
| 132 | /// (there are severe issues on most platforms finding out the real |
| 133 | /// creation date of a file), the creation date of the file is |
| 134 | /// written into the log file as the first entry. |
| 135 | { |
| 136 | public: |
| 137 | RotateByIntervalStrategy(const Timespan& span); |
| 138 | ~RotateByIntervalStrategy(); |
| 139 | bool mustRotate(LogFile* pFile); |
| 140 | |
| 141 | private: |
| 142 | Timespan _span; |
| 143 | Timestamp _lastRotate; |
| 144 | static const std::string ROTATE_TEXT; |
| 145 | }; |
| 146 | |
| 147 | |
| 148 | class Foundation_API RotateBySizeStrategy: public RotateStrategy |
| 149 | /// The file is rotated when the log file |
| 150 | /// exceeds a given size. |
| 151 | { |
| 152 | public: |
| 153 | RotateBySizeStrategy(UInt64 size); |
| 154 | ~RotateBySizeStrategy(); |
| 155 | bool mustRotate(LogFile* pFile); |
| 156 | |
| 157 | private: |
| 158 | UInt64 _size; |
| 159 | }; |
| 160 | |
| 161 | |
| 162 | } // namespace Poco |
| 163 | |
| 164 | |
| 165 | #endif // Foundation_RotateStrategy_INCLUDED |
| 166 | |