logger_impl.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright (C) 2011 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. #ifndef __LOGGER_IMPL_H
  15. #define __LOGGER_IMPL_H
  16. #include <stdarg.h>
  17. #include <time.h>
  18. #include <iostream>
  19. #include <cstdlib>
  20. #include <string>
  21. #include <map>
  22. #include <utility>
  23. // log4cplus logger header file
  24. #include <log4cplus/logger.h>
  25. // BIND-10 logger files
  26. #include <log/logger_level_impl.h>
  27. #include <log/message_types.h>
  28. namespace isc {
  29. namespace log {
  30. /// \brief Console Logger Implementation
  31. ///
  32. /// The logger uses a "pimpl" idiom for implementation, where the base logger
  33. /// class contains little more than a pointer to the implementation class, and
  34. /// all actions are carried out by the latter.
  35. ///
  36. /// This particular implementation is based on log4cplus (from sourceforge:
  37. /// http://log4cplus.sourceforge.net). Particular items of note:
  38. ///
  39. /// a) BIND 10 loggers have names of the form "program.sublogger". In other
  40. /// words, each of the loggers is a sub-logger of the main program logger.
  41. /// In log4cplus, there is a root logger (called "root" according to the
  42. /// documentation, but actually unnamed) and all loggers created are subloggers
  43. /// if it.
  44. ///
  45. /// In this implementation, the name of the logger is checked. If it is the
  46. /// name of the program (as set in the call to isc::log::setRootLoggerName),
  47. /// the log4cplus root logger is used. Otherwise the name passed is used as
  48. /// the name of a logger when a log4cplus logger is created.
  49. ///
  50. /// To clarify: if the program is "b10auth" (and that is used to set the BIND 10
  51. /// root logger name via a call to isc::log::setRootLoggerName()), the BIND 10
  52. /// logger "b10auth" corresponds to the log4cplus root logger instance (returned
  53. /// by a call to log4cplus::Logger::getRoot()). The BIND 10 sub-logger "cache"
  54. /// corresponds to the log4cplus logger "cache", created by a call to
  55. /// log4cplus::Logger::getInstance("cache"). The distinction is, however,
  56. /// invisible to users as the logger reported in messages is always
  57. /// "programm.sublogger".
  58. ///
  59. /// b) The idea of debug levels is implemented. Seee logger_level.h and
  60. /// logger_level_impl.h for more details on this.
  61. class LoggerImpl {
  62. public:
  63. /// \brief Constructor
  64. ///
  65. /// Creates a logger of the specific name.
  66. ///
  67. /// \param name Name of the logger.
  68. LoggerImpl(const std::string& name);
  69. /// \brief Destructor
  70. virtual ~LoggerImpl();
  71. /// \brief Get the full name of the logger (including the root name)
  72. virtual std::string getName() {
  73. return (name_);
  74. }
  75. /// \brief Set Severity Level for Logger
  76. ///
  77. /// Sets the level at which this logger will log messages. If none is set,
  78. /// the level is inherited from the parent.
  79. ///
  80. /// \param severity Severity level to log
  81. /// \param dbglevel If the severity is DEBUG, this is the debug level.
  82. /// This can be in the range 0 to 99 and controls the verbosity. A value
  83. /// outside these limits is silently coerced to the nearest boundary.
  84. virtual void setSeverity(Severity severity, int dbglevel = 1);
  85. /// \brief Get Severity Level for Logger
  86. ///
  87. /// \return The current logging level of this logger. In most cases though,
  88. /// the effective logging level is what is required.
  89. virtual Severity getSeverity();
  90. /// \brief Get Effective Severity Level for Logger
  91. ///
  92. /// \return The effective severity level of the logger. This is the same
  93. /// as getSeverity() if the logger has a severity level set, but otherwise
  94. /// is the severity of the parent.
  95. virtual Severity getEffectiveSeverity();
  96. /// \brief Return debug level
  97. ///
  98. /// \return Current setting of debug level. This will be zero if the
  99. /// the current severity level is not DEBUG.
  100. virtual int getDebugLevel();
  101. /// \brief Return effective debug level
  102. ///
  103. /// \return Current setting of effective debug level. This will be zero if
  104. /// the current effective severity level is not DEBUG.
  105. virtual int getEffectiveDebugLevel();
  106. /// \brief Returns if Debug Message Should Be Output
  107. ///
  108. /// \param dbglevel Level for which debugging is checked. Debugging is
  109. /// enabled only if the logger has DEBUG enabled and if the dbglevel
  110. /// checked is less than or equal to the debug level set for the logger.
  111. virtual bool isDebugEnabled(int dbglevel = MIN_DEBUG_LEVEL) {
  112. Level level(DEBUG, dbglevel);
  113. return logger_.isEnabledFor(LoggerLevelImpl::convertFromBindLevel(level));
  114. }
  115. /// \brief Is INFO Enabled?
  116. virtual bool isInfoEnabled() {
  117. return (logger_.isEnabledFor(log4cplus::INFO_LOG_LEVEL));
  118. }
  119. /// \brief Is WARNING Enabled?
  120. virtual bool isWarnEnabled() {
  121. return (logger_.isEnabledFor(log4cplus::WARN_LOG_LEVEL));
  122. }
  123. /// \brief Is ERROR Enabled?
  124. virtual bool isErrorEnabled() {
  125. return (logger_.isEnabledFor(log4cplus::ERROR_LOG_LEVEL));
  126. }
  127. /// \brief Is FATAL Enabled?
  128. virtual bool isFatalEnabled() {
  129. return (logger_.isEnabledFor(log4cplus::FATAL_LOG_LEVEL));
  130. }
  131. /// \brief Raw output
  132. ///
  133. /// Writes the message with time into the log. Used by the Formatter
  134. /// to produce output.
  135. ///
  136. /// \param severity Severity of the message. (This controls the prefix
  137. /// label output with the message text.)
  138. /// \param message Text of the message.
  139. void outputRaw(const Severity& severity, const std::string& message);
  140. /// \brief Look up message text in dictionary
  141. ///
  142. /// This gets you the unformatted text of message for given ID.
  143. std::string* lookupMessage(const MessageID& id);
  144. /// \brief Equality
  145. ///
  146. /// Check if two instances of this logger refer to the same stream.
  147. /// (This method is principally for testing.)
  148. ///
  149. /// \return true if the logger objects are instances of the same logger.
  150. bool operator==(const LoggerImpl& other) {
  151. return (name_ == other.name_);
  152. }
  153. /// \brief Reset logging
  154. ///
  155. /// Resets (clears) the log4cplus logging, requiring that an initialization
  156. /// call be performed again.
  157. static void reset();
  158. private:
  159. std::string name_; ///< Full name of this logger
  160. log4cplus::Logger logger_; ///< Underlying log4cplus logger
  161. };
  162. } // namespace log
  163. } // namespace isc
  164. #endif // __LOGGER_IMPL_H