logging_info.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright (C) 2014-2015,2017 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. #include <config.h>
  7. #include <dhcpsrv/cfgmgr.h>
  8. #include <dhcpsrv/logging_info.h>
  9. #include <log/logger_name.h>
  10. using namespace isc::log;
  11. using namespace isc::data;
  12. namespace isc {
  13. namespace dhcp {
  14. bool
  15. LoggingDestination::equals(const LoggingDestination& other) const {
  16. return (output_ == other.output_ &&
  17. maxver_ == other.maxver_ &&
  18. maxsize_ == other.maxsize_ &&
  19. flush_ == other.flush_);
  20. }
  21. ElementPtr
  22. LoggingDestination::toElement() const {
  23. ElementPtr result = Element::createMap();
  24. // Set output
  25. result->set("output", Element::create(output_));
  26. // Set maxver
  27. result->set("maxver", Element::create(maxver_));
  28. // Set maxsize
  29. result->set("maxsize", Element::create(static_cast<long long>(maxsize_)));
  30. // Set flush
  31. result->set("flush", Element::create(flush_));
  32. return(result);
  33. }
  34. LoggingInfo::LoggingInfo()
  35. : name_("kea"), severity_(isc::log::INFO), debuglevel_(0) {
  36. // If configuration Manager is in the verbose mode, we need to modify the
  37. // default settings.
  38. if (CfgMgr::instance().isVerbose()) {
  39. severity_ = isc::log::DEBUG;
  40. debuglevel_ = 99;
  41. }
  42. // If the process has set the non-empty name for the default logger,
  43. // let's use this name.
  44. std::string logger_name = CfgMgr::instance().getDefaultLoggerName();
  45. if (!logger_name.empty()) {
  46. name_ = logger_name;
  47. }
  48. // Add a default logging destination in case use hasn't provided a
  49. // logger specification.
  50. LoggingDestination dest;
  51. dest.output_ = "stdout";
  52. destinations_.push_back(dest);
  53. }
  54. bool
  55. LoggingInfo::equals(const LoggingInfo& other) const {
  56. // If number of destinations aren't equal, the objects are not equal.
  57. if (destinations_.size() != other.destinations_.size()) {
  58. return (false);
  59. }
  60. // If there is the same number of logging destinations verify that the
  61. // destinations are equal. The order doesn't matter to we don't expect
  62. // that they are at the same index of the vectors.
  63. for (std::vector<LoggingDestination>::const_iterator
  64. it_this = destinations_.begin();
  65. it_this != destinations_.end();
  66. ++it_this) {
  67. bool match = false;
  68. for (std::vector<LoggingDestination>::const_iterator
  69. it_other = other.destinations_.begin();
  70. it_other != other.destinations_.end();
  71. ++it_other) {
  72. if (it_this->equals(*it_other)) {
  73. match = true;
  74. break;
  75. }
  76. }
  77. if (!match) {
  78. return (false);
  79. }
  80. }
  81. // Logging destinations are equal. Check the rest of the parameters for
  82. // equality.
  83. return (name_ == other.name_ &&
  84. severity_ == other.severity_ &&
  85. debuglevel_ == other.debuglevel_);
  86. }
  87. LoggerSpecification
  88. LoggingInfo::toSpec() const {
  89. static const std::string STDOUT = "stdout";
  90. static const std::string STDERR = "stderr";
  91. static const std::string SYSLOG = "syslog";
  92. static const std::string SYSLOG_COLON = "syslog:";
  93. LoggerSpecification spec(name_, severity_, debuglevel_);
  94. // Go over logger destinations and create output options accordingly.
  95. for (std::vector<LoggingDestination>::const_iterator dest =
  96. destinations_.begin(); dest != destinations_.end(); ++dest) {
  97. OutputOption option;
  98. // Set up output option according to destination specification
  99. if (dest->output_ == STDOUT) {
  100. option.destination = OutputOption::DEST_CONSOLE;
  101. option.stream = OutputOption::STR_STDOUT;
  102. } else if (dest->output_ == STDERR) {
  103. option.destination = OutputOption::DEST_CONSOLE;
  104. option.stream = OutputOption::STR_STDERR;
  105. } else if (dest->output_ == SYSLOG) {
  106. option.destination = OutputOption::DEST_SYSLOG;
  107. // Use default specified in OutputOption constructor for the
  108. // syslog destination
  109. } else if (dest->output_.find(SYSLOG_COLON) == 0) {
  110. option.destination = OutputOption::DEST_SYSLOG;
  111. // Must take account of the string actually being "syslog:"
  112. if (dest->output_ == SYSLOG_COLON) {
  113. // The expected syntax is syslog:facility. User skipped
  114. // the logging name, so we'll just use the default ("kea")
  115. option.facility = isc::log::getDefaultRootLoggerName();
  116. } else {
  117. // Everything else in the string is the facility name
  118. option.facility = dest->output_.substr(SYSLOG_COLON.size());
  119. }
  120. } else {
  121. // Not a recognized destination, assume a file.
  122. option.destination = OutputOption::DEST_FILE;
  123. option.filename = dest->output_;
  124. }
  125. // Copy the immediate flush flag
  126. option.flush = dest->flush_;
  127. // ... and set the destination
  128. spec.addOutputOption(option);
  129. }
  130. return (spec);
  131. }
  132. ElementPtr
  133. LoggingInfo::toElement() const {
  134. ElementPtr result = Element::createMap();
  135. // Set name
  136. result->set("name", Element::create(name_));
  137. // Set output_options
  138. ElementPtr options = Element::createList();
  139. for (std::vector<LoggingDestination>::const_iterator dest =
  140. destinations_.cbegin();
  141. dest != destinations_.cend(); ++dest) {
  142. options->add(dest->toElement());
  143. }
  144. result->set("output_options", options);
  145. // Set severity
  146. std::string severity;
  147. switch (severity_) {
  148. case isc::log::DEBUG:
  149. severity = "DEBUG";
  150. break;
  151. case isc::log::INFO:
  152. severity = "INFO";
  153. break;
  154. case isc::log::WARN:
  155. severity = "WARN";
  156. break;
  157. case isc::log::ERROR:
  158. severity = "ERROR";
  159. break;
  160. case isc::log::FATAL:
  161. severity = "FATAL";
  162. break;
  163. case isc::log::NONE:
  164. severity = "NONE";
  165. break;
  166. default:
  167. isc_throw(ToElementError, "illegal severity: " << severity_);
  168. break;
  169. }
  170. result->set("severity", Element::create(severity));
  171. // Set debug level
  172. result->set("debuglevel", Element::create(debuglevel_));
  173. return (result);
  174. }
  175. } // end of namespace isc::dhcp
  176. } // end of namespace isc