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 | |
21 | |
22 | namespace Poco { |
23 | |
24 | |
25 | // |
26 | // RotateStrategy |
27 | // |
28 | |
29 | |
30 | RotateStrategy::RotateStrategy() |
31 | { |
32 | } |
33 | |
34 | |
35 | RotateStrategy::~RotateStrategy() |
36 | { |
37 | } |
38 | |
39 | |
40 | // |
41 | // RotateByIntervalStrategy |
42 | // |
43 | |
44 | |
45 | const std::string RotateByIntervalStrategy::ROTATE_TEXT("# Log file created/rotated " ); |
46 | |
47 | |
48 | RotateByIntervalStrategy::RotateByIntervalStrategy(const Timespan& span): |
49 | _span(span), |
50 | _lastRotate(0) |
51 | { |
52 | if (span.totalMicroseconds() <= 0) throw InvalidArgumentException("time span must be greater than zero" ); |
53 | } |
54 | |
55 | |
56 | RotateByIntervalStrategy::~RotateByIntervalStrategy() |
57 | { |
58 | } |
59 | |
60 | |
61 | bool RotateByIntervalStrategy::mustRotate(LogFile* pFile) |
62 | { |
63 | if (_lastRotate == 0 || pFile->size() == 0) |
64 | { |
65 | if (pFile->size() != 0) |
66 | { |
67 | Poco::FileInputStream istr(pFile->path()); |
68 | std::string tag; |
69 | std::getline(istr, tag); |
70 | if (tag.compare(0, ROTATE_TEXT.size(), ROTATE_TEXT) == 0) |
71 | { |
72 | std::string timestamp(tag, ROTATE_TEXT.size()); |
73 | int tzd; |
74 | _lastRotate = DateTimeParser::parse(DateTimeFormat::RFC1036_FORMAT, timestamp, tzd).timestamp(); |
75 | } |
76 | else _lastRotate = pFile->creationDate(); |
77 | } |
78 | else |
79 | { |
80 | _lastRotate.update(); |
81 | std::string tag(ROTATE_TEXT); |
82 | DateTimeFormatter::append(tag, _lastRotate, DateTimeFormat::RFC1036_FORMAT); |
83 | pFile->write(tag); |
84 | } |
85 | } |
86 | Timestamp now; |
87 | return _span <= now - _lastRotate; |
88 | } |
89 | |
90 | |
91 | // |
92 | // RotateBySizeStrategy |
93 | // |
94 | |
95 | |
96 | RotateBySizeStrategy::RotateBySizeStrategy(UInt64 size): _size(size) |
97 | { |
98 | if (size == 0) throw InvalidArgumentException("size must be greater than zero" ); |
99 | } |
100 | |
101 | |
102 | RotateBySizeStrategy::~RotateBySizeStrategy() |
103 | { |
104 | } |
105 | |
106 | |
107 | bool RotateBySizeStrategy::mustRotate(LogFile* pFile) |
108 | { |
109 | return pFile->size() >= _size; |
110 | } |
111 | |
112 | |
113 | } // namespace Poco |
114 | |