Coverage Report

Created: 2026-05-30 06:57

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
    using AppenderSkeleton::activateOptions;
58
    /**
59
    Derived appenders should override this method if option structure
60
    requires it.
61
    */
62
    void activateOptions( LOG4CXX_ACTIVATE_OPTIONS_FORMAL_PARAMETERS  ) override;
63
64
    /**
65
    If the <b>ImmediateFlush</b> option is set to
66
    <code>true</code>, the appender will flush at the end of each
67
    write. This is the default behavior. If the option is set to
68
    <code>false</code>, then the underlying stream can defer writing
69
    to physical medium to a later time.
70
71
    <p>Avoiding the flush operation at the end of each append results in
72
    a performance gain of 10 to 20 percent. However, there is safety
73
    tradeoff involved in skipping flushing. Indeed, when flushing is
74
    skipped, then it is likely that the last few log events will not
75
    be recorded on disk when the application exits. This is a high
76
    price to pay even for a 20% performance gain.
77
    */
78
    void setImmediateFlush(bool value);
79
    /**
80
    Returns value of the <b>ImmediateFlush</b> option.
81
    */
82
    bool getImmediateFlush() const;
83
84
    /**
85
    This method is called by the AppenderSkeleton#doAppend
86
    method.
87
88
    <p>If the output stream exists and is writable then write a log
89
    statement to the output stream. Otherwise, write a single warning
90
    message to <code>stderr</code>.
91
92
    <p>The format of the output will depend on this appender's
93
    layout.
94
95
    */
96
    void append( LOG4CXX_APPEND_FORMAL_PARAMETERS ) override;
97
98
99
  protected:
100
    /**
101
    This method determines if there is a sense in attempting to append.
102
103
    <p>It checks whether there is a set output target and also if
104
    there is a set layout. If these checks fail, then the boolean
105
    value <code>false</code> is returned. */
106
    virtual bool checkEntryConditions() const;
107
108
109
  public:
110
    /**
111
    Close this appender instance. The underlying stream or writer is
112
    also closed.
113
114
    <p>Closed appenders cannot be reused.
115
    */
116
    void close() override;
117
118
  protected:
119
#if LOG4CXX_ABI_VERSION <= 15
120
    /**
121
     * Close the underlying {@link log4cxx::helpers::Writer}.
122
     * */
123
    [[ deprecated( "Use WriterAppenderPriv::close" ) ]]
124
    void closeWriter();
125
#endif
126
127
    /**
128
        Returns an OutputStreamWriter when passed an OutputStream.  The
129
        encoding used will depend on the value of the
130
        <code>encoding</code> property.  If the encoding value is
131
        specified incorrectly the writer will be opened using the default
132
        system encoding (an error message will be printed to the loglog.  */
133
    virtual helpers::WriterPtr createWriter(LOG4CXX_16_CONST helpers::OutputStreamPtr& os);
134
135
  public:
136
    /**
137
    The current encoding value.
138
139
    \sa setOption
140
     */
141
    LogString getEncoding() const;
142
    /**
143
    Set the encoding to \c value.
144
145
    \sa setOption
146
     */
147
    void setEncoding(const LogString& value);
148
149
    /**
150
    \copybrief AppenderSkeleton::setOption()
151
152
    Supported options | Supported values | Default value |
153
    -------------- | ---------------- | --------------- |
154
    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 |
155
156
    \sa AppenderSkeleton::setOption()
157
     */
158
    void setOption(const LogString& option, const LogString& value) override;
159
160
#if LOG4CXX_ABI_VERSION <= 15
161
    /**
162
      <p>Send log output to \c writer which must be open and be writable.
163
164
      <p>The helpers::Writer will be closed when the
165
      appender instance is closed.
166
167
      <b>WARNING:</b> Logging to an unopened Writer will fail.
168
169
      @param writer An already opened Writer.
170
    */
171
    [[ deprecated( "Use WriterAppenderPriv::setWriter" ) ]]
172
    void setWriter(const helpers::WriterPtr& writer);
173
174
    [[ deprecated( "Use WriterAppenderPriv::getWriter" ) ]]
175
    const helpers::WriterPtr getWriter() const;
176
#endif
177
178
    bool requiresLayout() const override;
179
180
  protected:
181
    /**
182
     Actual writing occurs here.
183
    */
184
    virtual void subAppend( LOG4CXX_APPEND_FORMAL_PARAMETERS );
185
186
187
#if LOG4CXX_ABI_VERSION <= 15
188
    /**
189
    Write a footer as produced by the embedded layout's
190
    Layout#appendFooter method.  */
191
    [[ deprecated( "Specialize Layout instead of Appender" ) ]]
192
    virtual void writeFooter(helpers::Pool& p);
193
194
    /**
195
    Write a header as produced by the embedded layout's
196
    Layout#appendHeader method.  */
197
    [[ deprecated( "Specialize Layout instead of Appender" ) ]]
198
    virtual void writeHeader(helpers::Pool& p);
199
200
    /**
201
     * Set the writer.  Mutex must already be held.
202
     */
203
    [[ deprecated( "Use WriterAppenderPriv::setWriter" ) ]]
204
    void setWriterInternal(const helpers::WriterPtr& writer);
205
#endif
206
  private:
207
    //
208
    //  prevent copy and assignment
209
    WriterAppender(const WriterAppender&);
210
    WriterAppender& operator=(const WriterAppender&);
211
};
212
213
LOG4CXX_PTR_DEF(WriterAppender);
214
215
}  //namespace log4cxx
216
217
#endif //_LOG4CXX_WRITER_APPENDER_H