Coverage Report

Created: 2026-03-12 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/include/log4cxx/writerappender.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_WRITER_APPENDER_H
19
#define _LOG4CXX_WRITER_APPENDER_H
20
21
#include <log4cxx/appenderskeleton.h>
22
#include <log4cxx/helpers/outputstreamwriter.h>
23
#include <atomic>
24
25
namespace LOG4CXX_NS
26
{
27
28
/**
29
WriterAppender appends log events to a standard output stream
30
*/
31
class LOG4CXX_EXPORT WriterAppender : public AppenderSkeleton
32
{
33
  protected:
34
    struct WriterAppenderPriv;
35
  public:
36
    DECLARE_ABSTRACT_LOG4CXX_OBJECT(WriterAppender)
37
0
    BEGIN_LOG4CXX_CAST_MAP()
38
0
    LOG4CXX_CAST_ENTRY(WriterAppender)
39
0
    LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton)
40
0
    END_LOG4CXX_CAST_MAP()
41
42
    /**
43
    This default constructor does nothing.*/
44
    WriterAppender();
45
  protected:
46
#if LOG4CXX_ABI_VERSION <= 15
47
    WriterAppender(const LayoutPtr& layout, helpers::WriterPtr& writer);
48
    WriterAppender(const LayoutPtr& layout);
49
#else
50
    WriterAppender(const LayoutPtr& layout, const helpers::WriterPtr& writer = helpers::WriterPtr());
51
#endif
52
    WriterAppender(std::unique_ptr<WriterAppenderPriv> priv);
53
54
  public:
55
    ~WriterAppender();
56
57
    /**
58
    Derived appenders should override this method if option structure
59
    requires it.
60
    */
61
    void activateOptions(helpers::Pool& pool) override;
62
63
    /**
64
    If the <b>ImmediateFlush</b> option is set to
65
    <code>true</code>, the appender will flush at the end of each
66
    write. This is the default behavior. If the option is set to
67
    <code>false</code>, then the underlying stream can defer writing
68
    to physical medium to a later time.
69
70
    <p>Avoiding the flush operation at the end of each append results in
71
    a performance gain of 10 to 20 percent. However, there is safety
72
    tradeoff involved in skipping flushing. Indeed, when flushing is
73
    skipped, then it is likely that the last few log events will not
74
    be recorded on disk when the application exits. This is a high
75
    price to pay even for a 20% performance gain.
76
    */
77
    void setImmediateFlush(bool value);
78
    /**
79
    Returns value of the <b>ImmediateFlush</b> option.
80
    */
81
    bool getImmediateFlush() const;
82
83
    /**
84
    This method is called by the AppenderSkeleton#doAppend
85
    method.
86
87
    <p>If the output stream exists and is writable then write a log
88
    statement to the output stream. Otherwise, write a single warning
89
    message to <code>stderr</code>.
90
91
    <p>The format of the output will depend on this appender's
92
    layout.
93
94
    */
95
    void append(const spi::LoggingEventPtr& event, helpers::Pool& p) override;
96
97
98
  protected:
99
    /**
100
    This method determines if there is a sense in attempting to append.
101
102
    <p>It checks whether there is a set output target and also if
103
    there is a set layout. If these checks fail, then the boolean
104
    value <code>false</code> is returned. */
105
    virtual bool checkEntryConditions() const;
106
107
108
  public:
109
    /**
110
    Close this appender instance. The underlying stream or writer is
111
    also closed.
112
113
    <p>Closed appenders cannot be reused.
114
    */
115
    void close() override;
116
117
  protected:
118
#if LOG4CXX_ABI_VERSION <= 15
119
    /**
120
     * Close the underlying {@link log4cxx::helpers::Writer}.
121
     * */
122
    [[ deprecated( "Use WriterAppenderPriv::close" ) ]]
123
    void closeWriter();
124
#endif
125
126
    /**
127
        Returns an OutputStreamWriter when passed an OutputStream.  The
128
        encoding used will depend on the value of the
129
        <code>encoding</code> property.  If the encoding value is
130
        specified incorrectly the writer will be opened using the default
131
        system encoding (an error message will be printed to the loglog.  */
132
    virtual helpers::WriterPtr createWriter(LOG4CXX_16_CONST helpers::OutputStreamPtr& os);
133
134
  public:
135
    /**
136
    The current encoding value.
137
138
    \sa setOption
139
     */
140
    LogString getEncoding() const;
141
    /**
142
    Set the encoding to \c value.
143
144
    \sa setOption
145
     */
146
    void setEncoding(const LogString& value);
147
148
    /**
149
    \copybrief AppenderSkeleton::setOption()
150
151
    Supported options | Supported values | Default value |
152
    -------------- | ---------------- | --------------- |
153
    Encoding | C,UTF-8,UTF-16,UTF-16BE,UTF-16LE,646,US-ASCII,ISO646-US,ANSI_X3.4-1968,ISO-8859-1,ISO-LATIN-1 | UTF-8 |
154
155
    \sa AppenderSkeleton::setOption()
156
     */
157
    void setOption(const LogString& option, const LogString& value) override;
158
159
#if LOG4CXX_ABI_VERSION <= 15
160
    /**
161
      <p>Send log output to \c writer which must be open and be writable.
162
163
      <p>The helpers::Writer will be closed when the
164
      appender instance is closed.
165
166
      <b>WARNING:</b> Logging to an unopened Writer will fail.
167
168
      @param writer An already opened Writer.
169
    */
170
    [[ deprecated( "Use WriterAppenderPriv::setWriter" ) ]]
171
    void setWriter(const helpers::WriterPtr& writer);
172
173
    [[ deprecated( "Use WriterAppenderPriv::getWriter" ) ]]
174
    const helpers::WriterPtr getWriter() const;
175
#endif
176
177
    bool requiresLayout() const override;
178
179
  protected:
180
    /**
181
     Actual writing occurs here.
182
    */
183
    virtual void subAppend(const spi::LoggingEventPtr& event, helpers::Pool& p);
184
185
186
#if LOG4CXX_ABI_VERSION <= 15
187
    /**
188
    Write a footer as produced by the embedded layout's
189
    Layout#appendFooter method.  */
190
    [[ deprecated( "Specialize Layout instead of Appender" ) ]]
191
    virtual void writeFooter(helpers::Pool& p);
192
193
    /**
194
    Write a header as produced by the embedded layout's
195
    Layout#appendHeader method.  */
196
    [[ deprecated( "Specialize Layout instead of Appender" ) ]]
197
    virtual void writeHeader(helpers::Pool& p);
198
199
    /**
200
     * Set the writer.  Mutex must already be held.
201
     */
202
    [[ deprecated( "Use WriterAppenderPriv::setWriter" ) ]]
203
    void setWriterInternal(const helpers::WriterPtr& writer);
204
#endif
205
  private:
206
    //
207
    //  prevent copy and assignment
208
    WriterAppender(const WriterAppender&);
209
    WriterAppender& operator=(const WriterAppender&);
210
};
211
212
LOG4CXX_PTR_DEF(WriterAppender);
213
214
}  //namespace log4cxx
215
216
#endif //_LOG4CXX_WRITER_APPENDER_H