Kea 2.7.5
log_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2014-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#include <cc/data.h>
9#include <process/d_log.h>
10#include <process/log_parser.h>
11#include <boost/lexical_cast.hpp>
13#include <log/logger_support.h>
14#include <log/logger_manager.h>
15#include <log/logger_name.h>
16
17using namespace isc::data;
18using namespace isc::log;
19
20namespace isc {
21namespace process {
22
24 :config_(storage), verbose_(false) {
25 if (!storage) {
26 isc_throw(BadValue, "LogConfigParser needs a pointer to the "
27 "configuration, so parsed data can be stored there");
28 }
29}
30
32 bool verbose) {
33 verbose_ = verbose;
34
35 // Iterate over all entries in "Server/loggers" list
36 for (auto const& logger : loggers->listValue()) {
37 parseConfigEntry(logger);
38 }
39}
40
41void LogConfigParser::parseConfigEntry(isc::data::ConstElementPtr entry) {
42 if (!entry) {
43 // This should not happen, but let's be on the safe side and check
44 return;
45 }
46
47 if (!config_) {
48 isc_throw(BadValue, "configuration storage not set, can't parse logger config.");
49 }
50
51 LoggingInfo info;
52 // Remove default destinations as we are going to replace them.
53 info.clearDestinations();
54
55 // Get user context
56 isc::data::ConstElementPtr user_context = entry->get("user-context");
57 if (user_context) {
58 info.setContext(user_context);
59 }
60
61 // Get a name
62 isc::data::ConstElementPtr name_ptr = entry->get("name");
63 if (!name_ptr) {
64 isc_throw(BadValue, "loggers entry does not have a mandatory 'name' "
65 "element (" << entry->getPosition() << ")");
66 }
67 info.name_ = name_ptr->stringValue();
68
69 // Get the severity.
70 // If not configured, set it to DEFAULT to inherit it from the root logger later.
71 isc::data::ConstElementPtr severity_ptr = entry->get("severity");
72 if (severity_ptr) {
73 info.severity_ = getSeverity(severity_ptr->stringValue());
74 } else {
75 info.severity_ = DEFAULT;
76 }
77
78 // Get debug logging level
79 info.debuglevel_ = 0;
80 isc::data::ConstElementPtr debuglevel_ptr = entry->get("debuglevel");
81
82 // It's ok to not have debuglevel, we'll just assume its least verbose
83 // (0) level.
84 if (debuglevel_ptr) {
85 try {
86 info.debuglevel_ = boost::lexical_cast<int>(debuglevel_ptr->str());
87 if ( (info.debuglevel_ < 0) || (info.debuglevel_ > 99) ) {
88 // Comment doesn't matter, it is caught several lines below
90 }
91 } catch (...) {
92 isc_throw(BadValue, "Unsupported debuglevel value "
93 << debuglevel_ptr->intValue()
94 << ", expected 0-99 ("
95 << debuglevel_ptr->getPosition() << ")");
96 }
97 }
98
99 // We want to follow the normal path, so it could catch parsing errors even
100 // when verbose mode is enabled. If it is, just override whatever was parsed
101 // in the config file.
102 if (verbose_) {
103 info.severity_ = isc::log::DEBUG;
104 info.debuglevel_ = 99;
105 }
106
107 isc::data::ConstElementPtr output_options = entry->get("output-options");
108 isc::data::ConstElementPtr deprecated_output_options = entry->get("output_options");
109
110 if (output_options && deprecated_output_options) {
111 isc_throw(BadValue, "Only one of 'output-options' and 'output_options' may be specified.");
112 }
113
114 if (deprecated_output_options) {
116 output_options = deprecated_output_options;
117 ElementPtr mutable_element = boost::const_pointer_cast<Element>(entry);
118 mutable_element->remove("output_options");
119 mutable_element->set("output-options", output_options);
120 }
121
122 if (output_options) {
123 parseOutputOptions(info.destinations_, output_options);
124 }
125
126 config_->addLoggingInfo(info);
127}
128
129void LogConfigParser::parseOutputOptions(std::vector<LoggingDestination>& destination,
130 isc::data::ConstElementPtr output_options) {
131 if (!output_options) {
132 isc_throw(BadValue, "Missing 'output-options' structure in 'loggers'");
133 }
134
135 for (auto const& output_option : output_options->listValue()) {
136
137 LoggingDestination dest;
138
139 isc::data::ConstElementPtr output = output_option->get("output");
140 if (!output) {
141 isc_throw(BadValue, "output-options entry does not have a mandatory 'output' "
142 "element (" << output_option->getPosition() << ")");
143 }
144 dest.output_ = output->stringValue();
145
146 isc::data::ConstElementPtr maxver_ptr = output_option->get("maxver");
147 if (maxver_ptr) {
148 dest.maxver_ = boost::lexical_cast<int>(maxver_ptr->str());
149 }
150
151 isc::data::ConstElementPtr maxsize_ptr = output_option->get("maxsize");
152 if (maxsize_ptr) {
153 dest.maxsize_ = boost::lexical_cast<uint64_t>(maxsize_ptr->str());
154 }
155
156 isc::data::ConstElementPtr flush_ptr = output_option->get("flush");
157 if (flush_ptr) {
158 dest.flush_ = flush_ptr->boolValue();
159 }
160
161 isc::data::ConstElementPtr pattern = output_option->get("pattern");
162 if (pattern) {
163 dest.pattern_ = pattern->stringValue();
164 }
165
166 destination.push_back(dest);
167 }
168}
169
170} // namespace isc::dhcp
171} // namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
LogConfigParser(const ConfigPtr &storage)
Constructor.
Definition log_parser.cc:23
void parseConfiguration(const isc::data::ConstElementPtr &log_config, bool verbose=false)
Parses specified configuration.
Definition log_parser.cc:31
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Logging initialization functions.
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition macros.h:26
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
@ info
Definition db_log.h:120
isc::log::Severity getSeverity(const std::string &sev_str)
Returns the isc::log::Severity value represented by the given string.
isc::log::Logger dctl_logger("dctl")
Defines the logger used within libkea-process library.
Definition d_log.h:18
boost::shared_ptr< ConfigBase > ConfigPtr
Non-const pointer to the ConfigBase.
const isc::log::MessageID DCTL_DEPRECATED_OUTPUT_OPTIONS
Defines the logger used by the top-level component of kea-lfc.