[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_ASYNC_APPENDER_H
|
---|
| 19 | #define _LOG4CXX_ASYNC_APPENDER_H
|
---|
| 20 |
|
---|
| 21 | #if defined(_MSC_VER)
|
---|
| 22 | #pragma warning ( push )
|
---|
| 23 | #pragma warning ( disable: 4231 4251 4275 4786 )
|
---|
| 24 | #endif
|
---|
| 25 |
|
---|
| 26 |
|
---|
| 27 | #include <log4cxx/appenderskeleton.h>
|
---|
| 28 | #include <log4cxx/helpers/appenderattachableimpl.h>
|
---|
| 29 | #include <deque>
|
---|
| 30 | #include <log4cxx/spi/loggingevent.h>
|
---|
| 31 | #include <log4cxx/helpers/thread.h>
|
---|
| 32 | #include <log4cxx/helpers/mutex.h>
|
---|
| 33 | #include <log4cxx/helpers/condition.h>
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | namespace log4cxx
|
---|
| 37 | {
|
---|
| 38 | LOG4CXX_LIST_DEF(LoggingEventList, log4cxx::spi::LoggingEventPtr);
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | The AsyncAppender lets users log events asynchronously. It uses a
|
---|
| 42 | bounded buffer to store logging events.
|
---|
| 43 |
|
---|
| 44 | <p>The AsyncAppender will collect the events sent to it and then
|
---|
| 45 | dispatch them to all the appenders that are attached to it. You can
|
---|
| 46 | attach multiple appenders to an AsyncAppender.
|
---|
| 47 |
|
---|
| 48 | <p>The AsyncAppender uses a separate thread to serve the events in
|
---|
| 49 | its bounded buffer.
|
---|
| 50 |
|
---|
| 51 | <p><b>Important note:</b> The <code>AsyncAppender</code> can only
|
---|
| 52 | be script configured using the {@link xml::DOMConfigurator DOMConfigurator}.
|
---|
| 53 | */
|
---|
| 54 | class LOG4CXX_EXPORT AsyncAppender :
|
---|
| 55 | public virtual spi::AppenderAttachable,
|
---|
| 56 | public virtual AppenderSkeleton
|
---|
| 57 | {
|
---|
| 58 | public:
|
---|
| 59 | DECLARE_LOG4CXX_OBJECT(AsyncAppender)
|
---|
| 60 | BEGIN_LOG4CXX_CAST_MAP()
|
---|
| 61 | LOG4CXX_CAST_ENTRY(AsyncAppender)
|
---|
| 62 | LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton)
|
---|
| 63 | LOG4CXX_CAST_ENTRY(spi::AppenderAttachable)
|
---|
| 64 | END_LOG4CXX_CAST_MAP()
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * Create new instance.
|
---|
| 68 | */
|
---|
| 69 | AsyncAppender();
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
| 72 | * Destructor.
|
---|
| 73 | */
|
---|
| 74 | virtual ~AsyncAppender();
|
---|
| 75 |
|
---|
| 76 | void addRef() const;
|
---|
| 77 | void releaseRef() const;
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * Add appender.
|
---|
| 81 | *
|
---|
| 82 | * @param newAppender appender to add, may not be null.
|
---|
| 83 | */
|
---|
| 84 | void addAppender(const AppenderPtr& newAppender);
|
---|
| 85 |
|
---|
| 86 | void append(const spi::LoggingEventPtr& event, log4cxx::helpers::Pool& p);
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | Close this <code>AsyncAppender</code> by interrupting the
|
---|
| 90 | dispatcher thread which will process all pending events before
|
---|
| 91 | exiting.
|
---|
| 92 | */
|
---|
| 93 | void close();
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
| 96 | * Get iterator over attached appenders.
|
---|
| 97 | * @return list of all attached appenders.
|
---|
| 98 | */
|
---|
| 99 | AppenderList getAllAppenders() const;
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * Get appender by name.
|
---|
| 103 | *
|
---|
| 104 | * @param name name, may not be null.
|
---|
| 105 | * @return matching appender or null.
|
---|
| 106 | */
|
---|
| 107 | AppenderPtr getAppender(const LogString& name) const;
|
---|
| 108 |
|
---|
| 109 | /**
|
---|
| 110 | * Gets whether the location of the logging request call
|
---|
| 111 | * should be captured.
|
---|
| 112 | *
|
---|
| 113 | * @return the current value of the <b>LocationInfo</b> option.
|
---|
| 114 | */
|
---|
| 115 | bool getLocationInfo() const;
|
---|
| 116 | /**
|
---|
| 117 | * Determines if specified appender is attached.
|
---|
| 118 | * @param appender appender.
|
---|
| 119 | * @return true if attached.
|
---|
| 120 | */
|
---|
| 121 | bool isAttached(const AppenderPtr& appender) const;
|
---|
| 122 |
|
---|
| 123 | virtual bool requiresLayout() const;
|
---|
| 124 |
|
---|
| 125 | /**
|
---|
| 126 | * Removes and closes all attached appenders.
|
---|
| 127 | */
|
---|
| 128 | void removeAllAppenders();
|
---|
| 129 |
|
---|
| 130 | /**
|
---|
| 131 | * Removes an appender.
|
---|
| 132 | * @param appender appender to remove.
|
---|
| 133 | */
|
---|
| 134 | void removeAppender(const AppenderPtr& appender);
|
---|
| 135 | /**
|
---|
| 136 | * Remove appender by name.
|
---|
| 137 | * @param name name.
|
---|
| 138 | */
|
---|
| 139 | void removeAppender(const LogString& name);
|
---|
| 140 |
|
---|
| 141 | /**
|
---|
| 142 | * The <b>LocationInfo</b> attribute is provided for compatibility
|
---|
| 143 | * with log4j and has no effect on the log output.
|
---|
| 144 | * @param flag new value.
|
---|
| 145 | */
|
---|
| 146 | void setLocationInfo(bool flag);
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * The <b>BufferSize</b> option takes a non-negative integer value.
|
---|
| 150 | * This integer value determines the maximum size of the bounded
|
---|
| 151 | * buffer.
|
---|
| 152 | * */
|
---|
| 153 | void setBufferSize(int size);
|
---|
| 154 |
|
---|
| 155 | /**
|
---|
| 156 | * Gets the current buffer size.
|
---|
| 157 | * @return the current value of the <b>BufferSize</b> option.
|
---|
| 158 | */
|
---|
| 159 | int getBufferSize() const;
|
---|
| 160 |
|
---|
| 161 | /**
|
---|
| 162 | * Sets whether appender should wait if there is no
|
---|
| 163 | * space available in the event buffer or immediately return.
|
---|
| 164 | *
|
---|
| 165 | * @param value true if appender should wait until available space in buffer.
|
---|
| 166 | */
|
---|
| 167 | void setBlocking(bool value);
|
---|
| 168 |
|
---|
| 169 | /**
|
---|
| 170 | * Gets whether appender should block calling thread when buffer is full.
|
---|
| 171 | * If false, messages will be counted by logger and a summary
|
---|
| 172 | * message appended after the contents of the buffer have been appended.
|
---|
| 173 | *
|
---|
| 174 | * @return true if calling thread will be blocked when buffer is full.
|
---|
| 175 | */
|
---|
| 176 | bool getBlocking() const;
|
---|
| 177 |
|
---|
| 178 |
|
---|
| 179 | /**
|
---|
| 180 | * Set appender properties by name.
|
---|
| 181 | * @param option property name.
|
---|
| 182 | * @param value property value.
|
---|
| 183 | */
|
---|
| 184 | void setOption(const LogString& option, const LogString& value);
|
---|
| 185 |
|
---|
| 186 |
|
---|
| 187 | private:
|
---|
| 188 | AsyncAppender(const AsyncAppender&);
|
---|
| 189 | AsyncAppender& operator=(const AsyncAppender&);
|
---|
| 190 | /**
|
---|
| 191 | * The default buffer size is set to 128 events.
|
---|
| 192 | */
|
---|
| 193 | enum { DEFAULT_BUFFER_SIZE = 128 };
|
---|
| 194 |
|
---|
| 195 | /**
|
---|
| 196 | * Event buffer.
|
---|
| 197 | */
|
---|
| 198 | LoggingEventList buffer;
|
---|
| 199 |
|
---|
| 200 | /**
|
---|
| 201 | * Mutex used to guard access to buffer and discardMap.
|
---|
| 202 | */
|
---|
| 203 | ::log4cxx::helpers::Mutex bufferMutex;
|
---|
| 204 | ::log4cxx::helpers::Condition bufferNotFull;
|
---|
| 205 | ::log4cxx::helpers::Condition bufferNotEmpty;
|
---|
| 206 |
|
---|
| 207 | class DiscardSummary {
|
---|
| 208 | private:
|
---|
| 209 | /**
|
---|
| 210 | * First event of the highest severity.
|
---|
| 211 | */
|
---|
| 212 | ::log4cxx::spi::LoggingEventPtr maxEvent;
|
---|
| 213 |
|
---|
| 214 | /**
|
---|
| 215 | * Total count of messages discarded.
|
---|
| 216 | */
|
---|
| 217 | int count;
|
---|
| 218 |
|
---|
| 219 | public:
|
---|
| 220 | /**
|
---|
| 221 | * Create new instance.
|
---|
| 222 | *
|
---|
| 223 | * @param event event, may not be null.
|
---|
| 224 | */
|
---|
| 225 | DiscardSummary(const ::log4cxx::spi::LoggingEventPtr& event);
|
---|
| 226 | /** Copy constructor. */
|
---|
| 227 | DiscardSummary(const DiscardSummary& src);
|
---|
| 228 | /** Assignment operator. */
|
---|
| 229 | DiscardSummary& operator=(const DiscardSummary& src);
|
---|
| 230 |
|
---|
| 231 | /**
|
---|
| 232 | * Add discarded event to summary.
|
---|
| 233 | *
|
---|
| 234 | * @param event event, may not be null.
|
---|
| 235 | */
|
---|
| 236 | void add(const ::log4cxx::spi::LoggingEventPtr& event);
|
---|
| 237 |
|
---|
| 238 | /**
|
---|
| 239 | * Create event with summary information.
|
---|
| 240 | *
|
---|
| 241 | * @return new event.
|
---|
| 242 | */
|
---|
| 243 | ::log4cxx::spi::LoggingEventPtr createEvent(::log4cxx::helpers::Pool& p);
|
---|
| 244 | };
|
---|
| 245 |
|
---|
| 246 | /**
|
---|
| 247 | * Map of DiscardSummary objects keyed by logger name.
|
---|
| 248 | */
|
---|
| 249 | typedef std::map<LogString, DiscardSummary> DiscardMap;
|
---|
| 250 | DiscardMap* discardMap;
|
---|
| 251 |
|
---|
| 252 | /**
|
---|
| 253 | * Buffer size.
|
---|
| 254 | */
|
---|
| 255 | int bufferSize;
|
---|
| 256 |
|
---|
| 257 | /**
|
---|
| 258 | * Nested appenders.
|
---|
| 259 | */
|
---|
| 260 | helpers::AppenderAttachableImplPtr appenders;
|
---|
| 261 |
|
---|
| 262 | /**
|
---|
| 263 | * Dispatcher.
|
---|
| 264 | */
|
---|
| 265 | helpers::Thread dispatcher;
|
---|
| 266 |
|
---|
| 267 | /**
|
---|
| 268 | * Should location info be included in dispatched messages.
|
---|
| 269 | */
|
---|
| 270 | bool locationInfo;
|
---|
| 271 |
|
---|
| 272 | /**
|
---|
| 273 | * Does appender block when buffer is full.
|
---|
| 274 | */
|
---|
| 275 | bool blocking;
|
---|
| 276 |
|
---|
| 277 | /**
|
---|
| 278 | * Dispatch routine.
|
---|
| 279 | */
|
---|
| 280 | static void* LOG4CXX_THREAD_FUNC dispatch(apr_thread_t* thread, void* data);
|
---|
| 281 |
|
---|
| 282 | }; // class AsyncAppender
|
---|
| 283 | LOG4CXX_PTR_DEF(AsyncAppender);
|
---|
| 284 | } // namespace log4cxx
|
---|
| 285 |
|
---|
| 286 | #if defined(_MSC_VER)
|
---|
| 287 | #pragma warning ( pop )
|
---|
| 288 | #endif
|
---|
| 289 |
|
---|
| 290 |
|
---|
| 291 | #endif// _LOG4CXX_ASYNC_APPENDER_H
|
---|
| 292 |
|
---|