/src/logging-log4cxx/src/main/include/log4cxx/rolling/fixedwindowrollingpolicy.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | * contributor license agreements. See the NOTICE file distributed with |
4 | | * this work for additional information regarding copyright ownership. |
5 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | * (the "License"); you may not use this file except in compliance with |
7 | | * the License. You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #if !defined(_LOG4CXX_ROLLING_FIXED_WINDOW_ROLLING_POLICY_H) |
19 | | #define _LOG4CXX_ROLLING_FIXED_WINDOW_ROLLING_POLICY_H |
20 | | |
21 | | #include <log4cxx/rolling/rollingpolicybase.h> |
22 | | |
23 | | |
24 | | |
25 | | |
26 | | namespace LOG4CXX_NS |
27 | | { |
28 | | |
29 | | namespace helpers |
30 | | { |
31 | | class Pool; |
32 | | } |
33 | | |
34 | | namespace rolling |
35 | | { |
36 | | |
37 | | |
38 | | /** |
39 | | * When rolling over, <code>FixedWindowRollingPolicy</code> renames files |
40 | | * according to a fixed window algorithm as described below. |
41 | | * |
42 | | * <p>The <b>ActiveFileName</b> property, which is required, represents the name |
43 | | * of the file where current logging output will be written. |
44 | | * The <b>FileNamePattern</b> option represents the file name pattern for the |
45 | | * archived (rolled over) log files. If present, the <b>FileNamePattern</b> |
46 | | * option must include an integer token, that is the string "%i" somwhere |
47 | | * within the pattern. |
48 | | * |
49 | | * <p>Let <em>max</em> and <em>min</em> represent the values of respectively |
50 | | * the <b>MaxIndex</b> and <b>MinIndex</b> options. Let "foo.log" be the value |
51 | | * of the <b>ActiveFile</b> option and "foo.%i.log" the value of |
52 | | * <b>FileNamePattern</b>. Then, when rolling over, the file |
53 | | * <code>foo.<em>max</em>.log</code> will be deleted, the file |
54 | | * <code>foo.<em>max-1</em>.log</code> will be renamed as |
55 | | * <code>foo.<em>max</em>.log</code>, the file <code>foo.<em>max-2</em>.log</code> |
56 | | * renamed as <code>foo.<em>max-1</em>.log</code>, and so on, |
57 | | * the file <code>foo.<em>min+1</em>.log</code> renamed as |
58 | | * <code>foo.<em>min+2</em>.log</code>. Lastly, the active file <code>foo.log</code> |
59 | | * will be renamed as <code>foo.<em>min</em>.log</code> and a new active file name |
60 | | * <code>foo.log</code> will be created. |
61 | | * |
62 | | * <p>Given that this rollover algorithm requires as many file renaming |
63 | | * operations as the window size, large window sizes are discouraged. The |
64 | | * current implementation will automatically reduce the window size to 12 when |
65 | | * larger values are specified by the user. |
66 | | * |
67 | | * |
68 | | * |
69 | | * |
70 | | * */ |
71 | | class LOG4CXX_EXPORT FixedWindowRollingPolicy : public RollingPolicyBase |
72 | | { |
73 | | DECLARE_LOG4CXX_OBJECT(FixedWindowRollingPolicy) |
74 | 0 | BEGIN_LOG4CXX_CAST_MAP() |
75 | 0 | LOG4CXX_CAST_ENTRY(FixedWindowRollingPolicy) |
76 | 0 | LOG4CXX_CAST_ENTRY_CHAIN(RollingPolicyBase) |
77 | 0 | END_LOG4CXX_CAST_MAP() |
78 | | |
79 | | struct FixedWindowRollingPolicyPrivate; |
80 | | |
81 | | /** |
82 | | * It's almost always a bad idea to have a large window size, say over 12. |
83 | | */ |
84 | | enum { MAX_WINDOW_SIZE = 12 }; |
85 | | |
86 | | bool purge(int purgeStart, int maxIndex, LOG4CXX_NS::helpers::Pool& p) const; |
87 | | |
88 | | public: |
89 | | |
90 | | FixedWindowRollingPolicy(); |
91 | | ~FixedWindowRollingPolicy(); |
92 | | |
93 | | /** |
94 | | \copybrief RollingPolicyBase::activateOptions() |
95 | | |
96 | | Logs a warning if an option is not valid. |
97 | | |
98 | | \sa RollingPolicyBase::activateOptions() |
99 | | */ |
100 | | void activateOptions(helpers::Pool& p) override; |
101 | | |
102 | | /** |
103 | | \copybrief RollingPolicyBase::setOption() |
104 | | |
105 | | Supported options | Supported values | Default value |
106 | | :-------------- | :----------------: | :---------------: |
107 | | MinIndex | 1-12 | 1 |
108 | | MaxIndex | 1-12 | 7 |
109 | | ThrowIOExceptionOnForkFailure | True,False | True |
110 | | |
111 | | \sa RollingPolicyBase::setOption() |
112 | | */ |
113 | | void setOption(const LogString& option, const LogString& value) override; |
114 | | |
115 | | void rollover(); |
116 | | |
117 | | int getMaxIndex() const; |
118 | | |
119 | | int getMinIndex() const; |
120 | | |
121 | | void setMaxIndex(int newVal); |
122 | | void setMinIndex(int newVal); |
123 | | |
124 | | /** |
125 | | * {@inheritDoc} |
126 | | */ |
127 | | RolloverDescriptionPtr initialize( |
128 | | const LogString& currentActiveFile, |
129 | | const bool append, |
130 | | helpers::Pool& pool) override; |
131 | | |
132 | | /** |
133 | | * {@inheritDoc} |
134 | | */ |
135 | | RolloverDescriptionPtr rollover( |
136 | | const LogString& currentActiveFile, |
137 | | const bool append, |
138 | | helpers::Pool& pool) override; |
139 | | |
140 | | protected: |
141 | | /** |
142 | | * A map from "i" and "index" to a integer conversion formatter. |
143 | | * |
144 | | * \sa IntegerPatternConverter |
145 | | */ |
146 | | LOG4CXX_NS::pattern::PatternMap getFormatSpecifiers() const override; |
147 | | |
148 | | }; |
149 | | |
150 | | LOG4CXX_PTR_DEF(FixedWindowRollingPolicy); |
151 | | |
152 | | } |
153 | | } |
154 | | |
155 | | #endif |