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