Apache log4cxx  Version 0.11.0
asyncappender.h
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 #if defined(NON_BLOCKING)
36  #include <boost/lockfree/queue.hpp>
37 #endif
38 
39 namespace log4cxx
40 {
41 LOG4CXX_LIST_DEF(LoggingEventList, log4cxx::spi::LoggingEventPtr);
42 
57 class LOG4CXX_EXPORT AsyncAppender :
58  public virtual spi::AppenderAttachable,
59  public virtual AppenderSkeleton
60 {
61  public:
62  DECLARE_LOG4CXX_OBJECT(AsyncAppender)
63  BEGIN_LOG4CXX_CAST_MAP()
64  LOG4CXX_CAST_ENTRY(AsyncAppender)
65  LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton)
66  LOG4CXX_CAST_ENTRY(spi::AppenderAttachable)
67  END_LOG4CXX_CAST_MAP()
68 
69 
72  AsyncAppender();
73 
77  virtual ~AsyncAppender();
78 
79  void addRef() const;
80  void releaseRef() const;
81 
87  void addAppender(const AppenderPtr& newAppender);
88 
89  virtual void doAppend(const spi::LoggingEventPtr& event,
90  log4cxx::helpers::Pool& pool1);
91 
92  void append(const spi::LoggingEventPtr& event, log4cxx::helpers::Pool& p);
93 
99  void close();
100 
105  AppenderList getAllAppenders() const;
106 
113  AppenderPtr getAppender(const LogString& name) const;
114 
121  bool getLocationInfo() const;
127  bool isAttached(const AppenderPtr& appender) const;
128 
129  virtual bool requiresLayout() const;
130 
134  void removeAllAppenders();
135 
140  void removeAppender(const AppenderPtr& appender);
145  void removeAppender(const LogString& name);
146 
152  void setLocationInfo(bool flag);
153 
159  void setBufferSize(int size);
160 
165  int getBufferSize() const;
166 
173  void setBlocking(bool value);
174 
182  bool getBlocking() const;
183 
184 
190  void setOption(const LogString& option, const LogString& value);
191 
192 
193  private:
195  AsyncAppender& operator=(const AsyncAppender&);
199  enum { DEFAULT_BUFFER_SIZE = 128 };
200 
204 #if defined(NON_BLOCKING)
205  boost::lockfree::queue<log4cxx::spi::LoggingEvent* > buffer;
206  std::atomic<size_t> discardedCount;
207 #else
208  LoggingEventList buffer;
209 #endif
210 
214  SHARED_MUTEX bufferMutex;
215 
216 #if defined(NON_BLOCKING)
217  ::log4cxx::helpers::Semaphore bufferNotFull;
218  ::log4cxx::helpers::Semaphore bufferNotEmpty;
219 #else
220  ::log4cxx::helpers::Condition bufferNotFull;
221  ::log4cxx::helpers::Condition bufferNotEmpty;
222 #endif
223  class DiscardSummary
224  {
225  private:
230 
234  int count;
235 
236  public:
242  DiscardSummary(const ::log4cxx::spi::LoggingEventPtr& event);
244  DiscardSummary(const DiscardSummary& src);
246  DiscardSummary& operator=(const DiscardSummary& src);
247 
253  void add(const ::log4cxx::spi::LoggingEventPtr& event);
254 
261 
262  static
263  ::log4cxx::spi::LoggingEventPtr createEvent(::log4cxx::helpers::Pool& p,
264  size_t discardedCount);
265  };
266 
270  typedef std::map<LogString, DiscardSummary> DiscardMap;
271  DiscardMap* discardMap;
272 
276  int bufferSize;
277 
281  helpers::AppenderAttachableImplPtr appenders;
282 
286  helpers::Thread dispatcher;
287 
291  bool locationInfo;
292 
296  bool blocking;
297 
301  static void* LOG4CXX_THREAD_FUNC dispatch(apr_thread_t* thread, void* data);
302 
303 }; // class AsyncAppender
304 LOG4CXX_PTR_DEF(AsyncAppender);
305 } // namespace log4cxx
306 
307 #if defined(_MSC_VER)
308  #pragma warning ( pop )
309 #endif
310 
311 
312 #endif// _LOG4CXX_ASYNC_APPENDER_H
313 
This class implements an approximation of java.util.Thread.
Definition: thread.h:59
Implementation base class for all appenders.
Definition: appenderskeleton.h:45
This Interface is for attaching Appenders to objects.
Definition: appenderattachable.h:39
The AsyncAppender lets users log events asynchronously.
Definition: asyncappender.h:57
This class provides a means for one thread to suspend exception until notified by another thread to r...
Definition: condition.h:39
Definition: pool.h:32
Definition: appender.h:33