logger.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: logger.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. * This header defines the Logger class and the logging macros. */
  23. #ifndef LOG4CPLUS_LOGGERHEADER_
  24. #define LOG4CPLUS_LOGGERHEADER_
  25. #include <log4cplus/config.hxx>
  26. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  27. #pragma once
  28. #endif
  29. #include <log4cplus/loglevel.h>
  30. #include <log4cplus/tstring.h>
  31. #include <log4cplus/spi/appenderattachable.h>
  32. #include <log4cplus/spi/loggerfactory.h>
  33. #include <vector>
  34. namespace log4cplus
  35. {
  36. // Forward declarations
  37. class Appender;
  38. class Hierarchy;
  39. class HierarchyLocker;
  40. class DefaultLoggerFactory;
  41. namespace spi
  42. {
  43. class LoggerImpl;
  44. }
  45. /** \typedef std::vector<Logger> LoggerList
  46. * This is a list of {@link Logger Loggers}. */
  47. typedef std::vector<Logger> LoggerList;
  48. /**
  49. * This is the central class in the log4cplus package. One of the
  50. * distintive features of log4cplus are hierarchical loggers and their
  51. * evaluation.
  52. */
  53. class LOG4CPLUS_EXPORT Logger
  54. : public log4cplus::spi::AppenderAttachable
  55. {
  56. public:
  57. // Static Methods
  58. /**
  59. * Returns <code>true </code>if the named logger exists
  60. * (in the default hierarchy).
  61. *
  62. * @param name The name of the logger to search for.
  63. */
  64. static bool exists(const log4cplus::tstring& name);
  65. /*
  66. * Returns all the currently defined loggers in the default
  67. * hierarchy.
  68. *
  69. * The root logger is <em>not</em> included in the returned
  70. * list.
  71. */
  72. static LoggerList getCurrentLoggers();
  73. /**
  74. * Return the default Hierarchy instance.
  75. */
  76. static Hierarchy& getDefaultHierarchy();
  77. /**
  78. * Retrieve a logger with name <code>name</code>. If the named
  79. * logger already exists, then the existing instance will be returned.
  80. * Otherwise, a new instance is created.
  81. *
  82. * By default, loggers do not have a set LogLevel but inherit
  83. * it from the hierarchy. This is one of the central features of
  84. * log4cplus.
  85. *
  86. * @param name The name of the logger to retrieve.
  87. */
  88. static Logger getInstance(const log4cplus::tstring& name);
  89. /**
  90. * Like getInstance() except that the type of logger
  91. * instantiated depends on the type returned by the {@link
  92. * spi::LoggerFactory#makeNewLoggerInstance} method of the
  93. * <code>factory</code> parameter.
  94. *
  95. * This method is intended to be used by sub-classes.
  96. *
  97. * @param name The name of the logger to retrieve.
  98. * @param factory A {@link spi::LoggerFactory} implementation that will
  99. * actually create a new Instance.
  100. */
  101. static Logger getInstance(const log4cplus::tstring& name,
  102. spi::LoggerFactory& factory);
  103. /**
  104. * Return the root of the default logger hierrachy.
  105. *
  106. * The root logger is always instantiated and available. It's
  107. * name is "root".
  108. *
  109. * Nevertheless, calling {@link #getInstance
  110. * Logger.getInstance("root")} does not retrieve the root logger
  111. * but a logger just under root named "root".
  112. */
  113. static Logger getRoot();
  114. /**
  115. * Calling this method will <em>safely</em> close and remove all
  116. * appenders in all the loggers including root contained in the
  117. * default hierachy.
  118. *
  119. * Some appenders such as SocketAppender need to be closed before the
  120. * application exits. Otherwise, pending logging events might be
  121. * lost.
  122. *
  123. * The <code>shutdown</code> method is careful to close nested
  124. * appenders before closing regular appenders. This is allows
  125. * configurations where a regular appender is attached to a logger
  126. * and again to a nested appender.
  127. */
  128. static void shutdown();
  129. // Non-Static Methods
  130. /**
  131. * If <code>assertionVal</code> parameter is <code>false</code>, then
  132. * logs <code>msg</code> with FATAL_LOG_LEVEL log level.
  133. *
  134. * @param assertionVal Truth value of assertion condition.
  135. * @param msg The message to print if <code>assertion</code> is
  136. * false.
  137. */
  138. void assertion(bool assertionVal, const log4cplus::tstring& msg) const;
  139. /**
  140. * Close all attached appenders implementing the AppenderAttachable
  141. * interface.
  142. */
  143. void closeNestedAppenders() const;
  144. /**
  145. * Check whether this logger is enabled for a given
  146. * LogLevel passed as parameter.
  147. *
  148. * @return boolean True if this logger is enabled for <code>ll</code>.
  149. */
  150. bool isEnabledFor(LogLevel ll) const;
  151. /**
  152. * This generic form is intended to be used by wrappers.
  153. */
  154. void log(LogLevel ll, const log4cplus::tstring& message,
  155. const char* file = LOG4CPLUS_CALLER_FILE (),
  156. int line = LOG4CPLUS_CALLER_LINE (),
  157. const char* function = LOG4CPLUS_CALLER_FUNCTION ()) const;
  158. void log(spi::InternalLoggingEvent const &) const;
  159. /**
  160. * This method creates a new logging event and logs the event
  161. * without further checks.
  162. */
  163. void forcedLog(LogLevel ll, const log4cplus::tstring& message,
  164. const char* file = LOG4CPLUS_CALLER_FILE (),
  165. int line = LOG4CPLUS_CALLER_LINE (),
  166. const char* function = LOG4CPLUS_CALLER_FUNCTION ()) const;
  167. void forcedLog(spi::InternalLoggingEvent const &) const;
  168. /**
  169. * Call the appenders in the hierrachy starting at
  170. * <code>this</code>. If no appenders could be found, emit a
  171. * warning.
  172. *
  173. * This method calls all the appenders inherited from the
  174. * hierarchy circumventing any evaluation of whether to log or not
  175. * to log the particular log request.
  176. *
  177. * @param event the event to log.
  178. */
  179. void callAppenders(const spi::InternalLoggingEvent& event) const;
  180. /**
  181. * Starting from this logger, search the logger hierarchy for a
  182. * "set" LogLevel and return it. Otherwise, return the LogLevel of the
  183. * root logger.
  184. *
  185. * The Logger class is designed so that this method executes as
  186. * quickly as possible.
  187. */
  188. LogLevel getChainedLogLevel() const;
  189. /**
  190. * Returns the assigned LogLevel, if any, for this Logger.
  191. *
  192. * @return LogLevel - the assigned LogLevel, can be <code>NOT_SET_LOG_LEVEL</code>.
  193. */
  194. LogLevel getLogLevel() const;
  195. /**
  196. * Set the LogLevel of this Logger.
  197. */
  198. void setLogLevel(LogLevel ll);
  199. /**
  200. * Return the the {@link Hierarchy} where this <code>Logger</code> instance is
  201. * attached.
  202. */
  203. Hierarchy& getHierarchy() const;
  204. /**
  205. * Return the logger name.
  206. */
  207. log4cplus::tstring const & getName() const;
  208. /**
  209. * Get the additivity flag for this Logger instance.
  210. */
  211. bool getAdditivity() const;
  212. /**
  213. * Set the additivity flag for this Logger instance.
  214. */
  215. void setAdditivity(bool additive);
  216. // AppenderAttachable Methods
  217. virtual void addAppender(SharedAppenderPtr newAppender);
  218. virtual SharedAppenderPtrList getAllAppenders();
  219. virtual SharedAppenderPtr getAppender(const log4cplus::tstring& name);
  220. virtual void removeAllAppenders();
  221. virtual void removeAppender(SharedAppenderPtr appender);
  222. virtual void removeAppender(const log4cplus::tstring& name);
  223. Logger () LOG4CPLUS_NOEXCEPT;
  224. Logger(const Logger& rhs) LOG4CPLUS_NOEXCEPT;
  225. Logger& operator=(const Logger& rhs) LOG4CPLUS_NOEXCEPT;
  226. Logger (Logger && rhs) LOG4CPLUS_NOEXCEPT;
  227. Logger & operator = (Logger && rhs) LOG4CPLUS_NOEXCEPT;
  228. virtual ~Logger();
  229. void swap (Logger &) LOG4CPLUS_NOEXCEPT;
  230. /**
  231. * Used to retrieve the parent of this Logger in the
  232. * Logger tree.
  233. */
  234. Logger getParent() const;
  235. protected:
  236. // Data
  237. /** This is a pointer to the implementation class. */
  238. spi::LoggerImpl * value = nullptr;
  239. private:
  240. // Ctors
  241. /**
  242. * This constructor created a new <code>Logger</code> instance
  243. * with a pointer to a Logger implementation.
  244. *
  245. * You should not create loggers directly.
  246. *
  247. * @param ptr A pointer to the Logger implementation. This value
  248. * cannot be NULL.
  249. */
  250. Logger(spi::LoggerImpl * ptr) LOG4CPLUS_NOEXCEPT;
  251. // Friends
  252. friend class log4cplus::spi::LoggerImpl;
  253. friend class log4cplus::Hierarchy;
  254. friend class log4cplus::HierarchyLocker;
  255. friend class log4cplus::DefaultLoggerFactory;
  256. };
  257. /**
  258. * This class is used to create the default implementation of
  259. * the Logger class.
  260. */
  261. class LOG4CPLUS_EXPORT DefaultLoggerFactory : public spi::LoggerFactory {
  262. public:
  263. Logger makeNewLoggerInstance(const log4cplus::tstring& name, Hierarchy& h);
  264. protected:
  265. virtual spi::LoggerImpl * makeNewLoggerImplInstance(
  266. const log4cplus::tstring& name, Hierarchy& h);
  267. };
  268. } // end namespace log4cplus
  269. #endif // LOG4CPLUS_LOGGERHEADER_