Kea 2.7.6
message_initializer.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2024 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
16namespace {
17
18using namespace isc::log;
19
28getNonConstLoggerValues() {
29 static LoggerValuesListPtr logger_values(new LoggerValuesList());
30 return (logger_values);
31}
32
40getNonConstDuplicates() {
41 static LoggerDuplicatesListPtr duplicates(new LoggerDuplicatesList());
42 return (duplicates);
43}
44} // end unnamed namespace
45
46
47namespace isc {
48namespace 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
93size_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
101void
102MessageInitializer::loadDictionary(bool ignore_duplicates) {
104 const LoggerValuesListPtr& logger_values = getNonConstLoggerValues();
105
106 for (auto const& values : *logger_values) {
107 std::vector<std::string> repeats = global->load(values);
108
109 // Append the IDs in the list just loaded (the "repeats") to the
110 // global list of duplicate IDs.
111 if (!ignore_duplicates && !repeats.empty()) {
112 const LoggerDuplicatesListPtr& duplicates = getNonConstDuplicates();
113 duplicates->insert(duplicates->end(), repeats.begin(), repeats.end());
114 }
115 }
116
117 // ... and mark that the messages have been loaded. (This avoids a lot
118 // of "duplicate message ID" messages in some of the unit tests where the
119 // logging initialization code may be called multiple times.)
120 logger_values->clear();
121}
122
123// Return reference to duplicates vector
124const std::list<std::string>&
126 return (*getNonConstDuplicates());
127}
128
129// Clear the duplicates vector
130void
132 getNonConstDuplicates()->clear();
133}
134
135} // namespace log
136} // namespace isc
static const MessageDictionaryPtr & globalDictionary()
Return Global Dictionary.
static void loadDictionary(bool ignore_duplicates=false)
Run-Time Initialization.
static size_t getPendingCount()
Obtain pending load count.
MessageInitializer(const char *values[])
Constructor.
static const std::list< std::string > & getDuplicates()
Return Duplicates.
static void clearDuplicates()
Clear the static duplicates list.
std::list< const char ** > LoggerValuesList
List of pointers to the messages.
boost::shared_ptr< LoggerValuesList > LoggerValuesListPtr
Shared pointer to the list of pointers to the messages.
boost::shared_ptr< LoggerDuplicatesList > LoggerDuplicatesListPtr
Shared pointer to the list of duplicated messages.
boost::shared_ptr< MessageDictionary > MessageDictionaryPtr
Shared pointer to the MessageDictionary.
std::list< std::string > LoggerDuplicatesList
List of duplicated messages.
Defines the logger used by the top-level component of kea-lfc.