logger_impl.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #include <iostream>
  15. #include <iomanip>
  16. #include <algorithm>
  17. #include <stdarg.h>
  18. #include <stdio.h>
  19. #include <boost/lexical_cast.hpp>
  20. #include <boost/static_assert.hpp>
  21. #include <log4cplus/configurator.h>
  22. #include <log/debug_levels.h>
  23. #include <log/root_logger_name.h>
  24. #include <log/logger.h>
  25. #include <log/logger_levels.h>
  26. #include <log/logger_impl.h>
  27. #include <log/message_dictionary.h>
  28. #include <log/message_types.h>
  29. #include <log/root_logger_name.h>
  30. #include <util/strutil.h>
  31. using namespace std;
  32. namespace isc {
  33. namespace log {
  34. // Static initializations
  35. // Constructor
  36. LoggerImpl::LoggerImpl(const std::string& name, bool)
  37. {
  38. // Are we the root logger?
  39. if (name == getRootLoggerName()) {
  40. is_root_ = true;
  41. name_ = name;
  42. } else {
  43. is_root_ = false;
  44. name_ = getRootLoggerName() + "." + name;
  45. }
  46. fmt_name_ = std::string("[") + name_ + std::string("] ");
  47. // Initialize log4cplus if not already done
  48. initLog4cplus();
  49. // Return existing instance of logger, creating it if it does not exist.
  50. logger_ = log4cplus::Logger::getInstance(name_);
  51. }
  52. // Destructor. (Here because of virtual declaration.)
  53. LoggerImpl::~LoggerImpl() {
  54. }
  55. // Set the severity for logging.
  56. // TODO IGNORE DEBUG LEVEL FOR NOW
  57. void
  58. LoggerImpl::setSeverity(isc::log::Severity severity, int /*dbglevel*/) {
  59. // Silently coerce the debug level into the valid range of 0 to 99
  60. //int debug_level = max(MIN_DEBUG_LEVEL, min(MAX_DEBUG_LEVEL, dbglevel));
  61. logger_.setLogLevel(convertFromBindSeverity(severity));
  62. }
  63. // Return severity level
  64. isc::log::Severity
  65. LoggerImpl::getSeverity() {
  66. return convertToBindSeverity(logger_.getLogLevel());
  67. }
  68. // Get effective severity. Either the current severity or, if not set, the
  69. // severity of the root level.
  70. isc::log::Severity
  71. LoggerImpl::getEffectiveSeverity() {
  72. return convertToBindSeverity(logger_.getChainedLogLevel());
  73. }
  74. // Get the debug level. This returns 0 unless the severity is DEBUG.
  75. // TODO: DEBUG LEVEL IGNORED FOR NOW
  76. int
  77. LoggerImpl::getDebugLevel() {
  78. return (0);
  79. }
  80. // The code for isXxxEnabled is quite simple and is in the header. The only
  81. // exception is isDebugEnabled() where we have the complication of the debug
  82. // levels.
  83. // TODO: DEBUG LEVEL IGNORED FOR NOW
  84. bool
  85. LoggerImpl::isDebugEnabled(int dbglevel) {
  86. return logger_.isEnabledFor(log4cplus::DEBUG_LOG_LEVEL);
  87. }
  88. // Output a general message
  89. string*
  90. LoggerImpl::lookupMessage(const MessageID& ident) {
  91. return (new string(string(ident) + ", " +
  92. MessageDictionary::globalDictionary().getText(ident)));
  93. }
  94. void
  95. LoggerImpl::outputRaw(const Severity& severity, const string& message) {
  96. switch (severity) {
  97. case DEBUG:
  98. LOG4CPLUS_DEBUG(logger_, fmt_name_ << message);
  99. break;
  100. case INFO:
  101. LOG4CPLUS_INFO(logger_, fmt_name_ << message);
  102. break;
  103. case WARN:
  104. LOG4CPLUS_WARN(logger_, fmt_name_ << message);
  105. break;
  106. case ERROR:
  107. LOG4CPLUS_ERROR(logger_, fmt_name_ << message);
  108. break;
  109. case FATAL:
  110. LOG4CPLUS_FATAL(logger_, fmt_name_ << message);
  111. }
  112. }
  113. // Convert levels - from BIND 10 level to log4cplus level.
  114. // Namespaces explicitly used to clarify what level we are talking about
  115. log4cplus::LogLevel
  116. LoggerImpl::convertFromBindSeverity(const isc::log::Severity& inlevel) {
  117. // BIND 10 logging levels are small integers so we can do a table lookup
  118. static const log4cplus::LogLevel level[] = {
  119. log4cplus::NOT_SET_LOG_LEVEL,
  120. log4cplus::DEBUG_LOG_LEVEL,
  121. log4cplus::INFO_LOG_LEVEL,
  122. log4cplus::WARN_LOG_LEVEL,
  123. log4cplus::ERROR_LOG_LEVEL,
  124. log4cplus::FATAL_LOG_LEVEL,
  125. log4cplus::OFF_LOG_LEVEL
  126. };
  127. // ... with compile-time checks to ensure that table indexes are correct.
  128. BOOST_STATIC_ASSERT(static_cast<int>(isc::log::DEFAULT) == 0);
  129. BOOST_STATIC_ASSERT(static_cast<int>(isc::log::DEBUG) == 1);
  130. BOOST_STATIC_ASSERT(static_cast<int>(isc::log::INFO) == 2);
  131. BOOST_STATIC_ASSERT(static_cast<int>(isc::log::WARN) == 3);
  132. BOOST_STATIC_ASSERT(static_cast<int>(isc::log::ERROR) == 4);
  133. BOOST_STATIC_ASSERT(static_cast<int>(isc::log::FATAL) == 5);
  134. BOOST_STATIC_ASSERT(static_cast<int>(isc::log::NONE) == 6);
  135. // No need to check that the iundex is out of range. Setting the type of
  136. // the argument to isc::log::Severity ensures that it must be one of the
  137. // Severity enum members - conversion of a numeric value to an enum is not
  138. // permitted.
  139. return (level[inlevel]);
  140. }
  141. // Convert levels - from log4plus level to BIND 10 level
  142. // Namespaces explicitly used to clarify what level we are talking about
  143. isc::log::Severity
  144. LoggerImpl::convertToBindSeverity(const log4cplus::LogLevel& inlevel) {
  145. // Not easy to do a table lookup as the numerical values of log4cplus levels
  146. // are quite high.
  147. switch (inlevel) {
  148. case log4cplus::NOT_SET_LOG_LEVEL:
  149. return (isc::log::DEFAULT);
  150. case log4cplus::DEBUG_LOG_LEVEL:
  151. return (isc::log::DEBUG);
  152. case log4cplus::INFO_LOG_LEVEL:
  153. return (isc::log::INFO);
  154. case log4cplus::WARN_LOG_LEVEL:
  155. return (isc::log::WARN);
  156. case log4cplus::ERROR_LOG_LEVEL:
  157. return (isc::log::ERROR);
  158. case log4cplus::FATAL_LOG_LEVEL:
  159. return (isc::log::FATAL);
  160. }
  161. return (isc::log::NONE);
  162. }
  163. // One-time initialization of log4cplus
  164. void
  165. LoggerImpl::initLog4cplus() {
  166. static bool not_initialized = true;
  167. if (not_initialized) {
  168. log4cplus::BasicConfigurator config;
  169. config.configure();
  170. not_initialized = false;
  171. }
  172. }
  173. } // namespace log
  174. } // namespace isc