Kea  1.9.9-git
message_initializer.cc
Go to the documentation of this file.
1 // Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
11 #include <algorithm>
12 #include <cassert>
13 #include <cstdlib>
14 
15 
16 namespace {
17 
18 using namespace isc::log;
19 
28 getNonConstLoggerValues() {
29  static LoggerValuesListPtr logger_values(new LoggerValuesList());
30  return (logger_values);
31 }
32 
40 getNonConstDuplicates() {
41  static LoggerDuplicatesListPtr duplicates(new LoggerDuplicatesList());
42  return (duplicates);
43 }
44 } // end unnamed namespace
45 
46 
47 namespace isc {
48 namespace log {
49 
51  : values_(values),
52  global_dictionary_(MessageDictionary::globalDictionary()),
53  global_logger_values_(getNonConstLoggerValues()),
54  global_logger_duplicates_(getNonConstDuplicates()) {
55  global_logger_values_->push_back(values);
56 }
57 
59  // Search for the pointer to pending messages belonging to our instance.
60  LoggerValuesList::iterator my_messages = std::find(global_logger_values_->begin(),
61  global_logger_values_->end(),
62  values_);
63  bool pending = (my_messages != global_logger_values_->end());
64  // Our messages are still pending, so let's remove them from the list
65  // of pending messages.
66  if (pending) {
67  global_logger_values_->erase(my_messages);
68 
69  } else {
70  // Our messages are not pending, so they might have been loaded to
71  // the dictionary and/or duplicates.
72  int i = 0;
73  while (values_[i]) {
74  // Check if the unloaded message is registered as duplicate. If it is,
75  // remove it from the duplicates list.
76  LoggerDuplicatesList::iterator dup =
77  std::find(global_logger_duplicates_->begin(),
78  global_logger_duplicates_->end(),
79  values_[i]);
80  if (dup != global_logger_duplicates_->end()) {
81  global_logger_duplicates_->erase(dup);
82 
83  } else {
84  global_dictionary_->erase(values_[i], values_[i + 1]);
85  }
86  i += 2;
87  }
88  }
89 }
90 
91 // Return the number of arrays registered but not yet loaded.
92 
93 size_t
95  return (getNonConstLoggerValues()->size());
96 }
97 
98 // Load the messages in the arrays registered in the logger_values array
99 // into the global dictionary.
100 
101 void
102 MessageInitializer::loadDictionary(bool ignore_duplicates) {
104  const LoggerValuesListPtr& logger_values = getNonConstLoggerValues();
105 
106  for (LoggerValuesList::const_iterator values = logger_values->begin();
107  values != logger_values->end(); ++values) {
108  std::vector<std::string> repeats = global->load(*values);
109 
110  // Append the IDs in the list just loaded (the "repeats") to the
111  // global list of duplicate IDs.
112  if (!ignore_duplicates && !repeats.empty()) {
113  const LoggerDuplicatesListPtr& duplicates = getNonConstDuplicates();
114  duplicates->insert(duplicates->end(), repeats.begin(), repeats.end());
115  }
116  }
117 
118  // ... and mark that the messages have been loaded. (This avoids a lot
119  // of "duplicate message ID" messages in some of the unit tests where the
120  // logging initialization code may be called multiple times.)
121  logger_values->clear();
122 }
123 
124 // Return reference to duplicates vector
125 const std::list<std::string>&
127  return (*getNonConstDuplicates());
128 }
129 
130 // Clear the duplicates vector
131 void
133  getNonConstDuplicates()->clear();
134 }
135 
136 } // namespace log
137 } // namespace isc
static const MessageDictionaryPtr & globalDictionary()
Return Global Dictionary.
std::list< const char ** > LoggerValuesList
List of pointers to the messages.
static void loadDictionary(bool ignore_duplicates=false)
Run-Time Initialization.
static const std::list< std::string > & getDuplicates()
Return Duplicates.
static void clearDuplicates()
Clear the static duplicates list.
std::list< std::string > LoggerDuplicatesList
List of duplicated messages.
Defines the logger used by the top-level component of kea-dhcp-ddns.
boost::shared_ptr< LoggerDuplicatesList > LoggerDuplicatesListPtr
Shared pointer to the list of duplicated messages.
boost::shared_ptr< MessageDictionary > MessageDictionaryPtr
Shared pointer to the MessageDictionary.
boost::shared_ptr< LoggerValuesList > LoggerValuesListPtr
Shared pointer to the list of pointers to the messages.
static size_t getPendingCount()
Obtain pending load count.
MessageInitializer(const char *values[])
Constructor.