logger.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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-alone 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 736#comment:12(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. (If the
  74. /// same logger is in multiple functions within a file, overhead can be
  75. /// minimised by declaring it as a file-wide static variable.)
  76. /// 2. Using the error(), info() etc. methods to log an error. (However, it is
  77. /// recommended to use the LOG_ERROR, LOG_INFO etc. macros defined in macros.h.
  78. /// These will avoid the potentially-expensive evaluation of arguments if the
  79. /// severity is such that the message will be suppressed.)
  80. class Logger {
  81. public:
  82. /// \brief Constructor
  83. ///
  84. /// Creates/attaches to a logger of a specific name.
  85. ///
  86. /// \param name Name of the logger. If the name is that of the root name,
  87. /// this creates an instance of the root logger; otherwise it creates a
  88. /// child of the root logger.
  89. Logger(const std::string& name) : loggerptr_(NULL), name_(name)
  90. {}
  91. /// \brief Destructor
  92. virtual ~Logger();
  93. /// \brief The formatter used to replace placeholders
  94. typedef isc::log::Formatter<Logger> Formatter;
  95. /// \brief Get Name of Logger
  96. ///
  97. /// \return The full name of the logger (including the root name)
  98. virtual std::string getName();
  99. /// \brief Set Severity Level for Logger
  100. ///
  101. /// Sets the level at which this logger will log messages. If none is set,
  102. /// the level is inherited from the parent.
  103. ///
  104. /// \param severity Severity level to log
  105. /// \param dbglevel If the severity is DEBUG, this is the debug level.
  106. /// This can be in the range 1 to 100 and controls the verbosity. A value
  107. /// outside these limits is silently coerced to the nearest boundary.
  108. virtual void setSeverity(isc::log::Severity severity, int dbglevel = 1);
  109. /// \brief Get Severity Level for Logger
  110. ///
  111. /// \return The current logging level of this logger. In most cases though,
  112. /// the effective logging level is what is required.
  113. virtual isc::log::Severity getSeverity();
  114. /// \brief Get Effective Severity Level for Logger
  115. ///
  116. /// \return The effective severity level of the logger. This is the same
  117. /// as getSeverity() if the logger has a severity level set, but otherwise
  118. /// is the severity of the parent.
  119. virtual isc::log::Severity getEffectiveSeverity();
  120. /// \brief Return DEBUG Level
  121. ///
  122. /// \return Current setting of debug level. This is returned regardless of
  123. /// whether the severity is set to debug.
  124. virtual int getDebugLevel();
  125. /// \brief Get Effective Debug Level for Logger
  126. ///
  127. /// \return The effective debug level of the logger. This is the same
  128. /// as getDebugLevel() if the logger has a debug level set, but otherwise
  129. /// is the debug level of the parent.
  130. virtual int getEffectiveDebugLevel();
  131. /// \brief Returns if Debug Message Should Be Output
  132. ///
  133. /// \param dbglevel Level for which debugging is checked. Debugging is
  134. /// enabled only if the logger has DEBUG enabled and if the dbglevel
  135. /// checked is less than or equal to the debug level set for the logger.
  136. virtual bool isDebugEnabled(int dbglevel = MIN_DEBUG_LEVEL);
  137. /// \brief Is INFO Enabled?
  138. virtual bool isInfoEnabled();
  139. /// \brief Is WARNING Enabled?
  140. virtual bool isWarnEnabled();
  141. /// \brief Is ERROR Enabled?
  142. virtual bool isErrorEnabled();
  143. /// \brief Is FATAL Enabled?
  144. virtual bool isFatalEnabled();
  145. /// \brief Output Debug Message
  146. ///
  147. /// \param dbglevel Debug level, ranging between 0 and 99. Higher numbers
  148. /// are used for more verbose output.
  149. /// \param ident Message identification.
  150. Formatter debug(int dbglevel, const MessageID& ident);
  151. /// \brief Output Informational Message
  152. ///
  153. /// \param ident Message identification.
  154. Formatter info(const MessageID& ident);
  155. /// \brief Output Warning Message
  156. ///
  157. /// \param ident Message identification.
  158. Formatter warn(const MessageID& ident);
  159. /// \brief Output Error Message
  160. ///
  161. /// \param ident Message identification.
  162. Formatter error(const MessageID& ident);
  163. /// \brief Output Fatal Message
  164. ///
  165. /// \param ident Message identification.
  166. Formatter fatal(const MessageID& ident);
  167. /// \brief Equality
  168. ///
  169. /// Check if two instances of this logger refer to the same stream.
  170. ///
  171. /// \return true if the logger objects are instances of the same logger.
  172. bool operator==(Logger& other);
  173. private:
  174. friend class isc::log::Formatter<Logger>;
  175. /// \brief Raw output function
  176. ///
  177. /// This is used by the formatter to output formatted output.
  178. ///
  179. /// \param severity Severity of the message being output.
  180. /// \param message Text of the message to be output.
  181. void output(const Severity& severity, const std::string& message);
  182. /// \brief Copy Constructor
  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(const Logger&);
  187. /// \brief Assignment Operator
  188. ///
  189. /// Disabled (marked private) as it makes no sense to copy the logger -
  190. /// just create another one of the same name.
  191. Logger& operator=(const Logger&);
  192. /// \brief Initialize Implementation
  193. ///
  194. /// Returns the logger pointer. If not yet set, the underlying
  195. /// implementation class is initialized.\n
  196. /// \n
  197. /// The reason for this indirection is to avoid the "static initialization
  198. /// fiacso", whereby we cannot rely on the order of static initializations.
  199. /// The main problem is the root logger name - declared statically - which
  200. /// is referenced by various loggers. By deferring a reference to it until
  201. /// after the program starts executing - by which time the root name object
  202. /// will be initialized - we avoid this problem.
  203. ///
  204. /// \return Returns pointer to implementation
  205. LoggerImpl* getLoggerPtr() {
  206. if (!loggerptr_) {
  207. initLoggerImpl();
  208. }
  209. return (loggerptr_);
  210. }
  211. /// \brief Initialize Underlying Implementation and Set loggerptr_
  212. void initLoggerImpl();
  213. LoggerImpl* loggerptr_; ///< Pointer to the underlying logger
  214. std::string name_; ///< Copy of the logger name
  215. };
  216. } // namespace log
  217. } // namespace isc
  218. #endif // __LOGGER_H