log4cplus  2.1.0
appender.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 // Module: Log4CPLUS
3 // File: appender.h
4 // Created: 6/2001
5 // Author: Tad E. Smith
6 //
7 //
8 // Copyright 2001-2017 Tad E. Smith
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 
24 #ifndef LOG4CPLUS_APPENDER_HEADER_
25 #define LOG4CPLUS_APPENDER_HEADER_
26 
27 #include <log4cplus/config.hxx>
28 
29 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
30 #pragma once
31 #endif
32 
33 #include <log4cplus/layout.h>
34 #include <log4cplus/loglevel.h>
35 #include <log4cplus/tstring.h>
37 #include <log4cplus/spi/filter.h>
39 
40 #include <memory>
41 #include <mutex>
42 #include <atomic>
43 #include <condition_variable>
44 
45 
46 namespace log4cplus {
47 
48 
49  namespace helpers
50  {
51 
52  class Properties;
53 
54  }
55 
56 
62  {
63  public:
65  virtual ~ErrorHandler() = 0;
66  virtual void error(const log4cplus::tstring& err) = 0;
67  virtual void reset() = 0;
68  };
69 
70 
72  : public ErrorHandler
73  {
74  public:
75  // Ctor
78  virtual void error(const log4cplus::tstring& err);
79  virtual void reset();
80 
81  private:
82  bool firstTime;
83  };
84 
85 
138  : public virtual log4cplus::helpers::SharedObject
139  {
140  public:
141  // Ctor
144 
145  // Dtor
146  virtual ~Appender();
147 
157 
158  // Methods
165  virtual void close() = 0;
166 
170  bool isClosed() const;
171 
178 
185 
192 
198 
203  virtual void setName(const log4cplus::tstring& name);
204 
208  virtual void setErrorHandler(std::unique_ptr<ErrorHandler> eh);
209 
215 
221  virtual void setLayout(std::unique_ptr<Layout> layout);
222 
228  virtual Layout* getLayout();
229 
234 
239 
244 
248  void addFilter (std::function<
250 
255  LogLevel getThreshold() const { return threshold; }
256 
265  void setThreshold(LogLevel th) { threshold = th; }
266 
273  return ((ll != NOT_SET_LOG_LEVEL) && (ll >= threshold));
274  }
275 
281 
282  protected:
283  // Methods
289  virtual void append(const log4cplus::spi::InternalLoggingEvent& event) = 0;
290 
292 
293  // Data
296  std::unique_ptr<Layout> layout;
297 
300 
303 
307 
309  std::unique_ptr<ErrorHandler> errorHandler;
310 
312  std::unique_ptr<helpers::LockFile> lockFile;
313 
317 
319  bool async;
320 #if ! defined (LOG4CPLUS_SINGLE_THREADED)
321  std::atomic<std::size_t> in_flight;
322  std::mutex in_flight_mutex;
323  std::condition_variable in_flight_condition;
324 #endif
325 
327  bool closed;
328 
329  private:
330 #if ! defined (LOG4CPLUS_SINGLE_THREADED)
331  void subtract_in_flight();
332 #endif
333  };
334 
337 
338 } // end namespace log4cplus
339 
340 #endif // LOG4CPLUS_APPENDER_HEADER_
Extend this class for implementing your own strategies for printing log statements.
Definition: appender.h:139
std::mutex in_flight_mutex
Definition: appender.h:322
void setThreshold(LogLevel th)
Set the threshold LogLevel.
Definition: appender.h:265
virtual void close()=0
Release any resources allocated within the appender such as file handles, network connections,...
std::atomic< std::size_t > in_flight
Definition: appender.h:321
void asyncDoAppend(const log4cplus::spi::InternalLoggingEvent &event)
This method performs book keeping related to asynchronous logging and executes syncDoAppend() to do t...
bool isClosed() const
Check if this appender is in closed state.
LogLevel getThreshold() const
Returns this appenders threshold LogLevel.
Definition: appender.h:255
bool async
Asynchronous append.
Definition: appender.h:319
virtual log4cplus::tstring getName()
Get the name of this appender.
log4cplus::spi::FilterPtr filter
The first filter in the filter chain.
Definition: appender.h:306
void addFilter(log4cplus::spi::FilterPtr f)
Add filter at the end of the filters chain.
void syncDoAppend(const log4cplus::spi::InternalLoggingEvent &event)
This method performs threshold checks and invokes filters before delegating actual logging to the sub...
virtual void setErrorHandler(std::unique_ptr< ErrorHandler > eh)
Set the ErrorHandler for this Appender.
virtual ErrorHandler * getErrorHandler()
Return the currently set ErrorHandler for this Appender.
tstring & formatEvent(const log4cplus::spi::InternalLoggingEvent &event) const
virtual Layout * getLayout()
Returns the layout of this appender.
bool useLockFile
Use lock file for inter-process synchronization of access to log file.
Definition: appender.h:316
std::unique_ptr< ErrorHandler > errorHandler
It is assumed and enforced that errorHandler is never null.
Definition: appender.h:309
std::condition_variable in_flight_condition
Definition: appender.h:323
std::unique_ptr< Layout > layout
The layout variable does not need to be set if the appender implementation has its own layout.
Definition: appender.h:296
virtual void setLayout(std::unique_ptr< Layout > layout)
Set the layout for this appender.
void doAppend(const log4cplus::spi::InternalLoggingEvent &event)
This function checks async flag.
log4cplus::tstring name
Appenders are named.
Definition: appender.h:299
bool closed
Is this appender closed?
Definition: appender.h:327
Appender(const log4cplus::helpers::Properties &properties)
virtual void setName(const log4cplus::tstring &name)
Set the name of this appender.
virtual void append(const log4cplus::spi::InternalLoggingEvent &event)=0
Subclasses of Appender should implement this method to perform actual logging.
bool isAsSevereAsThreshold(LogLevel ll) const
Check whether the message LogLevel is below the appender's threshold.
Definition: appender.h:272
void waitToFinishAsyncLogging()
This method waits for all events that are being asynchronously logged to finish.
std::unique_ptr< helpers::LockFile > lockFile
Optional system wide synchronization lock.
Definition: appender.h:312
LogLevel threshold
There is no LogLevel threshold filtering by default.
Definition: appender.h:302
void addFilter(std::function< spi::FilterResult(const log4cplus::spi::InternalLoggingEvent &)>)
Add filter at the end of the filters chain.
log4cplus::spi::FilterPtr getFilter() const
Get the filter chain on this Appender.
void setFilter(log4cplus::spi::FilterPtr f)
Set the filter chain on this Appender.
void destructorImpl()
This function is for derived appenders to call from their destructors.
This class is used to "handle" errors encountered in an {}.
Definition: appender.h:62
virtual void error(const log4cplus::tstring &err)=0
virtual void reset()=0
This class is used to layout strings sent to an {}.
Definition: layout.h:74
virtual void error(const log4cplus::tstring &err)
The internal representation of logging events.
Definition: loggingevent.h:51
This header defines Filter and all of it's subclasses.
This header defines the LogLevel type.
helpers::SharedObjectPtr< Appender > SharedAppenderPtr
This is a pointer to an Appender.
Definition: appender.h:336
std::basic_string< tchar > tstring
Definition: tstring.h:39
const LogLevel NOT_SET_LOG_LEVEL
The NOT_SET_LOG_LEVEL LogLevel is used to indicated that no particular LogLevel is desired and that t...
Definition: loglevel.h:95
int LogLevel
Defines the minimum set of priorities recognized by the system, that is FATAL_LOG_LEVEL,...
Definition: loglevel.h:48
#define LOG4CPLUS_EXPORT
Definition: win32.h:141