logger_impl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // $Id$
  15. #ifndef __LOGGER_IMPL_H
  16. #define __LOGGER_IMPL_H
  17. #include <cstdlib>
  18. #include <string>
  19. #include <boost/lexical_cast.hpp>
  20. #include <log4cxx/logger.h>
  21. #include <log4cxx/logger.h>
  22. #include <log/debug_levels.h>
  23. #include <log/logger.h>
  24. #include <log/message_types.h>
  25. namespace isc {
  26. namespace log {
  27. /// \brief Log4cxx Logger Implementation
  28. ///
  29. /// The logger uses a "pimpl" idiom for implementation, where the base logger
  30. /// class contains little more than a pointer to the implementation class, and
  31. /// all actions are carried out by the latter. This class is an implementation
  32. /// class interfacing to the log4cxx logging system.
  33. class LoggerImpl {
  34. public:
  35. /// \brief Constructor
  36. ///
  37. /// Creates/attaches to a logger of a specific name.
  38. ///
  39. /// \param name Name of the logger. If the name is that of the root name,
  40. /// this creates an instance of the root logger; otherwise it creates a
  41. /// child of the root logger.
  42. ///
  43. /// \param exit_delete This argument is present to get round a bug in
  44. /// log4cxx. If a log4cxx logger is declared outside an execution unit, it
  45. /// is not deleted until the program runs down. At that point all such
  46. /// objects - including internal log4cxx objects - are deleted. However,
  47. /// there seems to be a bug in log4cxx where the way that such objects are
  48. /// destroyed causes a MutexException to be thrown (this is described in
  49. /// https://issues.apache.org/jira/browse/LOGCXX-322). As this only occurs
  50. /// during program rundown, the issue is not serious - it just looks bad to
  51. /// have the program crash instead of shut down cleanly.\n
  52. /// \n
  53. /// The original implementation of the isc::log::Logger had as a member a
  54. /// log4cxx logger (actually a LoggerPtr). If the isc:: Logger was declared
  55. /// statically, when it was destroyed at the end of the program the internal
  56. /// LoggerPtr was destroyed, which triggered the problem. The problem did
  57. /// not occur if the isc::log::Logger was created on the stack. To get
  58. /// round this, the internal LoggerPtr is now created dynamically. The
  59. /// exit_delete argument controls its destruction: if true, it is destroyed
  60. /// in the ISC Logger destructor. If false, it is not.\n
  61. /// \n
  62. /// When creating an isc::log::Logger on the stack, the argument should be
  63. /// false (the default); when the Logger is destroyed, all the internal
  64. /// log4cxx objects are destroyed. As only the logger (and not the internal
  65. /// log4cxx data structures are being destroyed), all is well. However,
  66. /// when creating the logger statically, the argument should be false. This
  67. /// means that the log4cxx objects are not destroyed at program rundown;
  68. /// instead memory is reclaimed and files are closed when the process is
  69. /// destroyed, something that does not trigger the bug.
  70. LoggerImpl(const std::string& name, bool exit_delete = false) :
  71. loggerptr_(NULL), name_(name), exit_delete_(exit_delete)
  72. {}
  73. /// \brief Destructor
  74. virtual ~LoggerImpl();
  75. /// \brief Get the full name of the logger (including the root name)
  76. virtual std::string getName() {
  77. return (getLogger()->getName());
  78. }
  79. /// \brief Set Severity Level for Logger
  80. ///
  81. /// Sets the level at which this logger will log messages. If none is set,
  82. /// the level is inherited from the parent.
  83. ///
  84. /// \param severity Severity level to log
  85. /// \param dbglevel If the severity is DEBUG, this is the debug level.
  86. /// This can be in the range 1 to 100 and controls the verbosity. A value
  87. /// outside these limits is silently coerced to the nearest boundary.
  88. virtual void setSeverity(isc::log::Severity severity, int dbglevel = 1);
  89. /// \brief Get Severity Level for Logger
  90. ///
  91. /// \return The current logging level of this logger. In most cases though,
  92. /// the effective logging level is what is required.
  93. virtual isc::log::Severity getSeverity() {
  94. return getSeverityCommon(getLogger(), false);
  95. }
  96. /// \brief Get Effective Severity Level for Logger
  97. ///
  98. /// \return The effective severity level of the logger. This is the same
  99. /// as getSeverity() if the logger has a severity level set, but otherwise
  100. /// is the severity of the parent.
  101. virtual isc::log::Severity getEffectiveSeverity() {
  102. return getSeverityCommon(getLogger(), true);
  103. }
  104. /// \brief Return DEBUG Level
  105. ///
  106. /// \return Current setting of debug level. This is returned regardless of
  107. /// whether the
  108. virtual int getDebugLevel();
  109. /// \brief Returns if Debug Message Should Be Output
  110. ///
  111. /// \param dbglevel Level for which debugging is checked. Debugging is
  112. /// enabled only if the logger has DEBUG enabled and if the dbglevel
  113. /// checked is less than or equal to the debug level set for the logger.
  114. virtual bool
  115. isDebugEnabled(int dbglevel = MIN_DEBUG_LEVEL) {
  116. return (getLogger()->getEffectiveLevel()->toInt() <=
  117. (log4cxx::Level::DEBUG_INT - dbglevel));
  118. }
  119. /// \brief Is INFO Enabled?
  120. virtual bool isInfoEnabled() {
  121. return (getLogger()->isInfoEnabled());
  122. }
  123. /// \brief Is WARNING Enabled?
  124. virtual bool isWarnEnabled() {
  125. return (getLogger()->isWarnEnabled());
  126. }
  127. /// \brief Is ERROR Enabled?
  128. virtual bool isErrorEnabled() {
  129. return (getLogger()->isErrorEnabled());
  130. }
  131. /// \brief Is FATAL Enabled?
  132. virtual bool isFatalEnabled() {
  133. return (getLogger()->isFatalEnabled());
  134. }
  135. /// \brief Output Debug Message
  136. ///
  137. /// \param ident Message identification.
  138. /// \param text Text to log
  139. void debug(MessageID ident, const char* text) {
  140. LOG4CXX_DEBUG(getLogger(), ident << ", " << text);
  141. }
  142. /// \brief Output Informational Message
  143. ///
  144. /// \param ident Message identification.
  145. /// \param text Text to log
  146. void info(MessageID ident, const char* text) {
  147. LOG4CXX_INFO(getLogger(), ident << ", " << text);
  148. }
  149. /// \brief Output Warning Message
  150. ///
  151. /// \param ident Message identification.
  152. /// \param text Text to log
  153. void warn(MessageID ident, const char* text) {
  154. LOG4CXX_WARN(getLogger(), ident << ", " << text);
  155. }
  156. /// \brief Output Error Message
  157. ///
  158. /// \param ident Message identification.
  159. /// \param text Text to log
  160. void error(MessageID ident, const char* text) {
  161. LOG4CXX_ERROR(getLogger(), ident << ", " << text);
  162. }
  163. /// \brief Output Fatal Message
  164. ///
  165. /// \param ident Message identification.
  166. /// \param text Text to log
  167. void fatal(MessageID ident, const char* text) {
  168. LOG4CXX_FATAL(getLogger(), ident << ", " << text);
  169. }
  170. //@{
  171. /// \brief Testing Methods
  172. ///
  173. /// The next set of methods are used in testing. As they are accessed from
  174. /// the main logger class, they must be public.
  175. /// \brief Equality
  176. ///
  177. /// Check if two instances of this logger refer to the same stream.
  178. /// (This method is principally for testing.)
  179. ///
  180. /// \return true if the logger objects are instances of the same logger.
  181. bool operator==(const LoggerImpl& other) {
  182. return (*loggerptr_ == *other.loggerptr_);
  183. }
  184. /// \brief Logger Initialized
  185. ///
  186. /// Check that the logger has been properly initialized. (This method
  187. /// is principally for testing.)
  188. ///
  189. /// \return true if this logger object has been initialized.
  190. bool isInitialized() {
  191. return (loggerptr_ != NULL);
  192. }
  193. //@}
  194. protected:
  195. /// \brief Convert Between BIND-10 and log4cxx Logging Levels
  196. ///
  197. /// This method is marked protected to allow for unit testing.
  198. ///
  199. /// \param value log4cxx numeric logging level
  200. ///
  201. /// \return BIND-10 logging severity
  202. isc::log::Severity convertLevel(int value);
  203. private:
  204. /// \brief Get Severity Level for Logger
  205. ///
  206. /// This is common code for getSeverity() and getEffectiveSeverity() -
  207. /// it returns the severity of the logger; if not set (and the check_parent)
  208. /// flag is set, it searches up the parent-child tree until a severity
  209. /// level is found and uses that.
  210. ///
  211. /// \param ptrlogger Pointer to the log4cxx logger to check.
  212. /// \param check_parent true to search up the tree, false to return the
  213. /// current level.
  214. ///
  215. /// \return The effective severity level of the logger. This is the same
  216. /// as getSeverity() if the logger has a severity level set, but otherwise
  217. /// is the severity of the parent.
  218. isc::log::Severity getSeverityCommon(const log4cxx::LoggerPtr& ptrlogger,
  219. bool check_parent);
  220. /// \brief Initialize log4cxx Logger
  221. ///
  222. /// Creates the log4cxx logger used internally. A function is provided for
  223. /// this so that the creation does not take place when this Logger object
  224. /// is created but when it is used. As the latter occurs in executable
  225. /// code but the former can occur during initialization, this order
  226. /// guarantees that anything that is statically initialized has completed
  227. /// its initialization by the time the logger is used.
  228. void initLogger();
  229. /// \brief Return underlying log4cxx logger, initializing it if necessary
  230. ///
  231. /// \return Loggerptr object
  232. log4cxx::LoggerPtr& getLogger() {
  233. if (loggerptr_ == NULL) {
  234. initLogger();
  235. }
  236. return *loggerptr_;
  237. }
  238. // Members. Note that loggerptr_ is a pointer to a LoggerPtr, which is
  239. // itself a pointer to the underlying log4cxx logger. This is due to the
  240. // problems with memory deletion on program exit, explained in the comments
  241. // for the "exit_delete" parameter in this class's constructor.
  242. mutable log4cxx::LoggerPtr* loggerptr_; ///< Pointer to the underlying logger
  243. std::string name_; ///< Name of this logger]
  244. bool exit_delete_; ///< Delete loggerptr_ on exit?
  245. // NOTE - THIS IS A PLACE HOLDER
  246. static bool init_; ///< Set true when initialized
  247. };
  248. } // namespace log
  249. } // namespace isc
  250. #endif // __LOGGER_IMPL_H