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.

321 lines
9.9 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: logger.h
  4. // Created: 6/2001
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2001-2013 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=NULL, int line=-1) 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=NULL, int line=-1) const;
  165. void forcedLog(spi::InternalLoggingEvent const &) const;
  166. /**
  167. * Call the appenders in the hierrachy starting at
  168. * <code>this</code>. If no appenders could be found, emit a
  169. * warning.
  170. *
  171. * This method calls all the appenders inherited from the
  172. * hierarchy circumventing any evaluation of whether to log or not
  173. * to log the particular log request.
  174. *
  175. * @param event the event to log.
  176. */
  177. void callAppenders(const spi::InternalLoggingEvent& event) const;
  178. /**
  179. * Starting from this logger, search the logger hierarchy for a
  180. * "set" LogLevel and return it. Otherwise, return the LogLevel of the
  181. * root logger.
  182. *
  183. * The Logger class is designed so that this method executes as
  184. * quickly as possible.
  185. */
  186. LogLevel getChainedLogLevel() const;
  187. /**
  188. * Returns the assigned LogLevel, if any, for this Logger.
  189. *
  190. * @return LogLevel - the assigned LogLevel, can be <code>NOT_SET_LOG_LEVEL</code>.
  191. */
  192. LogLevel getLogLevel() const;
  193. /**
  194. * Set the LogLevel of this Logger.
  195. */
  196. void setLogLevel(LogLevel ll);
  197. /**
  198. * Return the the {@link Hierarchy} where this <code>Logger</code> instance is
  199. * attached.
  200. */
  201. Hierarchy& getHierarchy() const;
  202. /**
  203. * Return the logger name.
  204. */
  205. log4cplus::tstring const & getName() const;
  206. /**
  207. * Get the additivity flag for this Logger instance.
  208. */
  209. bool getAdditivity() const;
  210. /**
  211. * Set the additivity flag for this Logger instance.
  212. */
  213. void setAdditivity(bool additive);
  214. // AppenderAttachable Methods
  215. virtual void addAppender(SharedAppenderPtr newAppender);
  216. virtual SharedAppenderPtrList getAllAppenders();
  217. virtual SharedAppenderPtr getAppender(const log4cplus::tstring& name);
  218. virtual void removeAllAppenders();
  219. virtual void removeAppender(SharedAppenderPtr appender);
  220. virtual void removeAppender(const log4cplus::tstring& name);
  221. Logger ();
  222. Logger(const Logger& rhs);
  223. Logger& operator=(const Logger& rhs);
  224. #if defined (LOG4CPLUS_HAVE_RVALUE_REFS)
  225. Logger (Logger && rhs);
  226. Logger & operator = (Logger && rhs);
  227. #endif
  228. virtual ~Logger();
  229. void swap (Logger &);
  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;
  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. LOG4CPLUS_PRIVATE Logger(spi::LoggerImpl * ptr);
  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. };
  265. } // end namespace log4cplus
  266. #endif // LOG4CPLUS_LOGGERHEADER_