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