/src/logging-log4cxx/src/main/include/log4cxx/writerappender.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_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 | | /** |
119 | | * Close the underlying {@link log4cxx::helpers::Writer}. |
120 | | * */ |
121 | | void closeWriter(); |
122 | | |
123 | | /** |
124 | | Returns an OutputStreamWriter when passed an OutputStream. The |
125 | | encoding used will depend on the value of the |
126 | | <code>encoding</code> property. If the encoding value is |
127 | | specified incorrectly the writer will be opened using the default |
128 | | system encoding (an error message will be printed to the loglog. */ |
129 | | virtual helpers::WriterPtr createWriter(LOG4CXX_16_CONST helpers::OutputStreamPtr& os); |
130 | | |
131 | | public: |
132 | | /** |
133 | | The current encoding value. |
134 | | |
135 | | \sa setOption |
136 | | */ |
137 | | LogString getEncoding() const; |
138 | | /** |
139 | | Set the encoding to \c value. |
140 | | |
141 | | \sa setOption |
142 | | */ |
143 | | void setEncoding(const LogString& value); |
144 | | |
145 | | /** |
146 | | \copybrief AppenderSkeleton::setOption() |
147 | | |
148 | | Supported options | Supported values | Default value |
149 | | -------------- | ---------------- | --------------- |
150 | | 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 |
151 | | |
152 | | \sa AppenderSkeleton::setOption() |
153 | | */ |
154 | | void setOption(const LogString& option, const LogString& value) override; |
155 | | |
156 | | /** |
157 | | <p>Send log output to \c writer which must be open and be writable. |
158 | | |
159 | | <p>The helpers::Writer will be closed when the |
160 | | appender instance is closed. |
161 | | |
162 | | <b>WARNING:</b> Logging to an unopened Writer will fail. |
163 | | |
164 | | @param writer An already opened Writer. |
165 | | */ |
166 | | void setWriter(const helpers::WriterPtr& writer); |
167 | | |
168 | | const helpers::WriterPtr getWriter() const; |
169 | | |
170 | | bool requiresLayout() const override; |
171 | | |
172 | | protected: |
173 | | /** |
174 | | Actual writing occurs here. |
175 | | */ |
176 | | virtual void subAppend(const spi::LoggingEventPtr& event, helpers::Pool& p); |
177 | | |
178 | | |
179 | | /** |
180 | | Write a footer as produced by the embedded layout's |
181 | | Layout#appendFooter method. */ |
182 | | virtual void writeFooter(helpers::Pool& p); |
183 | | |
184 | | /** |
185 | | Write a header as produced by the embedded layout's |
186 | | Layout#appendHeader method. */ |
187 | | virtual void writeHeader(helpers::Pool& p); |
188 | | |
189 | | /** |
190 | | * Set the writer. Mutex must already be held. |
191 | | */ |
192 | | void setWriterInternal(const helpers::WriterPtr& writer); |
193 | | |
194 | | private: |
195 | | // |
196 | | // prevent copy and assignment |
197 | | WriterAppender(const WriterAppender&); |
198 | | WriterAppender& operator=(const WriterAppender&); |
199 | | }; |
200 | | |
201 | | LOG4CXX_PTR_DEF(WriterAppender); |
202 | | |
203 | | } //namespace log4cxx |
204 | | |
205 | | #endif //_LOG4CXX_WRITER_APPENDER_H |