Kea  2.3.4-git
log_parser.cc
Go to the documentation of this file.
1 // Copyright (C) 2014-2022 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/log_parser.h>
10 #include <boost/foreach.hpp>
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 
17 using namespace isc::data;
18 using namespace isc::log;
19 
20 namespace isc {
21 namespace process {
22 
23 LogConfigParser::LogConfigParser(const ConfigPtr& storage)
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  BOOST_FOREACH(ConstElementPtr logger, loggers->listValue()) {
37  parseConfigEntry(logger);
38  }
39 }
40 
41 void 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 
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
89  isc_throw(BadValue, "");
90  }
91  } catch (...) {
92  isc_throw(BadValue, "Unsupported debuglevel value '"
93  << debuglevel_ptr->stringValue()
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 
109  if (output_options) {
110  parseOutputOptions(info.destinations_, output_options);
111  }
112 
113  config_->addLoggingInfo(info);
114 }
115 
116 void LogConfigParser::parseOutputOptions(std::vector<LoggingDestination>& destination,
117  isc::data::ConstElementPtr output_options) {
118  if (!output_options) {
119  isc_throw(BadValue, "Missing 'output_options' structure in 'loggers'");
120  }
121 
122  BOOST_FOREACH(ConstElementPtr output_option, output_options->listValue()) {
123 
124  LoggingDestination dest;
125 
126  isc::data::ConstElementPtr output = output_option->get("output");
127  if (!output) {
128  isc_throw(BadValue, "output_options entry does not have a mandatory 'output' "
129  "element (" << output_option->getPosition() << ")");
130  }
131  dest.output_ = output->stringValue();
132 
133  isc::data::ConstElementPtr maxver_ptr = output_option->get("maxver");
134  if (maxver_ptr) {
135  dest.maxver_ = boost::lexical_cast<int>(maxver_ptr->str());
136  }
137 
138  isc::data::ConstElementPtr maxsize_ptr = output_option->get("maxsize");
139  if (maxsize_ptr) {
140  dest.maxsize_ = boost::lexical_cast<uint64_t>(maxsize_ptr->str());
141  }
142 
143  isc::data::ConstElementPtr flush_ptr = output_option->get("flush");
144  if (flush_ptr) {
145  dest.flush_ = flush_ptr->boolValue();
146  }
147 
148  isc::data::ConstElementPtr pattern = output_option->get("pattern");
149  if (pattern) {
150  dest.pattern_ = pattern->stringValue();
151  }
152 
153  destination.push_back(dest);
154  }
155 }
156 
157 } // namespace isc::dhcp
158 } // namespace isc
void setContext(const data::ConstElementPtr &ctx)
Sets user context.
Definition: user_context.h:30
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
void clearDestinations()
Removes logging destinations.
Definition: logging_info.h:102
std::vector< LoggingDestination > destinations_
specific logging destinations
Definition: logging_info.h:96
structure that describes one logging entry
Definition: logging_info.h:81
std::string pattern_
defines the log format pattern It dictates what additional elements are output
Definition: logging_info.h:43
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.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
std::string output_
defines logging destination output
Definition: logging_info.h:30
uint64_t maxsize_
Maximum log file size.
Definition: logging_info.h:36
Defines single logging destination.
Definition: logging_info.h:23
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
Defines the logger used by the top-level component of kea-lfc.
isc::log::Logger logger("asiodns")
Use the ASIO logger.
Logging initialization functions.
isc::log::Severity severity_
describes logging severity
Definition: logging_info.h:88
int debuglevel_
debuglevel (used when severity_ == DEBUG)
Definition: logging_info.h:93
std::string name_
logging name
Definition: logging_info.h:85
int maxver_
Maximum number of log files in rotation.
Definition: logging_info.h:33
bool flush_
Immediate flush.
Definition: logging_info.h:39
boost::shared_ptr< ConfigBase > ConfigPtr
Non-const pointer to the ConfigBase.
Definition: config_base.h:176