Kea  2.3.6-git
logging_info.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 <process/logging_info.h>
9 #include <process/daemon.h>
10 #include <log/logger_name.h>
11 
12 using namespace isc::log;
13 using namespace isc::data;
14 
15 namespace isc {
16 namespace process {
17 
18 static const std::string STDOUT = "stdout";
19 static const std::string STDERR = "stderr";
20 static const std::string SYSLOG = "syslog";
21 static const std::string SYSLOG_COLON = "syslog:";
22 
23 bool
24 LoggingDestination::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 
33 LoggingDestination::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 
57 LoggingInfo::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()) {
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 
80 bool
81 LoggingInfo::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) {
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")
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 
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
void addOutputOption(const OutputOption &option)
Add output option.
const std::string & getDefaultRootLoggerName()
Returns the default (&#39;kea&#39;) root logger name.
Definition: logger_name.cc:37
bool flush
true to flush after each message
Definition: output_option.h:70
static std::string getDefaultLoggerName()
Returns default logger name.
Definition: daemon.h:207
std::vector< LoggingDestination > destinations_
specific logging destinations
Definition: logging_info.h:96
Cannot unparse error.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
Stream stream
stdout/stderr if console output
Definition: output_option.h:69
isc::log::LoggerSpecification toSpec() const
Converts logger configuration to a spec.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
structure that describes one logging entry
Definition: logging_info.h:81
uint64_t maxsize
0 if no maximum size
Definition: output_option.h:73
std::string pattern_
defines the log format pattern It dictates what additional elements are output
Definition: logging_info.h:43
std::string facility
syslog facility
Definition: output_option.h:71
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::string output_
defines logging destination output
Definition: logging_info.h:30
std::string pattern
log content pattern
Definition: output_option.h:75
uint64_t maxsize_
Maximum log file size.
Definition: logging_info.h:36
Destination destination
Members.
Definition: output_option.h:68
Defines single logging destination.
Definition: logging_info.h:23
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition: user_context.cc:15
Defines the logger used by the top-level component of kea-lfc.
string & output_
Definition: dns/message.cc:877
const Name & name_
Definition: dns/message.cc:693
unsigned int maxver
Maximum versions (none if <= 0)
Definition: output_option.h:74
static bool getVerbose()
Returns if running in verbose mode.
Definition: daemon.cc:84
isc::log::Severity severity_
describes logging severity
Definition: logging_info.h:88
bool equals(const LoggingInfo &other) const
Compares two objects for equality.
Definition: logging_info.cc:81
int debuglevel_
debuglevel (used when severity_ == DEBUG)
Definition: logging_info.h:93
std::string name_
logging name
Definition: logging_info.h:85
std::string filename
Filename if file output.
Definition: output_option.h:72
int maxver_
Maximum number of log files in rotation.
Definition: logging_info.h:33
bool flush_
Immediate flush.
Definition: logging_info.h:39