1 | // |
2 | // RotateStrategy.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: Logging |
6 | // Module: FileChannel |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/RotateStrategy.h" |
16 | #include "Poco/FileStream.h" |
17 | #include "Poco/DateTimeParser.h" |
18 | #include "Poco/DateTimeFormatter.h" |
19 | #include "Poco/DateTimeFormat.h" |
20 | #include "Poco/LineEndingConverter.h" |
21 | |
22 | |
23 | namespace Poco { |
24 | |
25 | |
26 | // |
27 | // RotateStrategy |
28 | // |
29 | |
30 | |
31 | RotateStrategy::RotateStrategy() |
32 | { |
33 | } |
34 | |
35 | |
36 | RotateStrategy::~RotateStrategy() |
37 | { |
38 | } |
39 | |
40 | |
41 | // |
42 | // RotateByIntervalStrategy |
43 | // |
44 | |
45 | |
46 | const std::string RotateByIntervalStrategy::ROTATE_TEXT("# Log file created/rotated " ); |
47 | |
48 | |
49 | RotateByIntervalStrategy::RotateByIntervalStrategy(const Timespan& span): |
50 | _span(span), |
51 | _lastRotate(0) |
52 | { |
53 | if (span.totalMicroseconds() <= 0) throw InvalidArgumentException("time span must be greater than zero" ); |
54 | } |
55 | |
56 | |
57 | RotateByIntervalStrategy::~RotateByIntervalStrategy() |
58 | { |
59 | } |
60 | |
61 | |
62 | bool RotateByIntervalStrategy::mustRotate(LogFile* pFile) |
63 | { |
64 | if (_lastRotate == 0 || pFile->size() == 0) |
65 | { |
66 | if (pFile->size() != 0) |
67 | { |
68 | Poco::FileInputStream istr(pFile->path()); |
69 | Poco::InputLineEndingConverter converter(istr, Poco::LineEnding::NEWLINE_LF); |
70 | std::string tag; |
71 | std::getline(converter, tag); |
72 | if (tag.compare(0, ROTATE_TEXT.size(), ROTATE_TEXT) == 0) |
73 | { |
74 | std::string timestamp(tag, ROTATE_TEXT.size()); |
75 | int tzd; |
76 | _lastRotate = DateTimeParser::parse(DateTimeFormat::RFC1036_FORMAT, timestamp, tzd).timestamp(); |
77 | } |
78 | else _lastRotate = pFile->creationDate(); |
79 | } |
80 | else |
81 | { |
82 | _lastRotate.update(); |
83 | std::string tag(ROTATE_TEXT); |
84 | DateTimeFormatter::append(tag, _lastRotate, DateTimeFormat::RFC1036_FORMAT); |
85 | pFile->write(tag); |
86 | } |
87 | } |
88 | Timestamp now; |
89 | return _span <= now - _lastRotate; |
90 | } |
91 | |
92 | |
93 | // |
94 | // RotateBySizeStrategy |
95 | // |
96 | |
97 | |
98 | RotateBySizeStrategy::RotateBySizeStrategy(UInt64 size): _size(size) |
99 | { |
100 | if (size == 0) throw InvalidArgumentException("size must be greater than zero" ); |
101 | } |
102 | |
103 | |
104 | RotateBySizeStrategy::~RotateBySizeStrategy() |
105 | { |
106 | } |
107 | |
108 | |
109 | bool RotateBySizeStrategy::mustRotate(LogFile* pFile) |
110 | { |
111 | return pFile->size() >= _size; |
112 | } |
113 | |
114 | |
115 | } // namespace Poco |
116 | |