Coverage Report

Created: 2026-06-15 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/include/log4cxx/rolling/rollingfileappender.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
#if !defined(_LOG4CXX_ROLLING_ROLLING_FILE_APPENDER_H)
19
#define _LOG4CXX_ROLLING_ROLLING_FILE_APPENDER_H
20
21
#include <log4cxx/fileappender.h>
22
#include <log4cxx/spi/optionhandler.h>
23
#include <log4cxx/fileappender.h>
24
#include <log4cxx/rolling/triggeringpolicy.h>
25
#include <log4cxx/rolling/rollingpolicy.h>
26
#include <log4cxx/rolling/action.h>
27
28
namespace LOG4CXX_NS
29
{
30
namespace rolling
31
{
32
33
34
/**
35
 * <code>RollingFileAppender</code> extends {@link log4cxx::FileAppender} to backup the log files
36
 * depending on {@link log4cxx::rolling::RollingPolicy RollingPolicy} and {@link log4cxx::rolling::TriggeringPolicy TriggeringPolicy}.
37
 * <p>
38
 * To be of any use, a <code>RollingFileAppender</code> instance must have both
39
 * a <code>RollingPolicy</code> and a <code>TriggeringPolicy</code> set up.
40
 * However, if its <code>RollingPolicy</code> also implements the
41
 * <code>TriggeringPolicy</code> interface, then only the former needs to be
42
 * set up. For example, {@link log4cxx::rolling::TimeBasedRollingPolicy TimeBasedRollingPolicy} acts both as a
43
 * <code>RollingPolicy</code> and a <code>TriggeringPolicy</code>.
44
 *
45
 * <p><code>RollingFileAppender</code> can be configured programattically or
46
 * using {@link log4cxx::xml::DOMConfigurator}.
47
 * The following is a sample configuration file.
48
49
<pre>&lt;?xml version="1.0" encoding="UTF-8" ?>
50
&lt;!DOCTYPE log4j:configuration>
51
  &lt;log4j:configuration debug="true">
52
  &lt;appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
53
    <b>&lt;rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
54
      &lt;param name="FileNamePattern" value="/wombat/foo.%d{yyyy-MM}.gz"/>
55
    &lt;/rollingPolicy></b>
56
    &lt;layout class="org.apache.log4j.PatternLayout">
57
      &lt;param name="ConversionPattern" value="%c{1} - %m%n"/>
58
    &lt;/layout>
59
  &lt;/appender>
60
61
  &lt;root>
62
    &lt;appender-ref ref="ROLL"/>
63
  &lt;/root>
64
&lt;/log4j:configuration>
65
</pre>
66
67
 *<p>This configuration file specifies a monthly rollover schedule including
68
 * automatic compression of the archived files. See
69
 * {@link TimeBasedRollingPolicy} for more details.
70
 *
71
 *
72
 * Note: Do *not* set the option <code>Append</code> to <code>false</code>.
73
 * Rolling over files is only relevant when you are appending.
74
 *
75
 */
76
class LOG4CXX_EXPORT RollingFileAppender : public FileAppender
77
{
78
    DECLARE_LOG4CXX_OBJECT(RollingFileAppender)
79
0
    BEGIN_LOG4CXX_CAST_MAP()
80
0
    LOG4CXX_CAST_ENTRY(RollingFileAppender)
81
0
    LOG4CXX_CAST_ENTRY_CHAIN(FileAppender)
82
0
    END_LOG4CXX_CAST_MAP()
83
  protected:
84
    struct RollingFileAppenderPriv;
85
86
  public:
87
    RollingFileAppender();
88
    RollingFileAppender( std::unique_ptr<RollingFileAppenderPriv> priv );
89
90
    /** Returns the value of the <b>MaxBackupIndex</b> option. */
91
    int getMaxBackupIndex() const;
92
93
    /** Get the maximum size that the output file is allowed to reach before being rolled over to backup files. */
94
    size_t getMaximumFileSize() const;
95
96
97
    /**
98
    Set the maximum number of backup files to keep around.
99
100
    <p>The <b>MaxBackupIndex</b> option determines how many backup
101
     files are kept before the oldest is erased. This option takes
102
     a positive integer value. If set to zero, then there will be no
103
     backup files and the log file will be truncated when it reaches <code>MaxFileSize</code>.
104
    */
105
    void setMaxBackupIndex( int maxBackupIndex );
106
107
    /**
108
    Set the maximum size that the output file is allowed to reach before being rolled over to backup files.
109
110
    <p>In configuration files, the <b>MaxFileSize</b> option takes an
111
     long integer in the range 0 - 2^63. You can specify the value with the suffixes "KB", "MB" or "GB" so that the integer is
112
     interpreted being expressed respectively in kilobytes, megabytes
113
     or gigabytes. For example, the value "10KB" will be interpreted as 10240.
114
    */
115
    void setMaxFileSize( const LogString& value );
116
117
    void setMaximumFileSize( size_t value );
118
119
    /**
120
       The <b>DatePattern</b> takes a string in the same format as
121
       expected by {@link log4cxx::helpers::SimpleDateFormat SimpleDateFormat}. This options determines the
122
       rollover schedule.
123
     */
124
    void setDatePattern(const LogString& pattern);
125
126
    LogString makeFileNamePattern(const LogString& datePattern);
127
128
    /**
129
    \copybrief FileAppender::setOption()
130
131
    Supported options | Supported values | Default value |
132
    :-------------- | :----------------: | :---------------: |
133
    FileDatePattern | (\ref dateChars "1") | - |
134
    MaxBackupIndex | 1-12 | 0 |
135
    MaxFileSize | (\ref fileSz "2") | 10 MB |
136
137
    \anchor dateChars (1) A pattern compatible with
138
      java.text.SimpleDateFormat, "ABSOLUTE", "DATE" or "ISO8601".
139
      For example, "HH:mm:ss,SSS", "dd MMM yyyy HH:mm:ss,SSS" or "DATE".
140
141
    \anchor fileSz (2) An integer in the range 0 - 2^63.
142
     You can specify the value with the suffixes "KB", "MB" or "GB" so that the integer is
143
     interpreted being expressed respectively in kilobytes, megabytes
144
     or gigabytes. For example, the value "10KB" will be interpreted as 10240.
145
146
    \sa FileAppender::setOption()
147
    */
148
    void setOption( const LogString& option, const LogString& value ) override;
149
150
    using spi::OptionHandler::activateOptions;
151
    /**
152
    \copybrief FileAppender::activateOptions()
153
154
    \sa FileAppender::activateOptions()
155
    */
156
    void activateOptions( LOG4CXX_ACTIVATE_OPTIONS_FORMAL_PARAMETERS ) override;
157
158
    /**
159
       Implements the configured roll over behaviour.
160
161
       <p>If <code>MaxBackupIndex</code> is positive, then files
162
       {<code>File.1</code>, ..., <code>File.MaxBackupIndex -1</code>}
163
       are renamed to {<code>File.2</code>, ...,
164
       <code>File.MaxBackupIndex</code>}. Moreover, <code>File</code> is
165
       renamed <code>File.1</code> and closed. A new <code>File</code> is
166
       created to receive further log output.
167
168
       <p>If <code>MaxBackupIndex</code> is equal to zero, then the
169
       <code>File</code> is truncated with no backup files created.
170
171
     */
172
    bool rollover();
173
#if LOG4CXX_ABI_VERSION <= 15
174
    bool rollover(LOG4CXX_NS::helpers::Pool& p);
175
#endif
176
177
  protected:
178
179
    /**
180
     Actual writing occurs here.
181
    */
182
    void subAppend( LOG4CXX_APPEND_FORMAL_PARAMETERS ) override;
183
184
    bool rolloverInternal();
185
#if LOG4CXX_ABI_VERSION <= 15
186
    bool rolloverInternal(LOG4CXX_NS::helpers::Pool& p);
187
#endif
188
189
  public:
190
    /**
191
     * The policy that implements the scheme for rolling over a log file.
192
     */
193
    RollingPolicyPtr getRollingPolicy() const;
194
195
    /**
196
     * The policy that determine when to trigger a log file rollover.
197
     */
198
    TriggeringPolicyPtr getTriggeringPolicy() const;
199
200
    /**
201
     * Use \c policy as the scheme for rolling over log files.
202
     *
203
     * Where the \c policy also implements
204
     * {@link TriggeringPolicy}, then \c policy
205
     * will be used to determine when to trigger a log file rollover.
206
     */
207
    void setRollingPolicy(const RollingPolicyPtr& policy);
208
209
    /**
210
     * Use \c policy to determine when to trigger a log file rollover.
211
     */
212
    void setTriggeringPolicy(const TriggeringPolicyPtr& policy);
213
214
  public:
215
    /**
216
      * Close appender.  Waits for any asynchronous file compression actions to be completed.
217
    */
218
    void close() override;
219
220
  protected:
221
    /**
222
       Returns an OutputStreamWriter when passed an OutputStream.  The
223
       encoding used will depend on the value of the
224
       <code>encoding</code> property.  If the encoding value is
225
       specified incorrectly the writer will be opened using the default
226
       system encoding (an error message will be printed to the loglog.
227
     @param os output stream, may not be null.
228
     @return new writer.
229
     */
230
    helpers::WriterPtr createWriter(LOG4CXX_16_CONST helpers::OutputStreamPtr& os) override;
231
232
  public:
233
    /**
234
     * Get byte length of current active log file.
235
     * @return byte length of current active log file.
236
     */
237
    size_t getFileLength() const;
238
239
    /**
240
     * Increments estimated byte length of current active log file.
241
     * @param increment additional bytes written to log file.
242
     */
243
    void incrementFileLength(size_t increment);
244
245
};
246
247
LOG4CXX_PTR_DEF(RollingFileAppender);
248
249
}
250
}
251
252
#endif
253