logger_impl.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE
  14. #include <iostream>
  15. #include <algorithm>
  16. #include <stdarg.h>
  17. #include <stdio.h>
  18. #include <boost/lexical_cast.hpp>
  19. #include <log/debug_levels.h>
  20. #include <log/root_logger_name.h>
  21. #include <log/logger.h>
  22. #include <log/logger_impl.h>
  23. #include <log/message_dictionary.h>
  24. #include <log/message_types.h>
  25. #include <log/root_logger_name.h>
  26. #include <util/strutil.h>
  27. using namespace std;
  28. namespace isc {
  29. namespace log {
  30. // Static initializations
  31. LoggerImpl::LoggerInfoMap LoggerImpl::logger_info_;
  32. LoggerImpl::LoggerInfo LoggerImpl::root_logger_info_(isc::log::INFO, 0);
  33. // Constructor
  34. LoggerImpl::LoggerImpl(const std::string& name, bool)
  35. {
  36. // Are we the root logger?
  37. if (name == getRootLoggerName()) {
  38. is_root_ = true;
  39. name_ = name;
  40. } else {
  41. is_root_ = false;
  42. name_ = getRootLoggerName() + "." + name;
  43. }
  44. }
  45. // Destructor. (Here because of virtual declaration.)
  46. LoggerImpl::~LoggerImpl() {
  47. }
  48. // Set the severity for logging.
  49. void
  50. LoggerImpl::setSeverity(isc::log::Severity severity, int dbglevel) {
  51. // Silently coerce the debug level into the valid range of 0 to 99
  52. int debug_level = max(MIN_DEBUG_LEVEL, min(MAX_DEBUG_LEVEL, dbglevel));
  53. if (is_root_) {
  54. // Can only set severity for the root logger, you can't disable it.
  55. // Any attempt to do so is silently ignored.
  56. if (severity != isc::log::DEFAULT) {
  57. root_logger_info_ = LoggerInfo(severity, debug_level);
  58. }
  59. } else if (severity == isc::log::DEFAULT) {
  60. // Want to set to default; this means removing the information
  61. // about this logger from the logger_info_ if it is set.
  62. LoggerInfoMap::iterator i = logger_info_.find(name_);
  63. if (i != logger_info_.end()) {
  64. logger_info_.erase(i);
  65. }
  66. } else {
  67. // Want to set this information
  68. logger_info_[name_] = LoggerInfo(severity, debug_level);
  69. }
  70. }
  71. // Return severity level
  72. isc::log::Severity
  73. LoggerImpl::getSeverity() {
  74. if (is_root_) {
  75. return (root_logger_info_.severity);
  76. }
  77. else {
  78. LoggerInfoMap::iterator i = logger_info_.find(name_);
  79. if (i != logger_info_.end()) {
  80. return ((i->second).severity);
  81. }
  82. else {
  83. return (isc::log::DEFAULT);
  84. }
  85. }
  86. }
  87. // Get effective severity. Either the current severity or, if not set, the
  88. // severity of the root level.
  89. isc::log::Severity
  90. LoggerImpl::getEffectiveSeverity() {
  91. if (!is_root_ && !logger_info_.empty()) {
  92. // Not root logger and there is at least one item in the info map for a
  93. // logger.
  94. LoggerInfoMap::iterator i = logger_info_.find(name_);
  95. if (i != logger_info_.end()) {
  96. // Found, so return the severity.
  97. return ((i->second).severity);
  98. }
  99. }
  100. // Must be the root logger, or this logger is defaulting to the root logger
  101. // settings.
  102. return (root_logger_info_.severity);
  103. }
  104. // Get the debug level. This returns 0 unless the severity is DEBUG.
  105. int
  106. LoggerImpl::getDebugLevel() {
  107. if (!is_root_ && !logger_info_.empty()) {
  108. // Not root logger and there is something in the map, check if there
  109. // is a setting for this one.
  110. LoggerInfoMap::iterator i = logger_info_.find(name_);
  111. if (i != logger_info_.end()) {
  112. // Found, so return the debug level.
  113. if ((i->second).severity == isc::log::DEBUG) {
  114. return ((i->second).dbglevel);
  115. } else {
  116. return (0);
  117. }
  118. }
  119. }
  120. // Must be the root logger, or this logger is defaulting to the root logger
  121. // settings.
  122. if (root_logger_info_.severity == isc::log::DEBUG) {
  123. return (root_logger_info_.dbglevel);
  124. } else {
  125. return (0);
  126. }
  127. }
  128. // The code for isXxxEnabled is quite simple and is in the header. The only
  129. // exception is isDebugEnabled() where we have the complication of the debug
  130. // levels.
  131. bool
  132. LoggerImpl::isDebugEnabled(int dbglevel) {
  133. if (!is_root_ && !logger_info_.empty()) {
  134. // Not root logger and there is something in the map, check if there
  135. // is a setting for this one.
  136. LoggerInfoMap::iterator i = logger_info_.find(name_);
  137. if (i != logger_info_.end()) {
  138. // Found, so return the debug level.
  139. if ((i->second).severity <= isc::log::DEBUG) {
  140. return ((i->second).dbglevel >= dbglevel);
  141. } else {
  142. return (false); // Nothing lower than debug
  143. }
  144. }
  145. }
  146. // Must be the root logger, or this logger is defaulting to the root logger
  147. // settings.
  148. if (root_logger_info_.severity <= isc::log::DEBUG) {
  149. return (root_logger_info_.dbglevel >= dbglevel);
  150. } else {
  151. return (false);
  152. }
  153. }
  154. // Output a general message
  155. string*
  156. LoggerImpl::lookupMessage(const MessageID& ident) {
  157. return (new string(string(ident) + ", " +
  158. MessageDictionary::globalDictionary().getText(ident)));
  159. }
  160. void
  161. LoggerImpl::outputRaw(const char* sevText, const string& message) {
  162. // Get the time in a struct tm format, and convert to text
  163. time_t t_time;
  164. time(&t_time);
  165. struct tm* tm_time = localtime(&t_time);
  166. char chr_time[32];
  167. (void) strftime(chr_time, sizeof(chr_time), "%Y-%m-%d %H:%M:%S", tm_time);
  168. // Now output.
  169. cout << chr_time << " " << sevText << " [" << getName() << "] " <<
  170. message << endl;
  171. }
  172. } // namespace log
  173. } // namespace isc