Kea  1.9.9-git
logging_info.cc
Go to the documentation of this file.
1 // Copyright (C) 2014-2019 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 bool
19 LoggingDestination::equals(const LoggingDestination& other) const {
20  return (output_ == other.output_ &&
21  maxver_ == other.maxver_ &&
22  maxsize_ == other.maxsize_ &&
23  flush_ == other.flush_ &&
24  pattern_ == other.pattern_);
25 }
26 
28 LoggingDestination::toElement() const {
29  ElementPtr result = Element::createMap();
30 
31  // Set output
32  result->set("output", Element::create(output_));
33  // Set maxver
34  result->set("maxver", Element::create(maxver_));
35  // Set maxsize
36  result->set("maxsize", Element::create(static_cast<long long>(maxsize_)));
37  // Set flush
38  result->set("flush", Element::create(flush_));
39  // Set pattern
40  result->set("pattern", Element::create(pattern_));
41 
42  return(result);
43 }
44 
45 LoggingInfo::LoggingInfo()
46  : name_("kea"), severity_(isc::log::INFO), debuglevel_(0) {
47  // If configuration Manager is in the verbose mode, we need to modify the
48  // default settings.
49  if (Daemon::getVerbose()) {
51  debuglevel_ = 99;
52  }
53 
54  // If the process has set the non-empty name for the default logger,
55  // let's use this name.
56  std::string default_logger = Daemon::getDefaultLoggerName();
57  if (!default_logger.empty()) {
58  name_ = default_logger;
59  }
60 
61  // Add a default logging destination in case use hasn't provided a
62  // logger specification.
63  LoggingDestination dest;
64  dest.output_ = "stdout";
65  destinations_.push_back(dest);
66 }
67 
68 bool
69 LoggingInfo::equals(const LoggingInfo& other) const {
70  // If number of destinations aren't equal, the objects are not equal.
71  if (destinations_.size() != other.destinations_.size()) {
72  return (false);
73  }
74  // If there is the same number of logging destinations verify that the
75  // destinations are equal. The order doesn't matter to we don't expect
76  // that they are at the same index of the vectors.
77  for (std::vector<LoggingDestination>::const_iterator
78  it_this = destinations_.begin();
79  it_this != destinations_.end();
80  ++it_this) {
81  bool match = false;
82  for (std::vector<LoggingDestination>::const_iterator
83  it_other = other.destinations_.begin();
84  it_other != other.destinations_.end();
85  ++it_other) {
86  if (it_this->equals(*it_other)) {
87  match = true;
88  break;
89  }
90  }
91  if (!match) {
92  return (false);
93  }
94  }
95 
96  // Logging destinations are equal. Check the rest of the parameters for
97  // equality.
98  return (name_ == other.name_ &&
99  severity_ == other.severity_ &&
100  debuglevel_ == other.debuglevel_);
101 }
102 
105  static const std::string STDOUT = "stdout";
106  static const std::string STDERR = "stderr";
107  static const std::string SYSLOG = "syslog";
108  static const std::string SYSLOG_COLON = "syslog:";
109 
111 
112  // Go over logger destinations and create output options accordingly.
113  for (std::vector<LoggingDestination>::const_iterator dest =
114  destinations_.begin(); dest != destinations_.end(); ++dest) {
115 
116  OutputOption option;
117  // Set up output option according to destination specification
118  if (dest->output_ == STDOUT) {
119  option.destination = OutputOption::DEST_CONSOLE;
120  option.stream = OutputOption::STR_STDOUT;
121 
122  } else if (dest->output_ == STDERR) {
123  option.destination = OutputOption::DEST_CONSOLE;
124  option.stream = OutputOption::STR_STDERR;
125 
126  } else if (dest->output_ == SYSLOG) {
127  option.destination = OutputOption::DEST_SYSLOG;
128  // Use default specified in OutputOption constructor for the
129  // syslog destination
130 
131  } else if (dest->output_.find(SYSLOG_COLON) == 0) {
132  option.destination = OutputOption::DEST_SYSLOG;
133  // Must take account of the string actually being "syslog:"
134  if (dest->output_ == SYSLOG_COLON) {
135  // The expected syntax is syslog:facility. User skipped
136  // the logging name, so we'll just use the default ("kea")
138 
139  } else {
140  // Everything else in the string is the facility name
141  option.facility = dest->output_.substr(SYSLOG_COLON.size());
142  }
143 
144  } else {
145  // Not a recognized destination, assume a file.
146  option.destination = OutputOption::DEST_FILE;
147  option.filename = dest->output_;
148  option.maxsize = dest->maxsize_;
149  option.maxver = dest->maxver_;
150  }
151 
152  // Copy the immediate flush flag
153  option.flush = dest->flush_;
154 
155  // Copy the pattern
156  option.pattern = dest->pattern_;
157 
158  // ... and set the destination
159  spec.addOutputOption(option);
160  }
161 
162  return (spec);
163 }
164 
167  ElementPtr result = Element::createMap();
168  // Set user context
169  contextToElement(result);
170  // Set name
171  result->set("name", Element::create(name_));
172  // Set output_options if not empty
173  if (!destinations_.empty()) {
174  ElementPtr options = Element::createList();
175  for (std::vector<LoggingDestination>::const_iterator dest =
176  destinations_.cbegin();
177  dest != destinations_.cend(); ++dest) {
178  options->add(dest->toElement());
179  }
180  result->set("output_options", options);
181  }
182  // Set severity
183  std::string severity;
184  switch (severity_) {
185  case isc::log::DEBUG:
186  severity = "DEBUG";
187  break;
188  case isc::log::INFO:
189  severity = "INFO";
190  break;
191  case isc::log::WARN:
192  severity = "WARN";
193  break;
194  case isc::log::ERROR:
195  severity = "ERROR";
196  break;
197  case isc::log::FATAL:
198  severity = "FATAL";
199  break;
200  case isc::log::NONE:
201  severity = "NONE";
202  break;
203  default:
204  isc_throw(ToElementError, "illegal severity: " << severity_);
205  break;
206  }
207  result->set("severity", Element::create(severity));
208  // Set debug level
209  result->set("debuglevel", Element::create(debuglevel_));
210  return (result);
211 }
212 
213 } // end of namespace isc::dhcp
214 } // end of namespace isc
void addOutputOption(const OutputOption &option)
Add output option.
size_t maxsize
0 if no maximum size
Definition: output_option.h:73
const std::string & getDefaultRootLoggerName()
Returns the default ('kea') 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:20
bool equals(const LoggingInfo &other) const
Compares two objects for equality.
Definition: logging_info.cc:69
Stream stream
stdout/stderr if console output
Definition: output_option.h:69
structure that describes one logging entry
Definition: logging_info.h:81
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition: user_context.cc:15
isc::log::LoggerSpecification toSpec() const
Converts logger configuration to a spec.
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
Defines the logger used by the top-level component of kea-dhcp-ddns.
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
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
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