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