logger_impl.h 8.6 KB

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