Coverage Report

Created: 2025-07-12 06:12

/src/logging-log4cxx/src/main/include/log4cxx/fileappender.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
#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^63.
141
     You can specify the value with the suffixes "KB", "MB" or "GB" so that the integer is
142
     interpreted being expressed respectively in kilobytes, megabytes
143
     or gigabytes. 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>BufferedIO will significatnly increase performance on heavily
153
    loaded systems.
154
155
    */
156
    bool getBufferedIO() const;
157
158
    /**
159
    Get the size of the IO buffer.
160
    */
161
    int getBufferSize() const;
162
163
    /**
164
    Get the number of seconds between file writes
165
    when the <code>BufferedIO</code> option is <code>true</code>.
166
    */
167
    int getBufferedSeconds() const;
168
169
    /**
170
    Set file open mode to \c newValue.
171
172
    The <b>Append</b> option takes a boolean value. It is set to
173
    <code>true</code> by default. If true, then <code>File</code>
174
    will be opened in append mode by #setFile (see
175
    above). Otherwise, setFile will open
176
    <code>File</code> in truncate mode.
177
178
    <p>Note: The file is opened when
179
    #activateOptions is called, not when the options are set.
180
    */
181
    void setAppend(bool newValue);
182
183
    /**
184
    Set buffered output behavior to \c newValue.
185
186
    By default buffered output is disabled and
187
    this appender writes each log message directly to the file.
188
    When buffered output is enabled,
189
    log messages are stored into a memory buffer
190
    and written to the file periodically or when the buffer is full.
191
192
    Using buffered output will significantly reduce logging overhead.
193
194
    Note: Behavior change occurs when
195
    #activateOptions is called, not when the options are set.
196
    */
197
    void setBufferedIO(bool newValue);
198
199
    /**
200
    Use \c newValue as the size of the output buffer.
201
    */
202
    void setBufferSize(int newValue);
203
204
    /**
205
    Flush the output buffer every \c newValue seconds.
206
    The default period is 5 seconds.
207
208
    Note: #activateOptions must be called after an option is changed
209
    to activate the new frequency.
210
    */
211
    void setBufferedSeconds(int newValue);
212
213
    /**
214
     *   Replaces double backslashes with single backslashes
215
     *   for compatibility with paths from earlier XML configurations files.
216
     *   @param name file name
217
     *   @return corrected file name
218
     */
219
    static LogString stripDuplicateBackslashes(const LogString& name);
220
221
  protected:
222
    void activateOptionsInternal(helpers::Pool& p);
223
224
    /**
225
    Sets and <i>opens</i> the file where the log output will
226
    go. The specified file must be writable.
227
228
    <p>If there was already an opened file, then the previous file
229
    is closed first.
230
231
    <p><b>Do not use this method directly. To configure a FileAppender
232
    or one of its subclasses, set its properties one by one and then
233
    call activateOptions.</b>
234
235
    The mutex must be locked before calling this function.
236
237
    @param file The path to the log file.
238
    @param append If true will append to fileName. Otherwise will
239
    truncate fileName.
240
    @param bufferedIO Do we do bufferedIO?
241
    @param bufferSize How big should the IO buffer be?
242
    @param p memory pool for operation.
243
    */
244
    void setFileInternal(const LogString& file, bool append,
245
      bool bufferedIO, size_t bufferSize,
246
      helpers::Pool& p);
247
248
    void setFileInternal(const LogString& file);
249
250
  private:
251
    FileAppender(const FileAppender&);
252
    FileAppender& operator=(const FileAppender&);
253
  protected:
254
    FileAppender(std::unique_ptr<FileAppenderPriv> priv);
255
256
}; // class FileAppender
257
LOG4CXX_PTR_DEF(FileAppender);
258
259
}  // namespace log4cxx
260
261
#endif