Coverage Report

Created: 2025-10-13 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/include/log4cxx/asyncappender.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_ASYNC_APPENDER_H
19
#define _LOG4CXX_ASYNC_APPENDER_H
20
21
#include <log4cxx/appenderskeleton.h>
22
#include <log4cxx/helpers/appenderattachableimpl.h>
23
#include <log4cxx/spi/loggingevent.h>
24
25
namespace LOG4CXX_NS
26
{
27
LOG4CXX_LIST_DEF(LoggingEventList, spi::LoggingEventPtr);
28
29
/**
30
The AsyncAppender decouples logging event creation from output
31
by processing log events asynchronously.
32
33
The AsyncAppender stores the logging event in a bounded buffer
34
and then returns control to the application.
35
A separate thread forwards events to the attached appender(s).
36
37
An AsyncAppender is used when you configure a logger to be asynchronous.
38
These AsyncAppender(s) use [the default values](@ref log4cxx::AsyncAppender::setOption) for all options
39
and they cannot be changed using configuration file entries.
40
For more control over the AsyncAppender options,
41
use <b>appender-ref</b> element in the logger configuration instead. 
42
43
<b>Important notes:</b>
44
- Your application must call LogManager::shutdown when it exits
45
to prevent undefined behaviour when using this appender.
46
- Runtime configuration of options requires an XML configuration file
47
(see the example below).
48
49
This appender is useful when outputting to a slow event sink,
50
for example, unbuffered output to a file,
51
a remote SMTP server or a database.
52
Note that configuring a FileAppender to use [buffered output](@ref log4cxx::FileAppender::setOption)
53
usually results in lower overhead than
54
attaching the FileAppender to an AsyncAppender
55
as the inter-thread communication overhead
56
can exceed the time to add a message to a buffer.
57
58
You can attach multiple appenders to an AsyncAppender by:
59
- calling AsyncAppender::addAppender repeatedly when progammatically configuring Log4cxx.
60
- adding multiple <b>appender-ref</b> elements
61
to the <b>appender class="AsyncAppender"</b> element
62
when using runtime configuration.
63
64
Here is a sample configuration file:
65
\include async-example.xml
66
67
### Configurable properties
68
69
\anchor BlockingProperty When the application produces logging events faster
70
than the background thread is able to process,
71
the bounded buffer can become full.
72
In this situation AsyncAppender will either
73
block until the bounded buffer has a free slot or
74
discard the event.
75
The [Blocking property](@ref AsyncAppender::setOption) controls which behaviour is used.
76
When events are discarded,
77
the logged output will indicate this
78
with a log message prefixed with <i>Discarded</i>.
79
The output may contain one <i>Discarded</i> message per logger name,
80
the logging event of the highest level for each logger
81
whose events have been discarded.
82
83
To determine whether the application produces logging events faster
84
than the background thread is able to process, enable [Log4cxx internal debugging](internal-debugging.html).
85
The AsyncAppender will output a histogram of queue length frequencies when closed.
86
87
See AsyncAppender::setOption for details.
88
89
*/
90
class LOG4CXX_EXPORT AsyncAppender :
91
  public virtual spi::AppenderAttachable,
92
  public virtual AppenderSkeleton
93
{
94
  protected:
95
    struct AsyncAppenderPriv;
96
97
  public:
98
    DECLARE_LOG4CXX_OBJECT(AsyncAppender)
99
0
    BEGIN_LOG4CXX_CAST_MAP()
100
0
    LOG4CXX_CAST_ENTRY(AsyncAppender)
101
0
    LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton)
102
0
    LOG4CXX_CAST_ENTRY(spi::AppenderAttachable)
103
0
    END_LOG4CXX_CAST_MAP()
104
105
    /**
106
     * Create new instance.
107
    */
108
    AsyncAppender();
109
110
    /**
111
     *  If not closed, calls AsyncAppender::close.
112
     */
113
    virtual ~AsyncAppender();
114
115
    /**
116
     * Ensure \c newAppender receives any logging event
117
     * added to this appender.
118
     *
119
     * @param newAppender appender to add, may not be null.
120
    */
121
    void addAppender(const AppenderPtr newAppender) override;
122
123
    /**
124
    * Call AppenderSkeleton#doAppendImpl without acquiring a lock.
125
    */
126
    void doAppend(const spi::LoggingEventPtr& event,
127
      helpers::Pool& pool1) override;
128
129
    /**
130
    * Add \c event to a ring buffer.
131
    * The behaviour when the ring buffer is full
132
    * is controlled by the [Blocking property](@ref BlockingProperty) value.
133
    */
134
    void append(const spi::LoggingEventPtr& event, helpers::Pool& p) override;
135
136
    /**
137
    Close this <code>AsyncAppender</code> by interrupting the
138
    dispatcher thread which will process all pending events before
139
    exiting.
140
    */
141
    void close() override;
142
143
    /**
144
     * Get iterator over attached appenders.
145
     * @return list of all attached appenders.
146
    */
147
    AppenderList getAllAppenders() const override;
148
149
    /**
150
     * Get appender by name.
151
     *
152
     * @param name name, may not be null.
153
     * @return matching appender or null.
154
    */
155
    AppenderPtr getAppender(const LogString& name) const override;
156
157
    /**
158
     * The current value of the (unused) <b>LocationInfo</b> option.
159
    */
160
    bool getLocationInfo() const;
161
    /**
162
    * Determines if specified appender is attached.
163
    * @param appender appender.
164
    * @return true if attached.
165
    */
166
    bool isAttached(const AppenderPtr appender) const override;
167
168
    /** Return false
169
    */
170
    bool requiresLayout() const override;
171
172
    /**
173
     * Removes and closes all attached appenders.
174
    */
175
    void removeAllAppenders() override;
176
177
    /**
178
     * Removes an appender.
179
     * @param appender appender to remove.
180
    */
181
    void removeAppender(const AppenderPtr appender) override;
182
    /**
183
    * Remove appender by name.
184
    * @param name name.
185
    */
186
    void removeAppender(const LogString& name) override;
187
188
    /**
189
     * Replace \c oldAppender  with \c newAppender.
190
     * @return true if oldAppender was replaced with newAppender.
191
     */
192
    bool replaceAppender(const AppenderPtr& oldAppender, const AppenderPtr& newAppender) LOG4CXX_16_VIRTUAL_SPECIFIER;
193
194
    /**
195
     * Replace any previously added appenders with \c newList.
196
     */
197
    void replaceAppenders(const AppenderList& newList) LOG4CXX_16_VIRTUAL_SPECIFIER;
198
199
    /**
200
    * The <b>LocationInfo</b> attribute is provided for compatibility
201
    * with log4j and has no effect on the log output.
202
    * @param flag new value.
203
    */
204
    void setLocationInfo(bool flag);
205
206
    /**
207
    * The <b>BufferSize</b> option takes a non-negative integer value.
208
    * This integer value determines the maximum size of the bounded
209
    * buffer.
210
    * */
211
    void setBufferSize(int size);
212
213
    /**
214
     * Gets the current buffer size.
215
     * @return the current value of the <b>BufferSize</b> option.
216
    */
217
    int getBufferSize() const;
218
219
    /**
220
     * Sets whether appender should wait if there is no
221
     * space available in the event buffer or immediately return.
222
     *
223
     * @param value true if appender should wait until available space in buffer.
224
     */
225
    void setBlocking(bool value);
226
227
    /**
228
     * Gets whether appender should block calling thread when buffer is full.
229
     * If false, messages will be counted by logger and a summary
230
     * message appended after the contents of the buffer have been appended.
231
     *
232
     * @return true if calling thread will be blocked when buffer is full.
233
     */
234
    bool getBlocking() const;
235
236
237
    /**
238
    \copybrief AppenderSkeleton::setOption()
239
240
    Supported options | Supported values | Default value |
241
    -------------- | ---------------- | --------------- |
242
    BufferSize | int  | 128 |
243
    Blocking | True,False | True |
244
245
    \sa AppenderSkeleton::setOption()
246
     */
247
    void setOption(const LogString& option, const LogString& value) override;
248
249
250
  private:
251
    AsyncAppender(const AsyncAppender&);
252
    AsyncAppender& operator=(const AsyncAppender&);
253
254
    /**
255
     *  Dispatch routine.
256
     */
257
    void dispatch();
258
259
}; // class AsyncAppender
260
LOG4CXX_PTR_DEF(AsyncAppender);
261
}  //  namespace log4cxx
262
263
#endif//  _LOG4CXX_ASYNC_APPENDER_H
264