logger_impl.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <iomanip>
  15. #include <algorithm>
  16. #include <stdarg.h>
  17. #include <stdio.h>
  18. #include <boost/lexical_cast.hpp>
  19. #include <boost/static_assert.hpp>
  20. #include <log4cplus/configurator.h>
  21. #include <log/root_logger_name.h>
  22. #include <log/logger.h>
  23. #include <log/logger_level.h>
  24. #include <log/logger_level_impl.h>
  25. #include <log/logger_impl.h>
  26. #include <log/message_dictionary.h>
  27. #include <log/message_types.h>
  28. #include <log/root_logger_name.h>
  29. #include <util/strutil.h>
  30. using namespace std;
  31. using namespace log4cplus;
  32. namespace isc {
  33. namespace log {
  34. // Static initializations
  35. // Constructor
  36. LoggerImpl::LoggerImpl(const std::string& name)
  37. {
  38. // Initialize log4cplus if not already done
  39. initLog4cplus();
  40. // Are we the root logger?
  41. if (name == getRootLoggerName()) {
  42. name_ = name;
  43. logger_ = log4cplus::Logger::getRoot();
  44. } else {
  45. name_ = getRootLoggerName() + "." + name;
  46. logger_ = log4cplus::Logger::getInstance(name_);
  47. }
  48. // Create a formatted name for use in messages (speeds up formatting if
  49. // we do it now.)
  50. fmt_name_ = std::string("[") + name_ + std::string("] ");
  51. }
  52. // Destructor. (Here because of virtual declaration.)
  53. LoggerImpl::~LoggerImpl() {
  54. }
  55. // Set the severity for logging.
  56. void
  57. LoggerImpl::setSeverity(isc::log::Severity severity, int dbglevel) {
  58. isc::log::Level level(severity, dbglevel);
  59. logger_.setLogLevel(LoggerLevelImpl::convertFromBindLevel(level));
  60. }
  61. // Return severity level
  62. isc::log::Severity
  63. LoggerImpl::getSeverity() {
  64. isc::log::Level level =
  65. LoggerLevelImpl::convertToBindLevel(logger_.getLogLevel());
  66. return level.severity;
  67. }
  68. // Return current debug level (only valid if current severity level is DEBUG).
  69. int
  70. LoggerImpl::getDebugLevel() {
  71. isc::log::Level level =
  72. LoggerLevelImpl::convertToBindLevel(logger_.getLogLevel());
  73. return level.dbglevel;
  74. }
  75. // Get effective severity. Either the current severity or, if not set, the
  76. // severity of the root level.
  77. isc::log::Severity
  78. LoggerImpl::getEffectiveSeverity() {
  79. isc::log::Level level =
  80. LoggerLevelImpl::convertToBindLevel(logger_.getChainedLogLevel());
  81. return level.severity;
  82. }
  83. // Return effective debug level (only valid if current effective severity level
  84. // is DEBUG).
  85. int
  86. LoggerImpl::getEffectiveDebugLevel() {
  87. isc::log::Level level =
  88. LoggerLevelImpl::convertToBindLevel(logger_.getChainedLogLevel());
  89. return level.dbglevel;
  90. }
  91. // Output a general message
  92. string*
  93. LoggerImpl::lookupMessage(const MessageID& ident) {
  94. return (new string(string(ident) + ", " +
  95. MessageDictionary::globalDictionary().getText(ident)));
  96. }
  97. void
  98. LoggerImpl::outputRaw(const Severity& severity, const string& message) {
  99. switch (severity) {
  100. case DEBUG:
  101. LOG4CPLUS_DEBUG(logger_, fmt_name_ << message);
  102. break;
  103. case INFO:
  104. LOG4CPLUS_INFO(logger_, fmt_name_ << message);
  105. break;
  106. case WARN:
  107. LOG4CPLUS_WARN(logger_, fmt_name_ << message);
  108. break;
  109. case ERROR:
  110. LOG4CPLUS_ERROR(logger_, fmt_name_ << message);
  111. break;
  112. case FATAL:
  113. LOG4CPLUS_FATAL(logger_, fmt_name_ << message);
  114. }
  115. }
  116. // One-time initialization of log4cplus
  117. void
  118. LoggerImpl::initLog4cplus() {
  119. static bool not_initialized = true;
  120. if (not_initialized) {
  121. // Set up basic configurator
  122. log4cplus::BasicConfigurator config;
  123. config.configure();
  124. // Add additional debug levels
  125. LoggerLevelImpl::init();
  126. // All done.
  127. not_initialized = false;
  128. }
  129. }
  130. } // namespace log
  131. } // namespace isc