logger_impl.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <log4cplus/loggingmacros.h>
  23. #include <log/logger.h>
  24. #include <log/logger_impl.h>
  25. #include <log/logger_level.h>
  26. #include <log/logger_level_impl.h>
  27. #include <log/logger_name.h>
  28. #include <log/logger_manager.h>
  29. #include <log/message_dictionary.h>
  30. #include <log/message_types.h>
  31. #include <log/interprocess/interprocess_sync_file.h>
  32. #include <util/strutil.h>
  33. // Note: as log4cplus and the BIND 10 logger have many concepts in common, and
  34. // thus many similar names, to disambiguate types we don't "use" the log4cplus
  35. // namespace: instead, all log4cplus types are explicitly qualified.
  36. using namespace std;
  37. namespace isc {
  38. namespace log {
  39. // Constructor. The setting of logger_ must be done when the variable is
  40. // constructed (instead of being left to the body of the function); at least
  41. // one compiler requires that all member variables be constructed before the
  42. // constructor is run, but log4cplus::Logger (the type of logger_) has no
  43. // default constructor.
  44. LoggerImpl::LoggerImpl(const string& name) :
  45. name_(expandLoggerName(name)),
  46. logger_(log4cplus::Logger::getInstance(name_)),
  47. sync_(new interprocess::InterprocessSyncFile("logger"))
  48. {
  49. }
  50. // Destructor. (Here because of virtual declaration.)
  51. LoggerImpl::~LoggerImpl() {
  52. delete sync_;
  53. }
  54. // Set the severity for logging.
  55. void
  56. LoggerImpl::setSeverity(isc::log::Severity severity, int dbglevel) {
  57. Level level(severity, dbglevel);
  58. logger_.setLogLevel(LoggerLevelImpl::convertFromBindLevel(level));
  59. }
  60. // Return severity level
  61. isc::log::Severity
  62. LoggerImpl::getSeverity() {
  63. Level level = LoggerLevelImpl::convertToBindLevel(logger_.getLogLevel());
  64. return level.severity;
  65. }
  66. // Return current debug level (only valid if current severity level is DEBUG).
  67. int
  68. LoggerImpl::getDebugLevel() {
  69. Level level = LoggerLevelImpl::convertToBindLevel(logger_.getLogLevel());
  70. return level.dbglevel;
  71. }
  72. // Get effective severity. Either the current severity or, if not set, the
  73. // severity of the root level.
  74. isc::log::Severity
  75. LoggerImpl::getEffectiveSeverity() {
  76. Level level = LoggerLevelImpl::convertToBindLevel(logger_.getChainedLogLevel());
  77. return level.severity;
  78. }
  79. // Return effective debug level (only valid if current effective severity level
  80. // is DEBUG).
  81. int
  82. LoggerImpl::getEffectiveDebugLevel() {
  83. Level level = LoggerLevelImpl::convertToBindLevel(logger_.getChainedLogLevel());
  84. return level.dbglevel;
  85. }
  86. // Output a general message
  87. string*
  88. LoggerImpl::lookupMessage(const MessageID& ident) {
  89. return (new string(string(ident) + " " +
  90. MessageDictionary::globalDictionary().getText(ident)));
  91. }
  92. // Replace the interprocess synchronization object
  93. void
  94. LoggerImpl::setInterprocessSync(isc::log::interprocess::InterprocessSync* sync)
  95. {
  96. if (sync == NULL) {
  97. isc_throw(BadInterprocessSync,
  98. "NULL was passed to setInterprocessSync()");
  99. }
  100. delete sync_;
  101. sync_ = sync;
  102. }
  103. void
  104. LoggerImpl::outputRaw(const Severity& severity, const string& message) {
  105. // Use a mutex locker for mutual exclusion from other threads in
  106. // this process.
  107. isc::util::thread::Mutex::Locker mutex_locker(LoggerManager::getMutex());
  108. // Use an interprocess sync locker for mutual exclusion from other
  109. // processes to avoid log messages getting interspersed.
  110. interprocess::InterprocessSyncLocker locker(*sync_);
  111. if (!locker.lock()) {
  112. LOG4CPLUS_ERROR(logger_, "Unable to lock logger lockfile");
  113. }
  114. switch (severity) {
  115. case DEBUG:
  116. LOG4CPLUS_DEBUG(logger_, message);
  117. break;
  118. case INFO:
  119. LOG4CPLUS_INFO(logger_, message);
  120. break;
  121. case WARN:
  122. LOG4CPLUS_WARN(logger_, message);
  123. break;
  124. case ERROR:
  125. LOG4CPLUS_ERROR(logger_, message);
  126. break;
  127. case FATAL:
  128. LOG4CPLUS_FATAL(logger_, message);
  129. break;
  130. case NONE:
  131. break;
  132. default:
  133. LOG4CPLUS_ERROR(logger_,
  134. "Unsupported severity in LoggerImpl::outputRaw(): "
  135. << severity);
  136. }
  137. if (!locker.unlock()) {
  138. LOG4CPLUS_ERROR(logger_, "Unable to unlock logger lockfile");
  139. }
  140. }
  141. } // namespace log
  142. } // namespace isc