log4cplus  2.0.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:
64  ErrorHandler ();
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
77  virtual ~OnlyOnceErrorHandler ();
78  virtual void error(const log4cplus::tstring& err);
79  virtual void reset();
80 
81  private:
82  bool firstTime;
83  };
84 
85 
133  : public virtual log4cplus::helpers::SharedObject
134  {
135  public:
136  // Ctor
137  Appender();
138  Appender(const log4cplus::helpers::Properties & properties);
139 
140  // Dtor
141  virtual ~Appender();
142 
151  void destructorImpl();
152 
153  // Methods
160  virtual void close() = 0;
161 
165  bool isClosed() const;
166 
172  void syncDoAppend(const log4cplus::spi::InternalLoggingEvent& event);
173 
179  void asyncDoAppend(const log4cplus::spi::InternalLoggingEvent& event);
180 
186  void doAppend(const log4cplus::spi::InternalLoggingEvent& event);
187 
192  virtual log4cplus::tstring getName();
193 
198  virtual void setName(const log4cplus::tstring& name);
199 
203  virtual void setErrorHandler(std::unique_ptr<ErrorHandler> eh);
204 
209  virtual ErrorHandler* getErrorHandler();
210 
216  virtual void setLayout(std::unique_ptr<Layout> layout);
217 
223  virtual Layout* getLayout();
224 
228  void setFilter(log4cplus::spi::FilterPtr f);
229 
233  log4cplus::spi::FilterPtr getFilter() const;
234 
238  void addFilter (log4cplus::spi::FilterPtr f);
239 
243  void addFilter (std::function<
245 
250  LogLevel getThreshold() const { return threshold; }
251 
260  void setThreshold(LogLevel th) { threshold = th; }
261 
268  return ((ll != NOT_SET_LOG_LEVEL) && (ll >= threshold));
269  }
270 
275  void waitToFinishAsyncLogging();
276 
277  protected:
278  // Methods
284  virtual void append(const log4cplus::spi::InternalLoggingEvent& event) = 0;
285 
286  tstring & formatEvent (const log4cplus::spi::InternalLoggingEvent& event) const;
287 
288  // Data
291  std::unique_ptr<Layout> layout;
292 
295 
298 
302 
304  std::unique_ptr<ErrorHandler> errorHandler;
305 
307  std::unique_ptr<helpers::LockFile> lockFile;
308 
312 
314  bool async;
315 #if ! defined (LOG4CPLUS_SINGLE_THREADED)
316  std::atomic<std::size_t> in_flight;
317  std::mutex in_flight_mutex;
318  std::condition_variable in_flight_condition;
319 #endif
320 
322  bool closed;
323 
324  private:
325 #if ! defined (LOG4CPLUS_SINGLE_THREADED)
326  void subtract_in_flight();
327 #endif
328  };
329 
332 
333 } // end namespace log4cplus
334 
335 #endif // LOG4CPLUS_APPENDER_HEADER_
The internal representation of logging events.
Definition: loggingevent.h:51
This header defines Filter and all of it's subclasses.
This class is used to layout strings sent to an {}.
Definition: layout.h:73
LogLevel getThreshold() const
Returns this appenders threshold LogLevel.
Definition: appender.h:250
#define LOG4CPLUS_EXPORT
Definition: win32.h:141
std::unique_ptr< helpers::LockFile > lockFile
Optional system wide synchronization lock.
Definition: appender.h:307
std::atomic< std::size_t > in_flight
Definition: appender.h:316
std::condition_variable in_flight_condition
Definition: appender.h:318
log4cplus::spi::FilterPtr filter
The first filter in the filter chain.
Definition: appender.h:301
void setThreshold(LogLevel th)
Set the threshold LogLevel.
Definition: appender.h:260
bool useLockFile
Use lock file for inter-process synchronization of access to log file.
Definition: appender.h:311
This class is used to "handle" errors encountered in an {}.
Definition: appender.h:61
helpers::SharedObjectPtr< Appender > SharedAppenderPtr
This is a pointer to an Appender.
Definition: appender.h:331
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
std::basic_string< tchar > tstring
Definition: tstring.h:39
This header defines the LogLevel type.
int LogLevel
Defines the minimum set of priorities recognized by the system, that is FATAL_LOG_LEVEL,...
Definition: loglevel.h:48
LogLevel threshold
There is no LogLevel threshold filtering by default.
Definition: appender.h:297
log4cplus::tstring name
Appenders are named.
Definition: appender.h:294
Extend this class for implementing your own strategies for printing log statements.
Definition: appender.h:132
bool closed
Is this appender closed?
Definition: appender.h:322
std::unique_ptr< ErrorHandler > errorHandler
It is assumed and enforced that errorHandler is never null.
Definition: appender.h:304
std::mutex in_flight_mutex
Definition: appender.h:317
bool isAsSevereAsThreshold(LogLevel ll) const
Check whether the message LogLevel is below the appender's threshold.
Definition: appender.h:267
bool async
Asynchronous append.
Definition: appender.h:314
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:291