Apache log4cxx  Version 0.12.0
asyncappender.h
Go to the documentation of this file.
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 
29 #include <deque>
31 #include <thread>
32 #include <condition_variable>
33 
34 #if defined(NON_BLOCKING)
35  #include <boost/lockfree/queue.hpp>
36 #endif
37 
38 namespace log4cxx
39 {
41 
56 class LOG4CXX_EXPORT AsyncAppender :
57  public virtual spi::AppenderAttachable,
58  public virtual AppenderSkeleton
59 {
60  public:
67 
68 
71  AsyncAppender();
72 
76  virtual ~AsyncAppender();
77 
83  void addAppender(const AppenderPtr newAppender);
84 
85  virtual void doAppend(const spi::LoggingEventPtr& event,
86  log4cxx::helpers::Pool& pool1);
87 
88  void append(const spi::LoggingEventPtr& event, log4cxx::helpers::Pool& p);
89 
95  void close();
96 
101  AppenderList getAllAppenders() const;
102 
109  AppenderPtr getAppender(const LogString& name) const;
110 
117  bool getLocationInfo() const;
123  bool isAttached(const AppenderPtr appender) const;
124 
125  virtual bool requiresLayout() const;
126 
130  void removeAllAppenders();
131 
136  void removeAppender(const AppenderPtr appender);
141  void removeAppender(const LogString& name);
142 
148  void setLocationInfo(bool flag);
149 
155  void setBufferSize(int size);
156 
161  int getBufferSize() const;
162 
169  void setBlocking(bool value);
170 
178  bool getBlocking() const;
179 
180 
186  void setOption(const LogString& option, const LogString& value);
187 
188 
189  private:
191  AsyncAppender& operator=(const AsyncAppender&);
195  enum { DEFAULT_BUFFER_SIZE = 128 };
196 
200 #if defined(NON_BLOCKING)
201  boost::lockfree::queue<log4cxx::spi::LoggingEvent* > buffer;
202  std::atomic<size_t> discardedCount;
203 #else
204  LoggingEventList buffer;
205 #endif
206 
210  std::mutex bufferMutex;
211 
212 #if defined(NON_BLOCKING)
213  ::log4cxx::helpers::Semaphore bufferNotFull;
214  ::log4cxx::helpers::Semaphore bufferNotEmpty;
215 #else
216  std::condition_variable bufferNotFull;
217  std::condition_variable bufferNotEmpty;
218 #endif
219  class DiscardSummary
220  {
221  private:
226 
230  int count;
231 
232  public:
238  DiscardSummary(const ::log4cxx::spi::LoggingEventPtr& event);
240  DiscardSummary(const DiscardSummary& src);
242  DiscardSummary& operator=(const DiscardSummary& src);
243 
249  void add(const ::log4cxx::spi::LoggingEventPtr& event);
250 
257 
258  static
260  size_t discardedCount);
261  };
262 
266  typedef std::map<LogString, DiscardSummary> DiscardMap;
267  DiscardMap* discardMap;
268 
272  int bufferSize;
273 
277  helpers::AppenderAttachableImplPtr appenders;
278 
282  std::thread dispatcher;
283 
287  bool locationInfo;
288 
292  bool blocking;
293 
297  void dispatch();
298 
299 }; // class AsyncAppender
301 } // namespace log4cxx
302 
303 #if defined(_MSC_VER)
304  #pragma warning ( pop )
305 #endif
306 
307 
308 #endif// _LOG4CXX_ASYNC_APPENDER_H
309 
#define LOG4CXX_CAST_ENTRY(Interface)
Definition: object.h:148
#define END_LOG4CXX_CAST_MAP()
Definition: object.h:142
Implementation base class for all appenders.
Definition: appenderskeleton.h:43
std::shared_ptr< LoggingEvent > LoggingEventPtr
Definition: appender.h:37
This Interface is for attaching Appenders to objects.
Definition: appenderattachable.h:38
The AsyncAppender lets users log events asynchronously.
Definition: asyncappender.h:56
#define BEGIN_LOG4CXX_CAST_MAP()
Definition: object.h:136
LOG4CXX_PTR_DEF(Appender)
#define DECLARE_LOG4CXX_OBJECT(object)
Definition: object.h:39
std::shared_ptr< Appender > AppenderPtr
Definition: basicconfigurator.h:28
Definition: pool.h:32
#define LOG4CXX_CAST_ENTRY_CHAIN(Interface)
Definition: object.h:154
LOG4CXX_LIST_DEF(AppenderList, AppenderPtr)
Definition: appender.h:32
std::basic_string< logchar > LogString
Definition: logstring.h:66