logger_manager.cc 5.8 KB

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