Kea: config_base.cc Source File

| Kea 3.2.0-git

config_base.cc
Go to the documentation of this file.
1// Copyright (C) 2018-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
9#include <log/logger_manager.h>
10#include <log/logger_name.h>
11#include <log/logger_specification.h>
12#include <process/config_base.h>
13
14#include <list>
15
16using namespace isc::log;
17using namespace isc::data;
18
19namespace isc {
20namespace process {
21
22void
23ConfigBase::applyLoggingCfg() const {
24 std::list<LoggerSpecification> specs;
25 for (LoggingInfo const& logging_info : logging_info_) {
26 if (logging_info.name_ == getRootLoggerName()) {
27 // Root logger has to be processed first if we expect child loggers
28 // to inherit configuration from it.
29 specs.push_front(logging_info.toSpec());
30 } else {
31 specs.push_back(logging_info.toSpec());
32 }
33 }
34 LoggerManager manager;
35 manager.process(specs.begin(), specs.end());
36}
37
38bool
39ConfigBase::equals(const ConfigBase& other) const {
40 // If number of loggers is different, then configurations aren't equal.
41 if (logging_info_.size() != other.logging_info_.size()) {
42 return (false);
43 }
44 // Pass through all loggers and try to find the match for each of them
45 // with the loggers from the other configuration. The order doesn't
46 // matter so we can't simply compare the vectors.
47 for (LoggingInfoStorage::const_iterator this_it =
48 logging_info_.begin(); this_it != logging_info_.end();
49 ++this_it) {
50 bool match = false;
51 for (LoggingInfoStorage::const_iterator other_it =
52 other.logging_info_.begin();
53 other_it != other.logging_info_.end(); ++other_it) {
54 if (this_it->equals(*other_it)) {
55 match = true;
56 break;
57 }
58 }
59 // No match found for the particular logger so return false.
60 if (!match) {
61 return (false);
62 }
63 }
64
65 // Check config control info for equality.
66 if ((config_ctl_info_ && !other.config_ctl_info_) ||
67 (!config_ctl_info_ && other.config_ctl_info_) ||
68 ((config_ctl_info_ && other.config_ctl_info_) &&
69 (!config_ctl_info_->equals(*(other.config_ctl_info_))))) {
70 return (false);
71 }
72
73 return (true);
74}
75
76void
77ConfigBase::copy(ConfigBase& other) const {
78 // We will entirely replace loggers in the new configuration.
79 other.logging_info_.clear();
80 for (auto const& it : logging_info_) {
81 other.addLoggingInfo(it);
82 }
83
84 // Clone the config control info
85 if (config_ctl_info_) {
86 other.config_ctl_info_.reset(new ConfigControlInfo(*config_ctl_info_));
87 } else {
88 other.config_ctl_info_.reset();
89 }
90
91 // Clone server tag.
92 other.server_tag_ = server_tag_;
93}
94
95void
96ConfigBase::merge(ConfigBase& other) {
97 // Merge logging info.
98 if (!other.logging_info_.empty()) {
99 logging_info_ = other.logging_info_;
100 }
101
102 // Merge the config control info
103 if (other.config_ctl_info_) {
104 if (config_ctl_info_) {
105 config_ctl_info_->merge(*other.config_ctl_info_);
106 } else {
107 config_ctl_info_ = other.config_ctl_info_;
108 }
109 }
110
111 // Merge server tag.
112 if (!other.server_tag_.unspecified()) {
113 server_tag_ = other.server_tag_.get();
114 }
115}
116
117 ElementPtr
118ConfigBase::toElement() const {
119 ElementPtr result = Element::createMap();
120
121 // Was in the Logging global map.
122 if (!logging_info_.empty()) {
123 // Set loggers list
124 ElementPtr loggers = Element::createList();
125 for (LoggingInfoStorage::const_iterator logger =
126 logging_info_.cbegin();
127 logger != logging_info_.cend(); ++logger) {
128 loggers->add(logger->toElement());
129 }
130 result->set("loggers", loggers);
131 }
132
133 // server-tag
134 if (!server_tag_.unspecified()) {
135 result->set("server-tag", Element::create(server_tag_.get()));
136 }
137
138 return (result);
139}
140
141} // namespace process
142} // namespace isc
Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
Definition data.cc:299
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:354
Element::createList
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:349
isc::log::LoggerManager
Logger Manager.
Definition logger_manager.h:39
isc::log::LoggerManager::process
void process(T start, T finish)
Process Specifications.
Definition logger_manager.h:56
isc::process::ConfigBase
Base class for all configurations.
Definition config_base.h:33
isc::process::ConfigBase::addLoggingInfo
void addLoggingInfo(const process::LoggingInfo &logging_info)
Sets logging specific configuration.
Definition config_base.h:50
isc::process::ConfigBase::applyLoggingCfg
void applyLoggingCfg() const
Apply logging configuration to log4cplus.
Definition config_base.cc:23
isc::process::ConfigBase::toElement
virtual isc::data::ElementPtr toElement() const
Converts to Element representation.
Definition config_base.cc:118
isc::process::ConfigBase::copy
void copy(ConfigBase &new_config) const
Copies the current configuration to a new configuration.
Definition config_base.cc:77
isc::process::ConfigBase::merge
virtual void merge(ConfigBase &other)
Merges specified configuration into this configuration.
Definition config_base.cc:96
isc::process::ConfigBase::equals
bool equals(const ConfigBase &other) const
Compares two configuration.
Definition config_base.cc:39
isc::process::ConfigControlInfo
Embodies configuration information used during a server's configuration process.
Definition config_ctl_info.h:141
isc::process::LoggingInfo
structure that describes one logging entry
Definition logging_info.h:81
isc::util::Optional::get
T get() const
Retrieves the encapsulated value.
Definition optional.h:123
isc::util::Optional::unspecified
void unspecified(bool unspecified)
Modifies the flag that indicates whether the value is specified or unspecified.
Definition optional.h:145
config_base.h
logger_manager.h
logger_name.h
logger_specification.h
isc::data
Definition base_stamped_element.cc:12
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
isc::log
Definition buffer_appender_impl.cc:17
isc::log::getRootLoggerName
const std::string & getRootLoggerName()
Get root logger name.
Definition logger_name.cc:33
isc::process
Definition cb_ctl_base.h:22
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18