Coverage Report

Created: 2026-04-12 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/include/log4cxx/fileappender.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
#ifndef _LOG4CXX_FILE_APPENDER_H
19
#define _LOG4CXX_FILE_APPENDER_H
20
21
#include <log4cxx/logger.h>
22
#include <log4cxx/logstring.h>
23
#include <log4cxx/writerappender.h>
24
#include <log4cxx/file.h>
25
#include <log4cxx/helpers/pool.h>
26
27
namespace LOG4CXX_NS
28
{
29
namespace helpers
30
{
31
class Pool;
32
}
33
34
/**
35
*  FileAppender appends log events to a file.
36
*
37
*  Uses a background thread to periodically flush the output buffer
38
*  when <code>BufferedIO</code> option is set <code>true</code>.
39
*  Use the <code>BufferedSeconds</code> option to control the frequency,
40
*  using <code>0</code> to disable the background output buffer flush.
41
*  Refer to FileAppender::setOption() for more information.
42
*
43
*/
44
class LOG4CXX_EXPORT FileAppender : public WriterAppender
45
{
46
  protected:
47
    struct FileAppenderPriv;
48
49
  public:
50
    DECLARE_LOG4CXX_OBJECT(FileAppender)
51
0
    BEGIN_LOG4CXX_CAST_MAP()
52
0
    LOG4CXX_CAST_ENTRY(FileAppender)
53
0
    LOG4CXX_CAST_ENTRY_CHAIN(WriterAppender)
54
0
    END_LOG4CXX_CAST_MAP()
55
56
    /**
57
    The default constructor does not do anything.
58
    */
59
    FileAppender();
60
61
    /**
62
    Instantiate a <code>FileAppender</code> and open the file
63
    designated by <code>filename</code>. The opened filename will
64
    become the output destination for this appender.
65
66
    <p>If the <code>append</code> parameter is true, the file will be
67
    appended to. Otherwise, the file designated by
68
    <code>filename</code> will be truncated before being opened.
69
70
    <p>If the <code>bufferedIO</code> parameter is <code>true</code>,
71
    then buffered IO will be used to write to the output file.
72
73
    */
74
    FileAppender(const LayoutPtr& layout, const LogString& filename, bool append,
75
      bool bufferedIO, int bufferSize);
76
77
    /**
78
    Instantiate a FileAppender and open the file designated by
79
    <code>filename</code>. The opened filename will become the output
80
    destination for this appender.
81
82
    <p>If the <code>append</code> parameter is true, the file will be
83
    appended to. Otherwise, the file designated by
84
    <code>filename</code> will be truncated before being opened.
85
    */
86
    FileAppender(const LayoutPtr& layout, const LogString& filename, bool append);
87
88
    /**
89
    Instantiate a FileAppender and open the file designated by
90
    <code>filename</code>. The opened filename will become the output
91
    destination for this appender.
92
93
    <p>The file will be appended to.  */
94
    FileAppender(const LayoutPtr& layout, const LogString& filename);
95
96
    ~FileAppender();
97
98
    /**
99
    The <b>File</b> property takes a string value which should be the
100
    name of the file to append to.
101
102
    <p><b>Note that the special values
103
    "System.out" or "System.err" are no longer honored.</b>
104
105
    <p>Note: Actual opening of the file is made when
106
    #activateOptions is called, not when the options are set.  */
107
    virtual void setFile(const LogString& file);
108
109
    /**
110
    Returns the value of the <b>Append</b> option.
111
    */
112
    bool getAppend() const;
113
114
    /** Returns the value of the <b>File</b> option. */
115
    LogString getFile() const;
116
117
    /**
118
    \copybrief AppenderSkeleton::activateOptions()
119
120
    Sets and <i>opens</i> the file where the log output will
121
    go. The specified file must be writable.
122
123
    If there was already an opened file, then the previous file
124
    is closed first.
125
    */
126
    void activateOptions(helpers::Pool& p) override;
127
128
    /**
129
    \copybrief AppenderSkeleton::setOption()
130
131
    Supported options | Supported values | Default value |
132
    :-------------- | :----------------: | :---------------: |
133
    FileName | {any} | - |
134
    Append | True,False | True |
135
    BufferedIO | True,False | False |
136
    BufferedSeconds | {any} | 5 |
137
    ImmediateFlush | True,False | False |
138
    BufferSize | (\ref fileSz1 "1") | 8 KB |
139
140
    \anchor fileSz1 (1) An integer in the range 0 - 2^32.
141
     You can specify the value with the suffixes "KB" or "MB" so that the integer is
142
     interpreted being expressed respectively in kilobytes or megabytes.
143
     For example, the value "10KB" will be interpreted as 10240.
144
145
    \sa AppenderSkeleton::setOption()
146
    */
147
    void setOption(const LogString& option, const LogString& value) override;
148
149
    /**
150
    Get the value of the <b>BufferedIO</b> option.
151
152
    <p>Using buffered output will significantly reduce logging overhead.
153
154
    */
155
    bool getBufferedIO() const;
156
157
    /**
158
    Get the size of the IO buffer.
159
    */
160
    int getBufferSize() const;
161
162
    /**
163
    Get the number of seconds between file writes
164
    when the <code>BufferedIO</code> option is <code>true</code>.
165
    */
166
    int getBufferedSeconds() const;
167
168
    /**
169
    Set file open mode to \c newValue.
170
171
    The <b>Append</b> option takes a boolean value. It is set to
172
    <code>true</code> by default. If true, then <code>File</code>
173
    will be opened in append mode by #setFile (see
174
    above). Otherwise, setFile will open
175
    <code>File</code> in truncate mode.
176
177
    <p>Note: The file is opened when
178
    #activateOptions is called, not when the options are set.
179
    */
180
    void setAppend(bool newValue);
181
182
    /**
183
    Set buffered output behavior to \c newValue.
184
185
    By default buffered output is disabled and
186
    this appender writes each log message directly to the file.
187
    When buffered output is enabled,
188
    log messages are stored into a memory buffer
189
    and written to the file periodically or when the buffer is full.
190
191
    Using buffered output will significantly reduce logging overhead.
192
193
    Note: Behavior change occurs when
194
    #activateOptions is called, not when the options are set.
195
    */
196
    void setBufferedIO(bool newValue);
197
198
    /**
199
    Use \c newValue as the size of the output buffer.
200
    */
201
    void setBufferSize(int newValue);
202
203
    /**
204
    Flush the output buffer every \c newValue seconds.
205
    The default period is 5 seconds.
206
207
    Note: #activateOptions must be called after an option is changed
208
    to activate the new frequency.
209
    */
210
    void setBufferedSeconds(int newValue);
211
212
    /**
213
     *   Replaces double backslashes with single backslashes
214
     *   for compatibility with paths from earlier XML configurations files.
215
     *   @param name file name
216
     *   @return corrected file name
217
     */
218
    static LogString stripDuplicateBackslashes(const LogString& name);
219
220
  protected:
221
    void activateOptionsInternal(helpers::Pool& p);
222
223
    /**
224
    Sets and <i>opens</i> the file where the log output will
225
    go. The specified file must be writable.
226
227
    <p>If there was already an opened file, then the previous file
228
    is closed first.
229
230
    <p><b>Do not use this method directly. To configure a FileAppender
231
    or one of its subclasses, set its properties one by one and then
232
    call activateOptions.</b>
233
234
    The mutex must be locked before calling this function.
235
236
    @param file The path to the log file.
237
    @param append If true will append to fileName. Otherwise will
238
    truncate fileName.
239
    @param bufferedIO Do we do bufferedIO?
240
    @param bufferSize How big should the IO buffer be?
241
    @param p memory pool for operation.
242
    */
243
    void setFileInternal(const LogString& file, bool append,
244
      bool bufferedIO, size_t bufferSize,
245
      helpers::Pool& p);
246
247
    void setFileInternal(const LogString& file);
248
249
  private:
250
    FileAppender(const FileAppender&);
251
    FileAppender& operator=(const FileAppender&);
252
  protected:
253
    FileAppender(std::unique_ptr<FileAppenderPriv> priv);
254
255
}; // class FileAppender
256
LOG4CXX_PTR_DEF(FileAppender);
257
258
}  // namespace log4cxx
259
260
#endif