loggerimpl.h 7.0 KB

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