logger_impl.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 <cstdlib>
  19. #include <string>
  20. #include <map>
  21. #include <utility>
  22. #include <log/debug_levels.h>
  23. #include <log/logger.h>
  24. #include <log/message_types.h>
  25. #include <log/root_logger_name.h>
  26. namespace isc {
  27. namespace log {
  28. /// \brief Console Logger Implementation
  29. ///
  30. /// The logger uses a "pimpl" idiom for implementation, where the base logger
  31. /// class contains little more than a pointer to the implementation class, and
  32. /// all actions are carried out by the latter. This class is an implementation
  33. /// class that just outputs to stdout.
  34. class LoggerImpl {
  35. public:
  36. /// \brief Information About Logger
  37. ///
  38. /// Holds a information about a logger, namely its severity and its debug
  39. /// level. This could be a std::pair, except that it gets confusing when
  40. /// accessing the LoggerInfoMap: that returns a pair, so we to reference
  41. /// elements we would use constructs like ((i->first).second);
  42. struct LoggerInfo {
  43. isc::log::Severity severity;
  44. int dbglevel;
  45. LoggerInfo(isc::log::Severity sev = isc::log::INFO,
  46. int dbg = MIN_DEBUG_LEVEL) : severity(sev), dbglevel(dbg)
  47. {}
  48. };
  49. /// \brief Information About All Loggers
  50. ///
  51. /// Information about all loggers in the system - except the root logger -
  52. /// is held in a map, linking name of the logger (excluding the root
  53. /// name component) and its set severity and debug levels. The root
  54. /// logger information is held separately.
  55. typedef std::map<std::string, LoggerInfo> LoggerInfoMap;
  56. /// \brief Constructor
  57. ///
  58. /// Creates a logger of the specific name.
  59. ///
  60. /// \param name Name of the logger.
  61. ///
  62. /// \param exit_delete This argument is present to get round a bug in
  63. /// the log4cxx implementation. It is unused here.
  64. LoggerImpl(const std::string& name, bool);
  65. /// \brief Destructor
  66. virtual ~LoggerImpl();
  67. /// \brief Get the full name of the logger (including the root name)
  68. virtual std::string getName() {
  69. return (name_);
  70. }
  71. /// \brief Set Severity Level for Logger
  72. ///
  73. /// Sets the level at which this logger will log messages. If none is set,
  74. /// the level is inherited from the parent.
  75. ///
  76. /// \param severity Severity level to log
  77. /// \param dbglevel If the severity is DEBUG, this is the debug level.
  78. /// This can be in the range 1 to 100 and controls the verbosity. A value
  79. /// outside these limits is silently coerced to the nearest boundary.
  80. virtual void setSeverity(isc::log::Severity severity, int dbglevel = 1);
  81. /// \brief Get Severity Level for Logger
  82. ///
  83. /// \return The current logging level of this logger. In most cases though,
  84. /// the effective logging level is what is required.
  85. virtual isc::log::Severity getSeverity();
  86. /// \brief Get Effective Severity Level for Logger
  87. ///
  88. /// \return The effective severity level of the logger. This is the same
  89. /// as getSeverity() if the logger has a severity level set, but otherwise
  90. /// is the severity of the parent.
  91. virtual isc::log::Severity getEffectiveSeverity();
  92. /// \brief Return DEBUG Level
  93. ///
  94. /// \return Current setting of debug level. This is returned regardless of
  95. /// whether the
  96. virtual int getDebugLevel();
  97. /// \brief Returns if Debug Message Should Be Output
  98. ///
  99. /// \param dbglevel Level for which debugging is checked. Debugging is
  100. /// enabled only if the logger has DEBUG enabled and if the dbglevel
  101. /// checked is less than or equal to the debug level set for the logger.
  102. virtual bool
  103. isDebugEnabled(int dbglevel = MIN_DEBUG_LEVEL);
  104. /// \brief Is INFO Enabled?
  105. virtual bool isInfoEnabled() {
  106. return (isEnabled(isc::log::INFO));
  107. }
  108. /// \brief Is WARNING Enabled?
  109. virtual bool isWarnEnabled() {
  110. return (isEnabled(isc::log::WARN));
  111. }
  112. /// \brief Is ERROR Enabled?
  113. virtual bool isErrorEnabled() {
  114. return (isEnabled(isc::log::ERROR));
  115. }
  116. /// \brief Is FATAL Enabled?
  117. virtual bool isFatalEnabled() {
  118. return (isEnabled(isc::log::FATAL));
  119. }
  120. /// \brief Common Severity check
  121. ///
  122. /// Implements the common severity check. As an optimisation, this checks
  123. /// to see if any logger-specific levels have been set (a quick check as it
  124. /// just involves seeing if the collection of logger information is empty).
  125. /// if not, it returns the information for the root level; if so, it has
  126. /// to take longer and look up the information in the map holding the
  127. /// logging details.
  128. virtual bool isEnabled(isc::log::Severity severity) {
  129. if (logger_info_.empty()) {
  130. return (root_logger_info_.severity <= severity);
  131. }
  132. else {
  133. return (getSeverity() <= severity);
  134. }
  135. }
  136. /// \brief Raw output
  137. ///
  138. /// Writes the message with time into the log. Used by the Formatter
  139. /// to produce output.
  140. void outputRaw(const char* sev_text, const std::string& message);
  141. /// \brief Look up message text in dictionary
  142. ///
  143. /// This gets you the unformatted text of message for given ID.
  144. std::string* lookupMessage(const MessageID& id);
  145. /// \brief Equality
  146. ///
  147. /// Check if two instances of this logger refer to the same stream.
  148. /// (This method is principally for testing.)
  149. ///
  150. /// \return true if the logger objects are instances of the same logger.
  151. bool operator==(const LoggerImpl& other) {
  152. return (name_ == other.name_);
  153. }
  154. /// \brief Reset Global Data
  155. ///
  156. /// Only used for testing, this clears all the logger information and
  157. /// resets it back to default values.
  158. static void reset() {
  159. root_logger_info_ = LoggerInfo(isc::log::INFO, MIN_DEBUG_LEVEL);
  160. logger_info_.clear();
  161. }
  162. private:
  163. bool is_root_; ///< true if a root logger
  164. std::string name_; ///< Name of this logger
  165. // Split the status of the root logger from this logger. If - is will
  166. // probably be the usual case - no per-logger setting is enabled, a
  167. // quick check of logger_info_.empty() will return true and we can quickly
  168. // return the root logger status without a length lookup in the map.
  169. static LoggerInfo root_logger_info_; ///< Status of root logger
  170. static LoggerInfoMap logger_info_; ///< Store of debug levels etc.
  171. };
  172. } // namespace log
  173. } // namespace isc
  174. #endif // __LOGGER_IMPL_H