Kea: logging_info.cc Source File

| Kea 3.2.0-git

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>
8#include <process/logging_info.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
24LoggingDestination::equals(const LoggingDestination& other) const {
25 return (output_ == other.output_ &&
26 maxver_ == other.maxver_ &&
27 maxsize_ == other.maxsize_ &&
28 flush_ == other.flush_ &&
29 pattern_ == other.pattern_);
30}
31
32 ElementPtr
33LoggingDestination::toElement() const {
34 ElementPtr result = Element::createMap();
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
57LoggingInfo::LoggingInfo()
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()) {
62 severity_ = isc::log::DEBUG;
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.
75 LoggingDestination dest;
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
109 LoggerSpecification
110LoggingInfo::toSpec() const {
111 LoggerSpecification spec(name_, severity_, debuglevel_);
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) {
118 option.destination = OutputOption::DEST_CONSOLE;
119 option.stream = OutputOption::STR_STDOUT;
120
121 } else if (dest.output_ == STDERR) {
122 option.destination = OutputOption::DEST_CONSOLE;
123 option.stream = OutputOption::STR_STDERR;
124
125 } else if (dest.output_ == SYSLOG) {
126 option.destination = OutputOption::DEST_SYSLOG;
127 // Use default specified in OutputOption constructor for the
128 // syslog destination
129
130 } else if (dest.output_.find(SYSLOG_COLON) == 0) {
131 option.destination = OutputOption::DEST_SYSLOG;
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")
136 option.facility = isc::log::getDefaultRootLoggerName();
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.
145 option.destination = OutputOption::DEST_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
164 ElementPtr
165LoggingInfo::toElement() const {
166 ElementPtr result = Element::createMap();
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()) {
173 ElementPtr options = Element::createList();
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
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::ToElementError
Cannot unparse error.
Definition cfg_to_element.h:19
isc::log::LoggerSpecification
Definition logger_specification.h:29
isc::log::LoggerSpecification::addOutputOption
void addOutputOption(const OutputOption &option)
Add output option.
Definition logger_specification.h:99
isc::process::Daemon::getVerbose
static bool getVerbose()
Returns if running in verbose mode.
Definition daemon.cc:87
isc::process::Daemon::getDefaultLoggerName
static std::string getDefaultLoggerName()
Returns default logger name.
Definition daemon.h:222
isc::process::LoggingInfo::LoggingInfo
LoggingInfo()
Default constructor.
Definition logging_info.cc:57
isc::process::LoggingInfo::debuglevel_
int debuglevel_
debuglevel (used when severity_ == DEBUG)
Definition logging_info.h:93
isc::process::LoggingInfo::equals
bool equals(const LoggingInfo &other) const
Compares two objects for equality.
Definition logging_info.cc:81
isc::process::LoggingInfo::toSpec
isc::log::LoggerSpecification toSpec() const
Converts logger configuration to a spec.
Definition logging_info.cc:110
isc::process::LoggingInfo::toElement
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition logging_info.cc:165
isc::process::LoggingInfo::name_
std::string name_
logging name
Definition logging_info.h:85
isc::process::LoggingInfo::destinations_
std::vector< LoggingDestination > destinations_
specific logging destinations
Definition logging_info.h:96
isc::process::LoggingInfo::severity_
isc::log::Severity severity_
describes logging severity
Definition logging_info.h:88
daemon.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
logger_name.h
logging_info.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::getDefaultRootLoggerName
const std::string & getDefaultRootLoggerName()
Returns the default ('kea') root logger name.
Definition logger_name.cc:37
isc::log::WARN
@ WARN
Definition logger_level.h:27
isc::log::NONE
@ NONE
Definition logger_level.h:30
isc::log::ERROR
@ ERROR
Definition logger_level.h:28
isc::log::INFO
@ INFO
Definition logger_level.h:26
isc::log::FATAL
@ FATAL
Definition logger_level.h:29
isc::log::DEBUG
@ DEBUG
Definition logger_level.h:25
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
isc::data::UserContext::contextToElement
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition user_context.cc:15
isc::log::OutputOption
Definition output_option.h:37
isc::log::OutputOption::STR_STDOUT
@ STR_STDOUT
Definition output_option.h:56
isc::log::OutputOption::STR_STDERR
@ STR_STDERR
Definition output_option.h:57
isc::log::OutputOption::destination
Destination destination
Members.
Definition output_option.h:68
isc::log::OutputOption::pattern
std::string pattern
log content pattern
Definition output_option.h:75
isc::log::OutputOption::DEST_CONSOLE
@ DEST_CONSOLE
Definition output_option.h:49
isc::log::OutputOption::DEST_SYSLOG
@ DEST_SYSLOG
Definition output_option.h:51
isc::log::OutputOption::DEST_FILE
@ DEST_FILE
Definition output_option.h:50
isc::log::OutputOption::flush
bool flush
true to flush after each message
Definition output_option.h:70
isc::log::OutputOption::maxver
unsigned int maxver
Maximum versions (none if <= 0).
Definition output_option.h:74
isc::log::OutputOption::facility
std::string facility
syslog facility
Definition output_option.h:71
isc::log::OutputOption::maxsize
uint64_t maxsize
0 if no maximum size
Definition output_option.h:73
isc::log::OutputOption::stream
Stream stream
stdout/stderr if console output
Definition output_option.h:69
isc::log::OutputOption::filename
std::string filename
Filename if file output.
Definition output_option.h:72
isc::process::LoggingDestination
Defines single logging destination.
Definition logging_info.h:23
isc::process::LoggingDestination::maxver_
int maxver_
Maximum number of log files in rotation.
Definition logging_info.h:33
isc::process::LoggingDestination::output_
std::string output_
defines logging destination output
Definition logging_info.h:30
isc::process::LoggingDestination::pattern_
std::string pattern_
defines the log format pattern It dictates what additional elements are output
Definition logging_info.h:43
isc::process::LoggingDestination::toElement
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition logging_info.cc:33
isc::process::LoggingDestination::LoggingDestination
LoggingDestination()
Default constructor.
Definition logging_info.h:53
isc::process::LoggingDestination::equals
bool equals(const LoggingDestination &other) const
Compares two objects for equality.
Definition logging_info.cc:24
isc::process::LoggingDestination::maxsize_
uint64_t maxsize_
Maximum log file size.
Definition logging_info.h:36
isc::process::LoggingDestination::flush_
bool flush_
Immediate flush.
Definition logging_info.h:39