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.

257 lines
7.5 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: appender.h
  4. // Created: 6/2001
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2001-2010 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. * <dt><tt>UseLockFile</tt></dt>
  71. * <dd>Set this property to <tt>true</tt> if you want your output
  72. * through this appender to be synchronized between multiple
  73. * processes. When this property is set to true then log4cplus
  74. * uses OS specific facilities (e.g., <code>lockf()</code>) to
  75. * provide inter-process locking. With the exception of
  76. * FileAppender and its derived classes, it is also necessary to
  77. * provide path to a lock file using the <tt>LockFile</tt>
  78. * property.
  79. * \sa FileAppender
  80. * </dd>
  81. *
  82. * <dt><tt>LockFile</tt></dt>
  83. * <dd>This property specifies lock file, file used for
  84. * inter-process synchronization of log file access. The property
  85. * is only used when <tt>UseLockFile</tt> is set to true. Then it
  86. * is mandatory.
  87. * \sa FileAppender
  88. * </dd>
  89. * </dl>
  90. */
  91. class LOG4CPLUS_EXPORT Appender
  92. : public virtual log4cplus::helpers::SharedObject
  93. {
  94. public:
  95. // Ctor
  96. Appender();
  97. Appender(const log4cplus::helpers::Properties & properties);
  98. // Dtor
  99. virtual ~Appender();
  100. void destructorImpl();
  101. // Methods
  102. /**
  103. * Release any resources allocated within the appender such as file
  104. * handles, network connections, etc.
  105. *
  106. * It is a programming error to append to a closed appender.
  107. */
  108. virtual void close() = 0;
  109. /**
  110. * This method performs threshold checks and invokes filters before
  111. * delegating actual logging to the subclasses specific {@link
  112. * #append} method.
  113. */
  114. void doAppend(const log4cplus::spi::InternalLoggingEvent& event);
  115. /**
  116. * Get the name of this appender. The name uniquely identifies the
  117. * appender.
  118. */
  119. virtual log4cplus::tstring getName();
  120. /**
  121. * Set the name of this appender. The name is used by other
  122. * components to identify this appender.
  123. */
  124. virtual void setName(const log4cplus::tstring& name);
  125. /**
  126. * Set the {@link ErrorHandler} for this Appender.
  127. */
  128. virtual void setErrorHandler(std::auto_ptr<ErrorHandler> eh);
  129. /**
  130. * Return the currently set {@link ErrorHandler} for this
  131. * Appender.
  132. */
  133. virtual ErrorHandler* getErrorHandler();
  134. /**
  135. * Set the layout for this appender. Note that some appenders have
  136. * their own (fixed) layouts or do not use one. For example, the
  137. * SocketAppender ignores the layout set here.
  138. */
  139. virtual void setLayout(std::auto_ptr<Layout> layout);
  140. /**
  141. * Returns the layout of this appender. The value may be NULL.
  142. *
  143. * This class owns the returned pointer.
  144. */
  145. virtual Layout* getLayout();
  146. /**
  147. * Set the filter chain on this Appender.
  148. */
  149. void setFilter(log4cplus::spi::FilterPtr f) { filter = f; }
  150. /**
  151. * Get the filter chain on this Appender.
  152. */
  153. log4cplus::spi::FilterPtr getFilter() const { return filter; }
  154. /**
  155. * Returns this appenders threshold LogLevel. See the {@link
  156. * #setThreshold} method for the meaning of this option.
  157. */
  158. LogLevel getThreshold() const { return threshold; }
  159. /**
  160. * Set the threshold LogLevel. All log events with lower LogLevel
  161. * than the threshold LogLevel are ignored by the appender.
  162. *
  163. * In configuration files this option is specified by setting the
  164. * value of the <b>Threshold</b> option to a LogLevel
  165. * string, such as "DEBUG", "INFO" and so on.
  166. */
  167. void setThreshold(LogLevel th) { threshold = th; }
  168. /**
  169. * Check whether the message LogLevel is below the appender's
  170. * threshold. If there is no threshold set, then the return value is
  171. * always <code>true</code>.
  172. */
  173. bool isAsSevereAsThreshold(LogLevel ll) const {
  174. return ((ll != NOT_SET_LOG_LEVEL) && (ll >= threshold));
  175. }
  176. protected:
  177. // Methods
  178. /**
  179. * Subclasses of <code>Appender</code> should implement this
  180. * method to perform actual logging.
  181. * @see doAppend method.
  182. */
  183. virtual void append(const log4cplus::spi::InternalLoggingEvent& event) = 0;
  184. tstring & formatEvent (const log4cplus::spi::InternalLoggingEvent& event) const;
  185. // Data
  186. /** The layout variable does not need to be set if the appender
  187. * implementation has its own layout. */
  188. std::auto_ptr<Layout> layout;
  189. /** Appenders are named. */
  190. log4cplus::tstring name;
  191. /** There is no LogLevel threshold filtering by default. */
  192. LogLevel threshold;
  193. /** The first filter in the filter chain. Set to <code>null</code>
  194. * initially. */
  195. log4cplus::spi::FilterPtr filter;
  196. /** It is assumed and enforced that errorHandler is never null. */
  197. std::auto_ptr<ErrorHandler> errorHandler;
  198. //! Optional system wide synchronization lock.
  199. std::auto_ptr<helpers::LockFile> lockFile;
  200. //! Use lock file for inter-process synchronization of access
  201. //! to log file.
  202. bool useLockFile;
  203. /** Is this appender closed? */
  204. bool closed;
  205. };
  206. /** This is a pointer to an Appender. */
  207. typedef helpers::SharedObjectPtr<Appender> SharedAppenderPtr;
  208. } // end namespace log4cplus
  209. #endif // LOG4CPLUS_APPENDER_HEADER_