logger_manager.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 <algorithm>
  15. #include <vector>
  16. #include <log/logger.h>
  17. #include <log/logger_manager_impl.h>
  18. #include <log/logger_manager.h>
  19. #include <log/messagedef.h>
  20. #include <log/message_dictionary.h>
  21. #include <log/message_exception.h>
  22. #include <log/message_initializer.h>
  23. #include <log/message_reader.h>
  24. #include <log/message_types.h>
  25. #include <log/root_logger_name.h>
  26. #include <log/macros.h>
  27. #include <log/messagedef.h>
  28. #include <log/message_initializer.h>
  29. using namespace std;
  30. namespace {
  31. // Logger used for logging messages within the logging code itself.
  32. isc::log::Logger logger("log");
  33. }
  34. namespace isc {
  35. namespace log {
  36. // Constructor - create the implementation class.
  37. LoggerManager::LoggerManager() {
  38. impl_ = new LoggerManagerImpl();
  39. }
  40. // Destructor - get rid of the implementation class
  41. LoggerManager::~LoggerManager() {
  42. delete impl_;
  43. }
  44. // Initialize processing
  45. void
  46. LoggerManager::processInit() {
  47. impl_->processInit();
  48. }
  49. // Process logging specification
  50. void
  51. LoggerManager::processSpecification(const LoggerSpecification& spec) {
  52. impl_->processSpecification(spec);
  53. }
  54. // End Processing
  55. void
  56. LoggerManager::processEnd() {
  57. impl_->processEnd();
  58. }
  59. /// Logging system initialization
  60. void
  61. LoggerManager::init(const std::string& root, const char* file,
  62. isc::log::Severity severity, int dbglevel)
  63. {
  64. // Create the BIND 10 root logger and set the default severity and
  65. // debug level. This is the logger that has the name of the application.
  66. // All other loggers created in this application will be its children.
  67. setRootLoggerName(root);
  68. // Initialize the implementation logging. After this point, some basic logging
  69. // has been set up and messages can be logged.
  70. LoggerManagerImpl::init(severity, dbglevel);
  71. // Check if there were any duplicate message IDs in the default dictionary
  72. // and if so, log them. Log using the logging facility logger.
  73. vector<string>& duplicates = MessageInitializer::getDuplicates();
  74. if (!duplicates.empty()) {
  75. // There are duplicates present. This will be listed in alphabetic order of
  76. // message ID, so they need to be sorted. This list itself may contain
  77. // duplicates; if so, the message ID is listed as many times as there are
  78. // duplicates.
  79. sort(duplicates.begin(), duplicates.end());
  80. for (vector<string>::iterator i = duplicates.begin(); i != duplicates.end();
  81. ++i) {
  82. LOG_WARN(logger, MSG_DUPMSGID).arg(*i);
  83. }
  84. }
  85. // Replace any messages with local ones (if given)
  86. if (file) {
  87. readLocalMessageFile(file);
  88. }
  89. }
  90. // Read local message file
  91. // TODO This should be done after the configuration has been read so that
  92. // the file can be placed in the local configuration
  93. void
  94. LoggerManager::readLocalMessageFile(const char* file) {
  95. Logger logger("log");
  96. MessageDictionary& dictionary = MessageDictionary::globalDictionary();
  97. MessageReader reader(&dictionary);
  98. try {
  99. logger.info(MSG_RDLOCMES).arg(file);
  100. reader.readFile(file, MessageReader::REPLACE);
  101. // File successfully read. As each message in the file is supposed to replace
  102. // one in the dictionary, any ID read that can't be located in the dictionary
  103. // will not be used. To aid problem diagnosis, the unknown message IDs are
  104. // listed.
  105. MessageReader::MessageIDCollection unknown = reader.getNotAdded();
  106. for (MessageReader::MessageIDCollection::const_iterator
  107. i = unknown.begin(); i != unknown.end(); ++i) {
  108. string message_id = boost::lexical_cast<string>(*i);
  109. logger.warn(MSG_IDNOTFND).arg(message_id);
  110. }
  111. }
  112. catch (MessageException& e) {
  113. MessageID ident = e.id();
  114. vector<string> args = e.arguments();
  115. switch (args.size()) {
  116. case 0:
  117. LOG_ERROR(logger, ident);
  118. break;
  119. case 1:
  120. LOG_ERROR(logger, ident).arg(args[0]);
  121. break;
  122. case 2:
  123. LOG_ERROR(logger, ident).arg(args[0]).arg(args[1]);
  124. break;
  125. default: // 3 or more (3 should be the maximum)
  126. LOG_ERROR(logger, ident).arg(args[0]).arg(args[1]).arg(args[2]);
  127. }
  128. }
  129. }
  130. // Reset logging
  131. void
  132. LoggerManager::reset() {
  133. LoggerManagerImpl::reset();
  134. }
  135. } // namespace log
  136. } // namespace isc