loggerimpl.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: loggerimpl.h
  4. // Created: 6/2001
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2001-2015 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. const char* function=NULL);
  82. virtual void log(spi::InternalLoggingEvent const &);
  83. /**
  84. * Starting from this logger, search the logger hierarchy for a
  85. * "set" LogLevel and return it. Otherwise, return the LogLevel of the
  86. * root logger.
  87. *
  88. * The Logger class is designed so that this method executes as
  89. * quickly as possible.
  90. */
  91. virtual LogLevel getChainedLogLevel() const;
  92. /**
  93. * Returns the assigned LogLevel, if any, for this Logger.
  94. *
  95. * @return LogLevel - the assigned LogLevel.
  96. */
  97. LogLevel getLogLevel() const { return this->ll; }
  98. /**
  99. * Set the LogLevel of this Logger.
  100. */
  101. void setLogLevel(LogLevel _ll) { this->ll = _ll; }
  102. /**
  103. * Return the the {@link Hierarchy} where this <code>Logger</code>
  104. * instance is attached.
  105. */
  106. virtual Hierarchy& getHierarchy() const;
  107. /**
  108. * Return the logger name.
  109. */
  110. log4cplus::tstring const & getName() const { return name; }
  111. /**
  112. * Get the additivity flag for this Logger instance.
  113. */
  114. bool getAdditivity() const;
  115. /**
  116. * Set the additivity flag for this Logger instance.
  117. */
  118. void setAdditivity(bool additive);
  119. virtual ~LoggerImpl();
  120. protected:
  121. // Ctors
  122. /**
  123. * This constructor created a new <code>Logger</code> instance and
  124. * sets its name.
  125. *
  126. * It is intended to be used by sub-classes only. You should not
  127. * create loggers directly.
  128. *
  129. * @param name The name of the logger.
  130. * @param h Hierarchy
  131. */
  132. LoggerImpl(const log4cplus::tstring& name, Hierarchy& h);
  133. // Methods
  134. /**
  135. * This method creates a new logging event and logs the event
  136. * without further checks.
  137. */
  138. virtual void forcedLog(LogLevel ll,
  139. const log4cplus::tstring& message,
  140. const char* file,
  141. int line,
  142. const char* function);
  143. virtual void forcedLog(spi::InternalLoggingEvent const & ev);
  144. // Data
  145. /** The name of this logger */
  146. log4cplus::tstring name;
  147. /**
  148. * The assigned LogLevel of this logger.
  149. */
  150. LogLevel ll;
  151. /**
  152. * The parent of this logger. All loggers have at least one
  153. * ancestor which is the root logger.
  154. */
  155. SharedLoggerImplPtr parent;
  156. /**
  157. * Additivity is set to true by default, that is children inherit
  158. * the appenders of their ancestors by default. If this variable is
  159. * set to <code>false</code> then the appenders found in the
  160. * ancestors of this logger are not used. However, the children
  161. * of this logger will inherit its appenders, unless the children
  162. * have their additivity flag set to <code>false</code> too. See
  163. * the user manual for more details.
  164. */
  165. bool additive;
  166. private:
  167. // Data
  168. /** Loggers need to know what Hierarchy they are in. */
  169. Hierarchy& hierarchy;
  170. // Disallow copying of instances of this class
  171. LoggerImpl(const LoggerImpl&);
  172. LoggerImpl& operator=(const LoggerImpl&);
  173. // Friends
  174. friend class log4cplus::Logger;
  175. friend class log4cplus::DefaultLoggerFactory;
  176. friend class log4cplus::Hierarchy;
  177. };
  178. typedef LoggerImpl::SharedLoggerImplPtr SharedLoggerImplPtr;
  179. } // end namespace spi
  180. } // end namespace log4cplus
  181. #endif // LOG4CPLUS_SPI_LOGGER_HEADER_