You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
8.6 KiB

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