Kea 2.5.8
logging_info.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>
9#include <process/daemon.h>
10#include <log/logger_name.h>
11
12using namespace isc::log;
13using namespace isc::data;
14
15namespace isc {
16namespace process {
17
18static const std::string STDOUT = "stdout";
19static const std::string STDERR = "stderr";
20static const std::string SYSLOG = "syslog";
21static const std::string SYSLOG_COLON = "syslog:";
22
23bool
25 return (output_ == other.output_ &&
26 maxver_ == other.maxver_ &&
27 maxsize_ == other.maxsize_ &&
28 flush_ == other.flush_ &&
29 pattern_ == other.pattern_);
30}
31
35
36 // Set output
37 result->set("output", Element::create(output_));
38
39 // Set flush
40 result->set("flush", Element::create(flush_));
41
42 // Set pattern
43 result->set("pattern", Element::create(pattern_));
44
45 if ((output_ != STDOUT) && (output_ != STDERR) && (output_ != SYSLOG) &&
46 (output_.find(SYSLOG_COLON) == std::string::npos)) {
47 // Set maxver
48 result->set("maxver", Element::create(maxver_));
49
50 // Set maxsize
51 result->set("maxsize", Element::create(static_cast<long long>(maxsize_)));
52 }
53
54 return (result);
55}
56
58 : name_("kea"), severity_(isc::log::INFO), debuglevel_(0) {
59 // If configuration Manager is in the verbose mode, we need to modify the
60 // default settings.
61 if (Daemon::getVerbose()) {
63 debuglevel_ = 99;
64 }
65
66 // If the process has set the non-empty name for the default logger,
67 // let's use this name.
68 std::string default_logger = Daemon::getDefaultLoggerName();
69 if (!default_logger.empty()) {
70 name_ = default_logger;
71 }
72
73 // Add a default logging destination in case use hasn't provided a
74 // logger specification.
76 dest.output_ = "stdout";
77 destinations_.push_back(dest);
78}
79
80bool
81LoggingInfo::equals(const LoggingInfo& other) const {
82 // If number of destinations aren't equal, the objects are not equal.
83 if (destinations_.size() != other.destinations_.size()) {
84 return (false);
85 }
86 // If there is the same number of logging destinations verify that the
87 // destinations are equal. The order doesn't matter to we don't expect
88 // that they are at the same index of the vectors.
89 for (auto const& dest : destinations_) {
90 bool match = false;
91 for (auto const& dest_other : other.destinations_) {
92 if (dest.equals(dest_other)) {
93 match = true;
94 break;
95 }
96 }
97 if (!match) {
98 return (false);
99 }
100 }
101
102 // Logging destinations are equal. Check the rest of the parameters for
103 // equality.
104 return (name_ == other.name_ &&
105 severity_ == other.severity_ &&
106 debuglevel_ == other.debuglevel_);
107}
108
112
113 // Go over logger destinations and create output options accordingly.
114 for (auto const& dest : destinations_) {
115 OutputOption option;
116 // Set up output option according to destination specification
117 if (dest.output_ == STDOUT) {
120
121 } else if (dest.output_ == STDERR) {
124
125 } else if (dest.output_ == SYSLOG) {
127 // Use default specified in OutputOption constructor for the
128 // syslog destination
129
130 } else if (dest.output_.find(SYSLOG_COLON) == 0) {
132 // Must take account of the string actually being "syslog:"
133 if (dest.output_ == SYSLOG_COLON) {
134 // The expected syntax is syslog:facility. User skipped
135 // the logging name, so we'll just use the default ("kea")
137
138 } else {
139 // Everything else in the string is the facility name
140 option.facility = dest.output_.substr(SYSLOG_COLON.size());
141 }
142
143 } else {
144 // Not a recognized destination, assume a file.
146 option.filename = dest.output_;
147 option.maxsize = dest.maxsize_;
148 option.maxver = dest.maxver_;
149 }
150
151 // Copy the immediate flush flag
152 option.flush = dest.flush_;
153
154 // Copy the pattern
155 option.pattern = dest.pattern_;
156
157 // ... and set the destination
158 spec.addOutputOption(option);
159 }
160
161 return (spec);
162}
163
167 // Set user context
168 contextToElement(result);
169 // Set name
170 result->set("name", Element::create(name_));
171 // Set output-options if not empty
172 if (!destinations_.empty()) {
174 for (auto const& dest : destinations_) {
175 options->add(dest.toElement());
176 }
177 result->set("output-options", options);
178 }
179 // Set severity
180 std::string severity;
181 switch (severity_) {
182 case isc::log::DEBUG:
183 severity = "DEBUG";
184 break;
185 case isc::log::INFO:
186 severity = "INFO";
187 break;
188 case isc::log::WARN:
189 severity = "WARN";
190 break;
191 case isc::log::ERROR:
192 severity = "ERROR";
193 break;
194 case isc::log::FATAL:
195 severity = "FATAL";
196 break;
197 case isc::log::NONE:
198 severity = "NONE";
199 break;
200 default:
201 isc_throw(ToElementError, "illegal severity: " << severity_);
202 break;
203 }
204 result->set("severity", Element::create(severity));
205 // Set debug level
206 result->set("debuglevel", Element::create(debuglevel_));
207
208 return (result);
209}
210
211} // end of namespace isc::dhcp
212} // end of namespace isc
Cannot unparse error.
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:304
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition: data.cc:299
void addOutputOption(const OutputOption &option)
Add output option.
static bool getVerbose()
Returns if running in verbose mode.
Definition: daemon.cc:84
static std::string getDefaultLoggerName()
Returns default logger name.
Definition: daemon.h:207
structure that describes one logging entry
Definition: logging_info.h:81
LoggingInfo()
Default constructor.
Definition: logging_info.cc:57
int debuglevel_
debuglevel (used when severity_ == DEBUG)
Definition: logging_info.h:93
bool equals(const LoggingInfo &other) const
Compares two objects for equality.
Definition: logging_info.cc:81
isc::log::LoggerSpecification toSpec() const
Converts logger configuration to a spec.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
std::string name_
logging name
Definition: logging_info.h:85
std::vector< LoggingDestination > destinations_
specific logging destinations
Definition: logging_info.h:96
isc::log::Severity severity_
describes logging severity
Definition: logging_info.h:88
const Name & name_
Definition: dns/message.cc:696
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:28
const std::string & getDefaultRootLoggerName()
Returns the default ('kea') root logger name.
Definition: logger_name.cc:37
Defines the logger used by the top-level component of kea-lfc.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition: user_context.cc:15
Destination destination
Members.
Definition: output_option.h:68
std::string pattern
log content pattern
Definition: output_option.h:75
bool flush
true to flush after each message
Definition: output_option.h:70
unsigned int maxver
Maximum versions (none if <= 0)
Definition: output_option.h:74
std::string facility
syslog facility
Definition: output_option.h:71
uint64_t maxsize
0 if no maximum size
Definition: output_option.h:73
Stream stream
stdout/stderr if console output
Definition: output_option.h:69
std::string filename
Filename if file output.
Definition: output_option.h:72
Defines single logging destination.
Definition: logging_info.h:23
int maxver_
Maximum number of log files in rotation.
Definition: logging_info.h:33
std::string output_
defines logging destination output
Definition: logging_info.h:30
std::string pattern_
defines the log format pattern It dictates what additional elements are output
Definition: logging_info.h:43
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition: logging_info.cc:33
bool equals(const LoggingDestination &other) const
Compares two objects for equality.
Definition: logging_info.cc:24
uint64_t maxsize_
Maximum log file size.
Definition: logging_info.h:36
bool flush_
Immediate flush.
Definition: logging_info.h:39