logger.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_H
  15. #define __LOGGER_H
  16. #include <cstdlib>
  17. #include <string>
  18. #include <log/logger_level.h>
  19. #include <log/message_types.h>
  20. #include <log/log_formatter.h>
  21. namespace isc {
  22. namespace log {
  23. /// \page LoggingApi Logging API
  24. /// \section LoggingApiOverview Overview
  25. /// BIND 10 logging uses the concepts of the widely-used Java logging
  26. /// package log4j (http://logging.apache.log/log4j), albeit implemented
  27. /// in C++ using an open-source port. Features of the system are:
  28. ///
  29. /// - Within the code objects - known as loggers - can be created and
  30. /// used to log messages. These loggers have names; those with the
  31. /// same name share characteristics (such as output destination).
  32. /// - Loggers have a hierarchical relationship, with each logger being
  33. /// the child of another logger, except for the top of the hierarchy, the
  34. /// root logger. If a logger does not log a message, it is passed to the
  35. /// parent logger.
  36. /// - Messages can be logged at severity levels of FATAL, ERROR, WARN, INFO
  37. /// or DEBUG. The DEBUG level has further sub-levels numbered 0 (least
  38. /// informative) to 99 (most informative).
  39. /// - Each logger has a severity level set associated with it. When a
  40. /// message is logged, it is output only if it is logged at a level equal
  41. /// to the logger severity level or greater, e.g. if the logger's severity
  42. /// is WARN, only messages logged at WARN, ERROR or FATAL will be output.
  43. ///
  44. /// \section LoggingApiLoggerNames BIND 10 Logger Names
  45. /// Within BIND 10, the root logger root logger is given the name of the
  46. /// program (via the stand-along function setRootLoggerName()). Other loggers
  47. /// are children of the root logger and are named "<program>.<sublogger>".
  48. /// This name appears in logging output, allowing users to identify both
  49. /// the BIND 10 program and the component within the program that generated
  50. /// the message.
  51. ///
  52. /// When creating a logger, the abbreviated name "<sublogger>" can be used;
  53. /// the program name will be prepended to it when the logger is created.
  54. /// In this way, individual libraries can have their own loggers without
  55. /// worrying about the program in which they are used, but:
  56. /// - The origin of the message will be clearly identified.
  57. /// - The same component can have different options (e.g. logging severity)
  58. /// in different programs at the same time.
  59. ///
  60. /// \section LoggingApiLoggingMessages Logging Messages
  61. /// Instead of embedding the text of messages within the code, each message
  62. /// is referred to using a symbolic name. The logging code uses this name as
  63. /// a key in a dictionary from which the message text is obtained. Such a
  64. /// system allows for the optional replacement of message text at run time.
  65. /// More details about the message disction (and the compiler used to create
  66. /// the symbol definitions) can be found in other modules in the src/lib/log
  67. /// directory.
  68. class LoggerImpl; // Forward declaration of the implementation class
  69. /// \brief Logger Class
  70. ///
  71. /// This class is the main class used for logging. Use comprises:
  72. ///
  73. /// 1. Constructing a logger by instantiating it with a specific name. As
  74. /// well as instantiating it when needed, the logger can also be declared
  75. /// outside a program using.
  76. ///
  77. /// 2. Using the error(), info() etc. methods to log an error. (Although it is
  78. /// recommended to use the LOG_ERROR, LOG_INFO etc. macros defined in macros.h.
  79. /// These will avoid the potentially-expensive evaluation of arguments if the
  80. /// severity is such that the message will be suppressed.)
  81. class Logger {
  82. public:
  83. /// \brief Constructor
  84. ///
  85. /// Creates/attaches to a logger of a specific name.
  86. ///
  87. /// \param name Name of the logger. If the name is that of the root name,
  88. /// this creates an instance of the root logger; otherwise it creates a
  89. /// child of the root logger.
  90. Logger(const std::string& name) : loggerptr_(NULL), name_(name)
  91. {}
  92. /// \brief Destructor
  93. virtual ~Logger();
  94. /// \brief The formatter used to replace placeholders
  95. typedef isc::log::Formatter<Logger> Formatter;
  96. /// \brief Get Name of Logger
  97. ///
  98. /// \return The full name of the logger (including the root name)
  99. virtual std::string getName();
  100. /// \brief Set Severity Level for Logger
  101. ///
  102. /// Sets the level at which this logger will log messages. If none is set,
  103. /// the level is inherited from the parent.
  104. ///
  105. /// \param severity Severity level to log
  106. /// \param dbglevel If the severity is DEBUG, this is the debug level.
  107. /// This can be in the range 1 to 100 and controls the verbosity. A value
  108. /// outside these limits is silently coerced to the nearest boundary.
  109. virtual void setSeverity(isc::log::Severity severity, int dbglevel = 1);
  110. /// \brief Get Severity Level for Logger
  111. ///
  112. /// \return The current logging level of this logger. In most cases though,
  113. /// the effective logging level is what is required.
  114. virtual isc::log::Severity getSeverity();
  115. /// \brief Get Effective Severity Level for Logger
  116. ///
  117. /// \return The effective severity level of the logger. This is the same
  118. /// as getSeverity() if the logger has a severity level set, but otherwise
  119. /// is the severity of the parent.
  120. virtual isc::log::Severity getEffectiveSeverity();
  121. /// \brief Return DEBUG Level
  122. ///
  123. /// \return Current setting of debug level. This is returned regardless of
  124. /// whether the severity is set to debug.
  125. virtual int getDebugLevel();
  126. /// \brief Returns if Debug Message Should Be Output
  127. ///
  128. /// \param dbglevel Level for which debugging is checked. Debugging is
  129. /// enabled only if the logger has DEBUG enabled and if the dbglevel
  130. /// checked is less than or equal to the debug level set for the logger.
  131. virtual bool isDebugEnabled(int dbglevel = MIN_DEBUG_LEVEL);
  132. /// \brief Is INFO Enabled?
  133. virtual bool isInfoEnabled();
  134. /// \brief Is WARNING Enabled?
  135. virtual bool isWarnEnabled();
  136. /// \brief Is ERROR Enabled?
  137. virtual bool isErrorEnabled();
  138. /// \brief Is FATAL Enabled?
  139. virtual bool isFatalEnabled();
  140. /// \brief Output Debug Message
  141. ///
  142. /// \param dbglevel Debug level, ranging between 0 and 99. Higher numbers
  143. /// are used for more verbose output.
  144. /// \param ident Message identification.
  145. Formatter debug(int dbglevel, const MessageID& ident);
  146. /// \brief Output Informational Message
  147. ///
  148. /// \param ident Message identification.
  149. Formatter info(const MessageID& ident);
  150. /// \brief Output Warning Message
  151. ///
  152. /// \param ident Message identification.
  153. Formatter warn(const MessageID& ident);
  154. /// \brief Output Error Message
  155. ///
  156. /// \param ident Message identification.
  157. Formatter error(const MessageID& ident);
  158. /// \brief Output Fatal Message
  159. ///
  160. /// \param ident Message identification.
  161. Formatter fatal(const MessageID& ident);
  162. /// \brief Equality
  163. ///
  164. /// Check if two instances of this logger refer to the same stream.
  165. ///
  166. /// \return true if the logger objects are instances of the same logger.
  167. bool operator==(Logger& other);
  168. private:
  169. friend class isc::log::Formatter<Logger>;
  170. /// \brief Raw output function
  171. ///
  172. /// This is used by the formatter to output formatted output.
  173. ///
  174. /// \param severity Severity of the message being output.
  175. /// \param message Text of the message to be output.
  176. void output(const Severity& severity, const std::string& message);
  177. /// \brief Copy Constructor
  178. ///
  179. /// Disabled (marked private) as it makes no sense to copy the logger -
  180. /// just create another one of the same name.
  181. Logger(const Logger&);
  182. /// \brief Assignment Operator
  183. ///
  184. /// Disabled (marked private) as it makes no sense to copy the logger -
  185. /// just create another one of the same name.
  186. Logger& operator=(const Logger&);
  187. /// \brief Initialize Implementation
  188. ///
  189. /// Returns the logger pointer. If not yet set, the underlying
  190. /// implementation class is initialized.\n
  191. /// \n
  192. /// The reason for this indirection is to avoid the "static initialization
  193. /// fiacso", whereby we cannot rely on the order of static initializations.
  194. /// The main problem is the root logger name - declared statically - which
  195. /// is referenced by various loggers. By deferring a reference to it until
  196. /// after the program starts executing - by which time the root name object
  197. /// will be initialized - we avoid this problem.
  198. ///
  199. /// \return Returns pointer to implementation
  200. LoggerImpl* getLoggerPtr() {
  201. if (!loggerptr_) {
  202. initLoggerImpl();
  203. }
  204. return (loggerptr_);
  205. }
  206. /// \brief Initialize Underlying Implementation and Set loggerptr_
  207. void initLoggerImpl();
  208. LoggerImpl* loggerptr_; ///< Pointer to the underlying logger
  209. std::string name_; ///< Copy of the logger name
  210. };
  211. } // namespace log
  212. } // namespace isc
  213. #endif // __LOGGER_H