Coverage Report

Created: 2026-08-13 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/include/log4cxx/rolling/timebasedrollingpolicy.h
Line
Count
Source
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
141
#if LOG4CXX_ABI_VERSION <= 15
142
  : public virtual RollingPolicyBase
143
  , public virtual TriggeringPolicy
144
#else
145
  : public RollingPolicyBase
146
  , public virtual TriggeringPolicy
147
#endif
148
{
149
    DECLARE_LOG4CXX_OBJECT(TimeBasedRollingPolicy)
150
3.99k
    BEGIN_LOG4CXX_CAST_MAP()
151
3.99k
    LOG4CXX_CAST_ENTRY(TimeBasedRollingPolicy)
152
3.99k
    LOG4CXX_CAST_ENTRY_CHAIN(RollingPolicyBase)
153
3.99k
    LOG4CXX_CAST_ENTRY_CHAIN(TriggeringPolicy)
154
2.66k
    END_LOG4CXX_CAST_MAP()
155
156
  private:
157
    LOG4CXX_DECLARE_PRIVATE_MEMBER_PTR(TimeBasedRollingPolicyPrivate, m_priv)
158
159
  public:
160
    TimeBasedRollingPolicy();
161
    virtual ~TimeBasedRollingPolicy();
162
163
    using RollingPolicyBase::activateOptions;
164
    /**
165
    \copybrief RollingPolicyBase::activateOptions()
166
167
    Logs a warning if an option is not valid.
168
169
    \sa RollingPolicyBase::activateOptions()
170
    */
171
    void activateOptions( LOG4CXX_ACTIVATE_OPTIONS_FORMAL_PARAMETERS) override;
172
173
    void setMultiprocess(bool multiprocess);
174
175
    using RollingPolicy::initialize;
176
    /**
177
     * {@inheritDoc}
178
     */
179
    RolloverDescriptionPtr initialize( LOG4CXX_ROLLING_POLICY_INITIALIZE_FORMAL_PARAMETERS ) override;
180
181
    using RollingPolicy::rollover;
182
    /**
183
     * {@inheritDoc}
184
     */
185
    RolloverDescriptionPtr rollover( LOG4CXX_ROLLING_POLICY_ROLLOVER_FORMAL_PARAMETERS ) override;
186
187
    /**
188
     * Determines if a rollover may be appropriate at this time.  If
189
     * true is returned, RolloverPolicy.rollover will be called but it
190
     * can determine that a rollover is not warranted.
191
     *
192
     * @param appender A reference to the appender.
193
     * @param event A reference to the currently event.
194
     * @param filename The filename for the currently active log file.
195
     * @param fileLength Length of the file in bytes.
196
     * @return true if a rollover should occur.
197
     */
198
    bool isTriggeringEvent(
199
      Appender* appender,
200
      const spi::LoggingEventPtr& event,
201
      const LogString& filename,
202
      size_t fileLength) override;
203
204
    /**
205
    \copybrief RollingPolicyBase::setOption()
206
207
    Supported options | Supported values | Default value
208
    :-------------- | :----------------: | :---------------:
209
    ThrowIOExceptionOnForkFailure | True,False | True
210
211
    \sa RollingPolicyBase::setOption()
212
     */
213
    void setOption(const LogString& option, const LogString& value) override;
214
215
    /**
216
     * Was the name in shared memory set by this process?
217
     */
218
    bool isLastFileNameUnchanged();
219
220
    /**
221
     * Load the name (set by some other process) from shared memory
222
     */
223
    void loadLastFileName();
224
225
  protected:
226
    /**
227
     * A map from "d" and "date" to a date conversion formatter.
228
     *
229
     * \sa FileDatePatternConverter
230
     */
231
    LOG4CXX_NS::pattern::PatternMap getFormatSpecifiers() const override;
232
233
  private:
234
235
    /**
236
     * Generate mmap file
237
     */
238
    int createMMapFile(const std::string& lastfilename, LOG4CXX_NS::helpers::Pool& pool);
239
240
    /**
241
     *  Detect if the mmap file is empty
242
     */
243
    bool isMapFileEmpty(LOG4CXX_NS::helpers::Pool& pool);
244
245
    /**
246
     *   init MMapFile
247
     */
248
    void initMMapFile(const LogString& lastFileName, LOG4CXX_NS::helpers::Pool& pool);
249
250
    /**
251
     *   lock MMapFile
252
     */
253
    int lockMMapFile(int type);
254
255
    /**
256
     *   unlock MMapFile
257
     */
258
    int unLockMMapFile();
259
260
    /**
261
     *   create MMapFile/lockFile
262
     */
263
    const std::string createFile(const std::string& filename, const std::string& suffix, LOG4CXX_NS::helpers::Pool& pool);
264
265
};
266
267
LOG4CXX_PTR_DEF(TimeBasedRollingPolicy);
268
269
}
270
}
271
272
#endif
273