logger_impl.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/logger.h>
  23. #include <log/logger_impl.h>
  24. #include <log/logger_level.h>
  25. #include <log/logger_level_impl.h>
  26. #include <log/logger_name.h>
  27. #include <log/message_dictionary.h>
  28. #include <log/message_types.h>
  29. #include <util/strutil.h>
  30. // Note: as log4cplus and the BIND 10 logger have many concepts in common, and
  31. // thus many similar names, to disambiguate types we don't "use" the log4cplus
  32. // namespace: instead, all log4cplus types are explicitly qualified.
  33. using namespace std;
  34. namespace isc {
  35. namespace log {
  36. // Constructor. The setting of logger_ must be done when the variable is
  37. // constructed (instead of being left to the body of the function); at least
  38. // one compiler requires that all member variables be constructed before the
  39. // constructor is run, but log4cplus::Logger (the type of logger_) has no
  40. // default constructor.
  41. LoggerImpl::LoggerImpl(const string& name) : name_(expandLoggerName(name)),
  42. logger_(log4cplus::Logger::getInstance(name_))
  43. {
  44. }
  45. // Destructor. (Here because of virtual declaration.)
  46. LoggerImpl::~LoggerImpl() {
  47. }
  48. // Set the severity for logging.
  49. void
  50. LoggerImpl::setSeverity(isc::log::Severity severity, int dbglevel) {
  51. Level level(severity, dbglevel);
  52. logger_.setLogLevel(LoggerLevelImpl::convertFromBindLevel(level));
  53. }
  54. // Return severity level
  55. isc::log::Severity
  56. LoggerImpl::getSeverity() {
  57. Level level = LoggerLevelImpl::convertToBindLevel(logger_.getLogLevel());
  58. return level.severity;
  59. }
  60. // Return current debug level (only valid if current severity level is DEBUG).
  61. int
  62. LoggerImpl::getDebugLevel() {
  63. Level level = LoggerLevelImpl::convertToBindLevel(logger_.getLogLevel());
  64. return level.dbglevel;
  65. }
  66. // Get effective severity. Either the current severity or, if not set, the
  67. // severity of the root level.
  68. isc::log::Severity
  69. LoggerImpl::getEffectiveSeverity() {
  70. Level level = LoggerLevelImpl::convertToBindLevel(logger_.getChainedLogLevel());
  71. return level.severity;
  72. }
  73. // Return effective debug level (only valid if current effective severity level
  74. // is DEBUG).
  75. int
  76. LoggerImpl::getEffectiveDebugLevel() {
  77. Level level = LoggerLevelImpl::convertToBindLevel(logger_.getChainedLogLevel());
  78. return level.dbglevel;
  79. }
  80. // Output a general message
  81. string*
  82. LoggerImpl::lookupMessage(const MessageID& ident) {
  83. return (new string(string(ident) + " " +
  84. MessageDictionary::globalDictionary().getText(ident)));
  85. }
  86. void
  87. LoggerImpl::outputRaw(const Severity& severity, const string& message) {
  88. switch (severity) {
  89. case DEBUG:
  90. LOG4CPLUS_DEBUG(logger_, message);
  91. break;
  92. case INFO:
  93. LOG4CPLUS_INFO(logger_, message);
  94. break;
  95. case WARN:
  96. LOG4CPLUS_WARN(logger_, message);
  97. break;
  98. case ERROR:
  99. LOG4CPLUS_ERROR(logger_, message);
  100. break;
  101. case FATAL:
  102. LOG4CPLUS_FATAL(logger_, message);
  103. }
  104. }
  105. } // namespace log
  106. } // namespace isc