logger.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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_H
  16. #define __LOGGER_H
  17. #include <cstdlib>
  18. #include <string>
  19. #include <log/dbglevels.h>
  20. #include <log/logger_levels.h>
  21. #include <log/message_types.h>
  22. namespace isc {
  23. namespace log {
  24. /// \brief Logging API
  25. ///
  26. /// This module forms the interface into the logging subsystem. Features of the
  27. /// system and its implementation are:
  28. ///
  29. /// # Multiple logging objects can be created, each given a name; those with the
  30. /// same name share characteristics (like destination, level being logged
  31. /// etc.)
  32. /// # Messages can be logged at severity levels of FATAL, ERROR, WARN, INFO or
  33. /// DEBUG. The DEBUG level has further sub-levels numbered 0 (least
  34. /// informative) to 99 (most informative).
  35. /// # Each logger has a severity level set associated with it. When a message
  36. /// is logged, it is output only if it is logged at a level equal to the
  37. /// logger severity level or greater, e.g. if the logger's severity is WARN,
  38. /// only messages logged at WARN, ERROR or FATAL will be output.
  39. /// # Messages are identified by message identifiers, which are keys into a
  40. /// message dictionary.
  41. class LoggerImpl; // Forward declaration of the implementation class
  42. class Logger {
  43. public:
  44. /// \brief Constructor
  45. ///
  46. /// Creates/attaches to a logger of a specific name.
  47. ///
  48. /// \param name Name of the logger. If the name is that of the root name,
  49. /// this creates an instance of the root logger; otherwise it creates a
  50. /// child of the root logger.
  51. ///
  52. /// \param infunc This argument is present to get round a bug in some
  53. /// implementations of the logging system. If the logger is declared in
  54. /// a function (such that it will be deleted when the function exits,
  55. /// before the program ends), set this true. If declared outside a
  56. /// function (such that it gets deleted during program rundown), set false
  57. /// (the default).\n
  58. /// \n
  59. /// The problems encountered was that during program rundown, one logging
  60. /// implementation (log4cxx) threw a MutexException (this is described in
  61. /// https://issues.apache.org/jira/browse/LOGCXX-322). As this only occurs
  62. /// during program rundown, the issue is not serious - it just looks bad to
  63. /// have the program crash instead of shut down cleanly.\n
  64. /// \n
  65. /// If log4cxx is chosen as the implementation, this flag controls the
  66. /// deletion of the underlying log4cxx data structures when the logger is
  67. /// deleted. Setting it false for externally-declared loggers inhibits
  68. /// their deletion; so at program exit the memory is not reclaimed during
  69. /// program rundown, only when the process is delected. Setting it true
  70. /// for loggers that will be deleted in the normal running of the program
  71. /// enables their deletion - which causes no issues as the problem only
  72. /// manifests itself during program rundown.
  73. /// \n
  74. /// The falg has no effect on non-log4cxx implementations.
  75. Logger(const std::string& name, bool infunc = false);
  76. /// \brief Destructor
  77. virtual ~Logger();
  78. /// \brief Get Name of Logger
  79. ///
  80. /// \return The full name of the logger (including the root name)
  81. virtual std::string getName();
  82. /// \brief Set Severity Level for Logger
  83. ///
  84. /// Sets the level at which this logger will log messages. If none is set,
  85. /// the level is inherited from the parent.
  86. ///
  87. /// \param severity Severity level to log
  88. /// \param dbglevel If the severity is DEBUG, this is the debug level.
  89. /// This can be in the range 1 to 100 and controls the verbosity. A value
  90. /// outside these limits is silently coerced to the nearest boundary.
  91. virtual void setSeverity(isc::log::Severity severity, int dbglevel = 1);
  92. /// \brief Get Severity Level for Logger
  93. ///
  94. /// \return The current logging level of this logger. In most cases though,
  95. /// the effective logging level is what is required.
  96. virtual isc::log::Severity getSeverity();
  97. /// \brief Get Effective Severity Level for Logger
  98. ///
  99. /// \return The effective severity level of the logger. This is the same
  100. /// as getSeverity() if the logger has a severity level set, but otherwise
  101. /// is the severity of the parent.
  102. virtual isc::log::Severity getEffectiveSeverity();
  103. /// \brief Return DEBUG Level
  104. ///
  105. /// \return Current setting of debug level. This is returned regardless of
  106. /// whether the
  107. virtual int getDebugLevel();
  108. /// \brief Returns if Debug Message Should Be Output
  109. ///
  110. /// \param dbglevel Level for which debugging is checked. Debugging is
  111. /// enabled only if the logger has DEBUG enabled and if the dbglevel
  112. /// checked is less than or equal to the debug level set for the logger.
  113. virtual bool isDebugEnabled(int dbglevel = MIN_DEBUG_LEVEL);
  114. /// \brief Is INFO Enabled?
  115. virtual bool isInfoEnabled();
  116. /// \brief Is WARNING Enabled?
  117. virtual bool isWarnEnabled();
  118. /// \brief Is ERROR Enabled?
  119. virtual bool isErrorEnabled();
  120. /// \brief Is FATAL Enabled?
  121. virtual bool isFatalEnabled();
  122. /// \brief Output Debug Message
  123. ///
  124. /// \param dbglevel Debug level, ranging between 0 and 99. Higher numbers
  125. /// are used for more verbose output.
  126. /// \param ident Message identification.
  127. /// \param ... Optional arguments for the message.
  128. void debug(int dbglevel, MessageID ident, ...);
  129. /// \brief Output Informational Message
  130. ///
  131. /// \param ident Message identification.
  132. /// \param ... Optional arguments for the message.
  133. void info(MessageID ident, ...);
  134. /// \brief Output Warning Message
  135. ///
  136. /// \param ident Message identification.
  137. /// \param ... Optional arguments for the message.
  138. void warn(MessageID ident, ...);
  139. /// \brief Output Error Message
  140. ///
  141. /// \param ident Message identification.
  142. /// \param ... Optional arguments for the message.
  143. void error(MessageID ident, ...);
  144. /// \brief Output Fatal Message
  145. ///
  146. /// \param ident Message identification.
  147. /// \param ... Optional arguments for the message.
  148. void fatal(MessageID ident, ...);
  149. protected:
  150. /// \brief Equality
  151. ///
  152. /// Check if two instances of this logger refer to the same stream.
  153. /// (This method is principally for testing.)
  154. ///
  155. /// \return true if the logger objects are instances of the same logger.
  156. bool operator==(const Logger& other);
  157. /// \brief Logger Initialized
  158. ///
  159. /// Check that the logger has been properly initialized. (This method
  160. /// is principally for testing.)
  161. ///
  162. /// \return true if this logger object has been initialized.
  163. bool isInitialized();
  164. private:
  165. LoggerImpl* loggerptr_; /// Pointer to the underlying logger
  166. };
  167. } // namespace log
  168. } // namespace isc
  169. #endif // __LOGGER_H