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.

219 lines
7.1 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: loggerimpl.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_SPI_LOGGER_HEADER_
  23. #define LOG4CPLUS_SPI_LOGGER_HEADER_
  24. #include <log4cplus/config.hxx>
  25. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  26. #pragma once
  27. #endif
  28. #include <log4cplus/tstring.h>
  29. #include <log4cplus/helpers/appenderattachableimpl.h>
  30. #include <log4cplus/helpers/pointer.h>
  31. #include <log4cplus/spi/loggerfactory.h>
  32. #include <memory>
  33. #include <vector>
  34. namespace log4cplus {
  35. class DefaultLoggerFactory;
  36. namespace spi {
  37. /**
  38. * This is the central class in the log4cplus package. One of the
  39. * distintive features of log4cplus are hierarchical loggers and their
  40. * evaluation.
  41. *
  42. * See the <a href="../../../../manual.html">user manual</a> for an
  43. * introduction on this class.
  44. */
  45. class LOG4CPLUS_EXPORT LoggerImpl
  46. : public virtual log4cplus::helpers::SharedObject,
  47. public log4cplus::helpers::AppenderAttachableImpl
  48. {
  49. public:
  50. typedef helpers::SharedObjectPtr<LoggerImpl> SharedLoggerImplPtr;
  51. // Methods
  52. /**
  53. * Call the appenders in the hierrachy starting at
  54. * <code>this</code>. If no appenders could be found, emit a
  55. * warning.
  56. *
  57. * This method calls all the appenders inherited from the
  58. * hierarchy circumventing any evaluation of whether to log or not
  59. * to log the particular log request.
  60. *
  61. * @param event The event to log.
  62. */
  63. virtual void callAppenders(const InternalLoggingEvent& event);
  64. /**
  65. * Close all attached appenders implementing the AppenderAttachable
  66. * interface.
  67. */
  68. virtual void closeNestedAppenders();
  69. /**
  70. * Check whether this logger is enabled for a given LogLevel passed
  71. * as parameter.
  72. *
  73. * @return boolean True if this logger is enabled for <code>ll</code>.
  74. */
  75. virtual bool isEnabledFor(LogLevel ll) const;
  76. /**
  77. * This generic form is intended to be used by wrappers.
  78. */
  79. virtual void log(LogLevel ll, const log4cplus::tstring& message,
  80. const char* file=NULL, int line=-1);
  81. virtual void log(spi::InternalLoggingEvent const &);
  82. /**
  83. * Starting from this logger, search the logger hierarchy for a
  84. * "set" LogLevel and return it. Otherwise, return the LogLevel of the
  85. * root logger.
  86. *
  87. * The Logger class is designed so that this method executes as
  88. * quickly as possible.
  89. */
  90. virtual LogLevel getChainedLogLevel() const;
  91. /**
  92. * Returns the assigned LogLevel, if any, for this Logger.
  93. *
  94. * @return LogLevel - the assigned LogLevel.
  95. */
  96. LogLevel getLogLevel() const { return this->ll; }
  97. /**
  98. * Set the LogLevel of this Logger.
  99. */
  100. void setLogLevel(LogLevel _ll) { this->ll = _ll; }
  101. /**
  102. * Return the the {@link Hierarchy} where this <code>Logger</code>
  103. * instance is attached.
  104. */
  105. virtual Hierarchy& getHierarchy() const;
  106. /**
  107. * Return the logger name.
  108. */
  109. log4cplus::tstring const & getName() const { return name; }
  110. /**
  111. * Get the additivity flag for this Logger instance.
  112. */
  113. bool getAdditivity() const;
  114. /**
  115. * Set the additivity flag for this Logger instance.
  116. */
  117. void setAdditivity(bool additive);
  118. virtual ~LoggerImpl();
  119. protected:
  120. // Ctors
  121. /**
  122. * This constructor created a new <code>Logger</code> instance and
  123. * sets its name.
  124. *
  125. * It is intended to be used by sub-classes only. You should not
  126. * create loggers directly.
  127. *
  128. * @param name The name of the logger.
  129. * @param h Hierarchy
  130. */
  131. LoggerImpl(const log4cplus::tstring& name, Hierarchy& h);
  132. // Methods
  133. /**
  134. * This method creates a new logging event and logs the event
  135. * without further checks.
  136. */
  137. virtual void forcedLog(LogLevel ll,
  138. const log4cplus::tstring& message,
  139. const char* file=NULL,
  140. int line=-1);
  141. virtual void forcedLog(spi::InternalLoggingEvent const & ev);
  142. // Data
  143. /** The name of this logger */
  144. log4cplus::tstring name;
  145. /**
  146. * The assigned LogLevel of this logger.
  147. */
  148. LogLevel ll;
  149. /**
  150. * The parent of this logger. All loggers have at least one
  151. * ancestor which is the root logger.
  152. */
  153. SharedLoggerImplPtr parent;
  154. /**
  155. * Additivity is set to true by default, that is children inherit
  156. * the appenders of their ancestors by default. If this variable is
  157. * set to <code>false</code> then the appenders found in the
  158. * ancestors of this logger are not used. However, the children
  159. * of this logger will inherit its appenders, unless the children
  160. * have their additivity flag set to <code>false</code> too. See
  161. * the user manual for more details.
  162. */
  163. bool additive;
  164. private:
  165. // Data
  166. /** Loggers need to know what Hierarchy they are in. */
  167. Hierarchy& hierarchy;
  168. // Disallow copying of instances of this class
  169. LoggerImpl(const LoggerImpl&);
  170. LoggerImpl& operator=(const LoggerImpl&);
  171. // Friends
  172. friend class log4cplus::Logger;
  173. friend class log4cplus::DefaultLoggerFactory;
  174. friend class log4cplus::Hierarchy;
  175. };
  176. typedef LoggerImpl::SharedLoggerImplPtr SharedLoggerImplPtr;
  177. } // end namespace spi
  178. } // end namespace log4cplus
  179. #endif // LOG4CPLUS_SPI_LOGGER_HEADER_