logger_manager.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.h>
  18. #include <log/logger_manager_impl.h>
  19. #include <log/logger_name.h>
  20. #include <log/logger_support.h>
  21. #include <log/log_messages.h>
  22. #include <log/macros.h>
  23. #include <log/message_dictionary.h>
  24. #include <log/message_exception.h>
  25. #include <log/message_initializer.h>
  26. #include <log/message_initializer.h>
  27. #include <log/message_reader.h>
  28. #include <log/message_types.h>
  29. #include <log/interprocess/interprocess_sync_null.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(isc::log::getDefaultRootLoggerName());
  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, bool buffer)
  78. {
  79. // Load in the messages declared in the program and registered by
  80. // statically-declared MessageInitializer objects.
  81. MessageInitializer::loadDictionary();
  82. // Save name, severity and debug level for later. No need to save the
  83. // file name as once the local message file is read the messages will
  84. // not be lost.
  85. initRootName() = root;
  86. initSeverity() = severity;
  87. initDebugLevel() = dbglevel;
  88. // Create the BIND 10 root logger and set the default severity and
  89. // debug level. This is the logger that has the name of the application.
  90. // All other loggers created in this application will be its children.
  91. setRootLoggerName(root);
  92. // Initialize the implementation logging. After this point, some basic
  93. // logging has been set up and messages can be logged.
  94. // However, they will not appear until a logging specification has been
  95. // processed (or the program exits), see TODO
  96. LoggerManagerImpl::init(severity, dbglevel, buffer);
  97. setLoggingInitialized();
  98. // Check if there were any duplicate message IDs in the default dictionary
  99. // and if so, log them. Log using the logging facility logger.
  100. const vector<string>& duplicates = MessageInitializer::getDuplicates();
  101. if (!duplicates.empty()) {
  102. // There are duplicates present. This list itself may contain
  103. // duplicates; if so, the message ID is listed as many times as
  104. // there are duplicates.
  105. for (vector<string>::const_iterator i = duplicates.begin();
  106. i != duplicates.end(); ++i) {
  107. LOG_WARN(logger, LOG_DUPLICATE_MESSAGE_ID).arg(*i);
  108. }
  109. MessageInitializer::clearDuplicates();
  110. }
  111. // Replace any messages with local ones (if given)
  112. if (file) {
  113. readLocalMessageFile(file);
  114. }
  115. // Ensure that the mutex is constructed and ready at this point.
  116. (void) getMutex();
  117. }
  118. // Read local message file
  119. // TODO This should be done after the configuration has been read so that
  120. // the file can be placed in the local configuration
  121. void
  122. LoggerManager::readLocalMessageFile(const char* file) {
  123. MessageDictionary& dictionary = MessageDictionary::globalDictionary();
  124. MessageReader reader(&dictionary);
  125. // Turn off use of any lock files. This is because this logger can
  126. // be used by standalone programs which may not have write access to
  127. // the local state directory (to create lock files). So we switch to
  128. // using a null interprocess sync object here.
  129. logger.setInterprocessSync(
  130. new isc::log::interprocess::InterprocessSyncNull("logger"));
  131. try {
  132. logger.info(LOG_READING_LOCAL_FILE).arg(file);
  133. reader.readFile(file, MessageReader::REPLACE);
  134. // File successfully read. As each message in the file is supposed to
  135. // replace one in the dictionary, any ID read that can't be located in
  136. // the dictionary will not be used. To aid problem diagnosis, the
  137. // unknown message IDs are listed.
  138. MessageReader::MessageIDCollection unknown = reader.getNotAdded();
  139. for (MessageReader::MessageIDCollection::const_iterator
  140. i = unknown.begin(); i != unknown.end(); ++i) {
  141. string message_id = boost::lexical_cast<string>(*i);
  142. logger.warn(LOG_NO_SUCH_MESSAGE).arg(message_id);
  143. }
  144. }
  145. catch (MessageException& e) {
  146. MessageID ident = e.id();
  147. vector<string> args = e.arguments();
  148. // Log the variable number of arguments. The actual message will be
  149. // logged when the error_message variable is destroyed.
  150. Formatter<isc::log::Logger> error_message = logger.error(ident);
  151. for (vector<string>::size_type i = 0; i < args.size(); ++i) {
  152. error_message = error_message.arg(args[i]);
  153. }
  154. }
  155. }
  156. // Reset logging to settings passed to init()
  157. void
  158. LoggerManager::reset() {
  159. setRootLoggerName(initRootName());
  160. LoggerManagerImpl::reset(initSeverity(), initDebugLevel());
  161. }
  162. isc::util::thread::Mutex&
  163. LoggerManager::getMutex() {
  164. static isc::util::thread::Mutex mutex;
  165. return (mutex);
  166. }
  167. } // namespace log
  168. } // namespace isc