/src/logging-log4cxx/src/main/include/log4cxx/rolling/timebasedrollingpolicy.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 | | |
19 | | #if !defined(_LOG4CXX_ROLLING_TIME_BASED_ROLLING_POLICY_H) |
20 | | #define _LOG4CXX_ROLLING_TIME_BASED_ROLLING_POLICY_H |
21 | | |
22 | | #include <log4cxx/rolling/rollingpolicybase.h> |
23 | | #include <log4cxx/rolling/triggeringpolicy.h> |
24 | | #include <log4cxx/writerappender.h> |
25 | | #include <log4cxx/helpers/outputstream.h> |
26 | | #include <functional> |
27 | | |
28 | | namespace LOG4CXX_NS |
29 | | { |
30 | | |
31 | | namespace rolling |
32 | | { |
33 | | |
34 | | |
35 | | |
36 | | /** |
37 | | * <code>TimeBasedRollingPolicy</code> is both easy to configure and quite |
38 | | * powerful. |
39 | | * |
40 | | * <p>In order to use <code>TimeBasedRollingPolicy</code>, the |
41 | | * <b>FileNamePattern</b> option must be set. It basically specifies the name of the |
42 | | * rolled log files. The value <code>FileNamePattern</code> should consist of |
43 | | * the name of the file, plus a suitably placed <code>\%d</code> conversion |
44 | | * specifier. The <code>\%d</code> conversion specifier may contain a date and |
45 | | * time pattern as specified by the {@link log4cxx::helpers::SimpleDateFormat} class. If |
46 | | * the date and time pattern is ommitted, then the default pattern of |
47 | | * "yyyy-MM-dd" is assumed. The following examples should clarify the point. |
48 | | * |
49 | | * <p> |
50 | | * <table cellspacing="5px" border="1"> |
51 | | * <tr> |
52 | | * <th><code>FileNamePattern</code> value</th> |
53 | | * <th>Rollover schedule</th> |
54 | | * <th>Example</th> |
55 | | * </tr> |
56 | | * <tr> |
57 | | * <td nowrap="true"><code>/wombat/folder/foo.\%d</code></td> |
58 | | * <td>Daily rollover (at midnight). Due to the omission of the optional |
59 | | * time and date pattern for the \%d token specifier, the default pattern |
60 | | * of "yyyy-MM-dd" is assumed, which corresponds to daily rollover. |
61 | | * </td> |
62 | | * <td>During November 23rd, 2004, logging output will go to |
63 | | * the file <code>/wombat/foo.2004-11-23</code>. At midnight and for |
64 | | * the rest of the 24th, logging output will be directed to |
65 | | * <code>/wombat/foo.2004-11-24</code>. |
66 | | * </td> |
67 | | * </tr> |
68 | | * <tr> |
69 | | * <td nowrap="true"><code>/wombat/foo.\%d{yyyy-MM}.log</code></td> |
70 | | * <td>Rollover at the beginning of each month.</td> |
71 | | * <td>During the month of October 2004, logging output will go to |
72 | | * <code>/wombat/foo.2004-10.log</code>. After midnight of October 31st |
73 | | * and for the rest of November, logging output will be directed to |
74 | | * <code>/wombat/foo.2004-11.log</code>. |
75 | | * </td> |
76 | | * </tr> |
77 | | * </table> |
78 | | * <h2>Automatic file compression</h2> |
79 | | * <code>TimeBasedRollingPolicy</code> supports automatic file compression. |
80 | | * This feature is enabled if the value of the <b>FileNamePattern</b> option |
81 | | * ends with <code>.gz</code> or <code>.zip</code>. |
82 | | * <p> |
83 | | * <table cellspacing="5px" border="1"> |
84 | | * <tr> |
85 | | * <th><code>FileNamePattern</code> value</th> |
86 | | * <th>Rollover schedule</th> |
87 | | * <th>Example</th> |
88 | | * </tr> |
89 | | * <tr> |
90 | | * <td nowrap="true"><code>/wombat/foo.\%d.gz</code></td> |
91 | | * <td>Daily rollover (at midnight) with automatic GZIP compression of the |
92 | | * arcived files.</td> |
93 | | * <td>During November 23rd, 2004, logging output will go to |
94 | | * the file <code>/wombat/foo.2004-11-23</code>. However, at midnight that |
95 | | * file will be compressed to become <code>/wombat/foo.2004-11-23.gz</code>. |
96 | | * For the 24th of November, logging output will be directed to |
97 | | * <code>/wombat/folder/foo.2004-11-24</code> until its rolled over at the |
98 | | * beginning of the next day. |
99 | | * </td> |
100 | | * </tr> |
101 | | * </table> |
102 | | * |
103 | | * <h2>Decoupling the location of the active log file and the archived log files</h2> |
104 | | * <p>The <em>active file</em> is defined as the log file for the current period |
105 | | * whereas <em>archived files</em> are thos files which have been rolled over |
106 | | * in previous periods. |
107 | | * |
108 | | * <p>By setting the <b>ActiveFileName</b> option you can decouple the location |
109 | | * of the active log file and the location of the archived log files. |
110 | | * <p> |
111 | | * <table cellspacing="5px" border="1"> |
112 | | * <tr> |
113 | | * <th><code>FileNamePattern</code> value</th> |
114 | | * <th>ActiveFileName</th> |
115 | | * <th>Rollover schedule</th> |
116 | | * <th>Example</th> |
117 | | * </tr> |
118 | | * <tr> |
119 | | * <td nowrap="true"><code>/wombat/foo.log.\%d</code></td> |
120 | | * <td nowrap="true"><code>/wombat/foo.log</code></td> |
121 | | * <td>Daily rollover.</td> |
122 | | * |
123 | | * <td>During November 23rd, 2004, logging output will go to |
124 | | * the file <code>/wombat/foo.log</code>. However, at midnight that file |
125 | | * will archived as <code>/wombat/foo.log.2004-11-23</code>. For the 24th |
126 | | * of November, logging output will be directed to |
127 | | * <code>/wombat/folder/foo.log</code> until its archived as |
128 | | * <code>/wombat/foo.log.2004-11-24</code> at the beginning of the next |
129 | | * day. |
130 | | * </td> |
131 | | * </tr> |
132 | | * </table> |
133 | | * <p> |
134 | | * If configuring programatically, do not forget to call {@link #activateOptions} |
135 | | * method before using this policy. Moreover, {@link #activateOptions} of |
136 | | * <code> TimeBasedRollingPolicy</code> must be called <em>before</em> calling |
137 | | * the {@link #activateOptions} method of the owning |
138 | | * <code>RollingFileAppender</code>. |
139 | | */ |
140 | | class LOG4CXX_EXPORT TimeBasedRollingPolicy : public virtual RollingPolicyBase, |
141 | | public virtual TriggeringPolicy |
142 | | { |
143 | | DECLARE_LOG4CXX_OBJECT(TimeBasedRollingPolicy) |
144 | 477 | BEGIN_LOG4CXX_CAST_MAP() |
145 | 477 | LOG4CXX_CAST_ENTRY(TimeBasedRollingPolicy) |
146 | 477 | LOG4CXX_CAST_ENTRY_CHAIN(RollingPolicyBase) |
147 | 477 | LOG4CXX_CAST_ENTRY_CHAIN(TriggeringPolicy) |
148 | 0 | END_LOG4CXX_CAST_MAP() |
149 | | |
150 | | private: |
151 | | LOG4CXX_DECLARE_PRIVATE_MEMBER_PTR(TimeBasedRollingPolicyPrivate, m_priv) |
152 | | |
153 | | public: |
154 | | TimeBasedRollingPolicy(); |
155 | | virtual ~TimeBasedRollingPolicy(); |
156 | | |
157 | | /** |
158 | | \copybrief RollingPolicyBase::activateOptions() |
159 | | |
160 | | Logs a warning if an option is not valid. |
161 | | |
162 | | \sa RollingPolicyBase::activateOptions() |
163 | | */ |
164 | | void activateOptions(helpers::Pool& ) override; |
165 | | |
166 | | void setMultiprocess(bool multiprocess); |
167 | | |
168 | | /** |
169 | | * {@inheritDoc} |
170 | | */ |
171 | | RolloverDescriptionPtr initialize( |
172 | | const LogString& currentActiveFile, |
173 | | const bool append, |
174 | | helpers::Pool& pool) override; |
175 | | |
176 | | /** |
177 | | * {@inheritDoc} |
178 | | */ |
179 | | RolloverDescriptionPtr rollover( |
180 | | const LogString& currentActiveFile, |
181 | | const bool append, |
182 | | helpers::Pool& pool) override; |
183 | | |
184 | | /** |
185 | | * Determines if a rollover may be appropriate at this time. If |
186 | | * true is returned, RolloverPolicy.rollover will be called but it |
187 | | * can determine that a rollover is not warranted. |
188 | | * |
189 | | * @param appender A reference to the appender. |
190 | | * @param event A reference to the currently event. |
191 | | * @param filename The filename for the currently active log file. |
192 | | * @param fileLength Length of the file in bytes. |
193 | | * @return true if a rollover should occur. |
194 | | */ |
195 | | bool isTriggeringEvent( |
196 | | Appender* appender, |
197 | | const spi::LoggingEventPtr& event, |
198 | | const LogString& filename, |
199 | | size_t fileLength) override; |
200 | | |
201 | | /** |
202 | | \copybrief RollingPolicyBase::setOption() |
203 | | |
204 | | Supported options | Supported values | Default value |
205 | | :-------------- | :----------------: | :---------------: |
206 | | ThrowIOExceptionOnForkFailure | True,False | True |
207 | | |
208 | | \sa RollingPolicyBase::setOption() |
209 | | */ |
210 | | void setOption(const LogString& option, const LogString& value) override; |
211 | | |
212 | | /** |
213 | | * Was the name in shared memory set by this process? |
214 | | */ |
215 | | bool isLastFileNameUnchanged(); |
216 | | |
217 | | /** |
218 | | * Load the name (set by some other process) from shared memory |
219 | | */ |
220 | | void loadLastFileName(); |
221 | | |
222 | | protected: |
223 | | /** |
224 | | * A map from "d" and "date" to a date conversion formatter. |
225 | | * |
226 | | * \sa FileDatePatternConverter |
227 | | */ |
228 | | LOG4CXX_NS::pattern::PatternMap getFormatSpecifiers() const override; |
229 | | |
230 | | private: |
231 | | |
232 | | /** |
233 | | * Generate mmap file |
234 | | */ |
235 | | int createMMapFile(const std::string& lastfilename, LOG4CXX_NS::helpers::Pool& pool); |
236 | | |
237 | | /** |
238 | | * Detect if the mmap file is empty |
239 | | */ |
240 | | bool isMapFileEmpty(LOG4CXX_NS::helpers::Pool& pool); |
241 | | |
242 | | /** |
243 | | * init MMapFile |
244 | | */ |
245 | | void initMMapFile(const LogString& lastFileName, LOG4CXX_NS::helpers::Pool& pool); |
246 | | |
247 | | /** |
248 | | * lock MMapFile |
249 | | */ |
250 | | int lockMMapFile(int type); |
251 | | |
252 | | /** |
253 | | * unlock MMapFile |
254 | | */ |
255 | | int unLockMMapFile(); |
256 | | |
257 | | /** |
258 | | * create MMapFile/lockFile |
259 | | */ |
260 | | const std::string createFile(const std::string& filename, const std::string& suffix, LOG4CXX_NS::helpers::Pool& pool); |
261 | | |
262 | | }; |
263 | | |
264 | | LOG4CXX_PTR_DEF(TimeBasedRollingPolicy); |
265 | | |
266 | | } |
267 | | } |
268 | | |
269 | | #endif |
270 | | |