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