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