appender.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. /** @file */
  22. #ifndef LOG4CPLUS_APPENDER_HEADER_
  23. #define LOG4CPLUS_APPENDER_HEADER_
  24. #include <log4cplus/config.hxx>
  25. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  26. #pragma once
  27. #endif
  28. #include <log4cplus/layout.h>
  29. #include <log4cplus/loglevel.h>
  30. #include <log4cplus/tstring.h>
  31. #include <log4cplus/helpers/pointer.h>
  32. #include <log4cplus/spi/filter.h>
  33. #include <log4cplus/helpers/lockfile.h>
  34. #include <memory>
  35. #include <mutex>
  36. #include <atomic>
  37. #include <condition_variable>
  38. namespace log4cplus {
  39. namespace helpers
  40. {
  41. class Properties;
  42. }
  43. /**
  44. * This class is used to "handle" errors encountered in an {@link
  45. * log4cplus::Appender}.
  46. */
  47. class LOG4CPLUS_EXPORT ErrorHandler
  48. {
  49. public:
  50. ErrorHandler ();
  51. virtual ~ErrorHandler() = 0;
  52. virtual void error(const log4cplus::tstring& err) = 0;
  53. virtual void reset() = 0;
  54. };
  55. class LOG4CPLUS_EXPORT OnlyOnceErrorHandler
  56. : public ErrorHandler
  57. {
  58. public:
  59. // Ctor
  60. OnlyOnceErrorHandler();
  61. virtual ~OnlyOnceErrorHandler ();
  62. virtual void error(const log4cplus::tstring& err);
  63. virtual void reset();
  64. private:
  65. bool firstTime;
  66. };
  67. /**
  68. * Extend this class for implementing your own strategies for printing log
  69. * statements.
  70. *
  71. * <h3>Properties</h3>
  72. * <dl>
  73. *
  74. * <dt><tt>layout</tt></dt>
  75. * <dd>This property specifies message layout used by
  76. * Appender.
  77. * \sa Layout
  78. * </dd>
  79. *
  80. * <dt><tt>filters</tt></dt>
  81. * <dd>This property specifies possibly multiple filters used by
  82. * Appender. Each of multple filters and its properties is under a
  83. * numbered subkey of filters key. E.g.:
  84. * <tt>filters.<em>1</em>=log4cplus::spi::LogLevelMatchFilter</tt>. Filter
  85. * subkey numbers must be consecutive.</dd>
  86. *
  87. * <dt><tt>Threshold</tt></dt>
  88. * <dd>This property specifies log level threshold. Events with
  89. * lower log level than the threshold will not be logged by
  90. * appender.</dd>
  91. *
  92. * <dt><tt>UseLockFile</tt></dt>
  93. * <dd>Set this property to <tt>true</tt> if you want your output
  94. * through this appender to be synchronized between multiple
  95. * processes. When this property is set to true then log4cplus
  96. * uses OS specific facilities (e.g., <code>lockf()</code>) to
  97. * provide inter-process locking. With the exception of
  98. * FileAppender and its derived classes, it is also necessary to
  99. * provide path to a lock file using the <tt>LockFile</tt>
  100. * property.
  101. * \sa FileAppender
  102. * </dd>
  103. *
  104. * <dt><tt>LockFile</tt></dt>
  105. * <dd>This property specifies lock file, file used for
  106. * inter-process synchronization of log file access. The property
  107. * is only used when <tt>UseLockFile</tt> is set to true. Then it
  108. * is mandatory.
  109. * \sa FileAppender
  110. * </dd>
  111. *
  112. * <dt><tt>AsyncAppend</tt></dt>
  113. * <dd>Set this property to <tt>true</tt> if you want all appends using
  114. * this appender to be done asynchronously. Default is <tt>false</tt>.</dd>
  115. *
  116. * </dl>
  117. */
  118. class LOG4CPLUS_EXPORT Appender
  119. : public virtual log4cplus::helpers::SharedObject
  120. {
  121. public:
  122. // Ctor
  123. Appender();
  124. Appender(const log4cplus::helpers::Properties & properties);
  125. // Dtor
  126. virtual ~Appender();
  127. /**
  128. * This function is for derived appenders to call from their
  129. * destructors. All classes derived from `Appender` class
  130. * _must_ call this function from their destructors. It
  131. * ensures that appenders will get properly closed during
  132. * shutdown by call to `close()` function before they are
  133. * destroyed.
  134. */
  135. void destructorImpl();
  136. // Methods
  137. /**
  138. * Release any resources allocated within the appender such as file
  139. * handles, network connections, etc.
  140. *
  141. * It is a programming error to append to a closed appender.
  142. */
  143. virtual void close() = 0;
  144. /**
  145. * Check if this appender is in closed state.
  146. */
  147. bool isClosed() const;
  148. /**
  149. * This method performs threshold checks and invokes filters before
  150. * delegating actual logging to the subclasses specific {@link
  151. * #append} method.
  152. */
  153. void syncDoAppend(const log4cplus::spi::InternalLoggingEvent& event);
  154. /**
  155. * This method performs book keeping related to asynchronous logging
  156. * and executes `syncDoAppend()` to do the actual logging.
  157. */
  158. void asyncDoAppend(const log4cplus::spi::InternalLoggingEvent& event);
  159. /**
  160. * This function checks `async` flag. It either executes
  161. * `syncDoAppend()` directly or enqueues its execution to thread pool
  162. * thread.
  163. */
  164. void doAppend(const log4cplus::spi::InternalLoggingEvent& event);
  165. /**
  166. * Get the name of this appender. The name uniquely identifies the
  167. * appender.
  168. */
  169. virtual log4cplus::tstring getName();
  170. /**
  171. * Set the name of this appender. The name is used by other
  172. * components to identify this appender.
  173. */
  174. virtual void setName(const log4cplus::tstring& name);
  175. /**
  176. * Set the {@link ErrorHandler} for this Appender.
  177. */
  178. virtual void setErrorHandler(std::unique_ptr<ErrorHandler> eh);
  179. /**
  180. * Return the currently set {@link ErrorHandler} for this
  181. * Appender.
  182. */
  183. virtual ErrorHandler* getErrorHandler();
  184. /**
  185. * Set the layout for this appender. Note that some appenders have
  186. * their own (fixed) layouts or do not use one. For example, the
  187. * SocketAppender ignores the layout set here.
  188. */
  189. virtual void setLayout(std::unique_ptr<Layout> layout);
  190. /**
  191. * Returns the layout of this appender. The value may be NULL.
  192. *
  193. * This class owns the returned pointer.
  194. */
  195. virtual Layout* getLayout();
  196. /**
  197. * Set the filter chain on this Appender.
  198. */
  199. void setFilter(log4cplus::spi::FilterPtr f);
  200. /**
  201. * Get the filter chain on this Appender.
  202. */
  203. log4cplus::spi::FilterPtr getFilter() const;
  204. /**
  205. * Add filter at the end of the filters chain.
  206. */
  207. void addFilter (log4cplus::spi::FilterPtr f);
  208. /**
  209. * Add filter at the end of the filters chain.
  210. */
  211. void addFilter (std::function<
  212. spi::FilterResult (const log4cplus::spi::InternalLoggingEvent &)>);
  213. /**
  214. * Returns this appenders threshold LogLevel. See the {@link
  215. * #setThreshold} method for the meaning of this option.
  216. */
  217. LogLevel getThreshold() const { return threshold; }
  218. /**
  219. * Set the threshold LogLevel. All log events with lower LogLevel
  220. * than the threshold LogLevel are ignored by the appender.
  221. *
  222. * In configuration files this option is specified by setting the
  223. * value of the <b>Threshold</b> option to a LogLevel
  224. * string, such as "DEBUG", "INFO" and so on.
  225. */
  226. void setThreshold(LogLevel th) { threshold = th; }
  227. /**
  228. * Check whether the message LogLevel is below the appender's
  229. * threshold. If there is no threshold set, then the return value is
  230. * always <code>true</code>.
  231. */
  232. bool isAsSevereAsThreshold(LogLevel ll) const {
  233. return ((ll != NOT_SET_LOG_LEVEL) && (ll >= threshold));
  234. }
  235. /**
  236. * This method waits for all events that are being asynchronously
  237. * logged to finish.
  238. */
  239. void waitToFinishAsyncLogging();
  240. protected:
  241. // Methods
  242. /**
  243. * Subclasses of <code>Appender</code> should implement this
  244. * method to perform actual logging.
  245. * @see doAppend method.
  246. */
  247. virtual void append(const log4cplus::spi::InternalLoggingEvent& event) = 0;
  248. tstring & formatEvent (const log4cplus::spi::InternalLoggingEvent& event) const;
  249. // Data
  250. /** The layout variable does not need to be set if the appender
  251. * implementation has its own layout. */
  252. std::unique_ptr<Layout> layout;
  253. /** Appenders are named. */
  254. log4cplus::tstring name;
  255. /** There is no LogLevel threshold filtering by default. */
  256. LogLevel threshold;
  257. /** The first filter in the filter chain. Set to <code>null</code>
  258. * initially. */
  259. log4cplus::spi::FilterPtr filter;
  260. /** It is assumed and enforced that errorHandler is never null. */
  261. std::unique_ptr<ErrorHandler> errorHandler;
  262. //! Optional system wide synchronization lock.
  263. std::unique_ptr<helpers::LockFile> lockFile;
  264. //! Use lock file for inter-process synchronization of access
  265. //! to log file.
  266. bool useLockFile;
  267. //! Asynchronous append.
  268. bool async;
  269. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  270. std::atomic<std::size_t> in_flight;
  271. std::mutex in_flight_mutex;
  272. std::condition_variable in_flight_condition;
  273. #endif
  274. /** Is this appender closed? */
  275. bool closed;
  276. private:
  277. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  278. void subtract_in_flight();
  279. #endif
  280. };
  281. /** This is a pointer to an Appender. */
  282. typedef helpers::SharedObjectPtr<Appender> SharedAppenderPtr;
  283. } // end namespace log4cplus
  284. #endif // LOG4CPLUS_APPENDER_HEADER_