logger.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <stdarg.h>
  15. #include <stdio.h>
  16. #include <log/logger.h>
  17. #include <log/logger_impl.h>
  18. #include <log/message_dictionary.h>
  19. #include <log/message_types.h>
  20. #include <log/root_logger_name.h>
  21. #include <log/strutil.h>
  22. using namespace std;
  23. namespace isc {
  24. namespace log {
  25. // Initialize Logger implementation. Does not check whether the implementation
  26. // has already been initialized - that was done by the caller (getLoggerPtr()).
  27. void Logger::initLoggerImpl() {
  28. loggerptr_ = new LoggerImpl(name_, infunc_);
  29. }
  30. // Destructor.
  31. Logger::~Logger() {
  32. delete loggerptr_;
  33. }
  34. // Get Name of Logger
  35. std::string
  36. Logger::getName() {
  37. return (getLoggerPtr()->getName());
  38. }
  39. // Set the severity for logging.
  40. void
  41. Logger::setSeverity(isc::log::Severity severity, int dbglevel) {
  42. getLoggerPtr()->setSeverity(severity, dbglevel);
  43. }
  44. // Return the severity of the logger.
  45. isc::log::Severity
  46. Logger::getSeverity() {
  47. return (getLoggerPtr()->getSeverity());
  48. }
  49. // Get Effective Severity Level for Logger
  50. isc::log::Severity
  51. Logger::getEffectiveSeverity() {
  52. return (getLoggerPtr()->getEffectiveSeverity());
  53. }
  54. // Debug level (only relevant if messages of severity DEBUG are being logged).
  55. int
  56. Logger::getDebugLevel() {
  57. return (getLoggerPtr()->getDebugLevel());
  58. }
  59. // Check on the current severity settings
  60. bool
  61. Logger::isDebugEnabled(int dbglevel) {
  62. return (getLoggerPtr()->isDebugEnabled(dbglevel));
  63. }
  64. bool
  65. Logger::isInfoEnabled() {
  66. return (getLoggerPtr()->isInfoEnabled());
  67. }
  68. bool
  69. Logger::isWarnEnabled() {
  70. return (getLoggerPtr()->isWarnEnabled());
  71. }
  72. bool
  73. Logger::isErrorEnabled() {
  74. return (getLoggerPtr()->isErrorEnabled());
  75. }
  76. bool
  77. Logger::isFatalEnabled() {
  78. return (getLoggerPtr()->isFatalEnabled());
  79. }
  80. // Format a message: looks up the message text in the dictionary and formats
  81. // it, replacing tokens with arguments.
  82. //
  83. // Owing to the use of variable arguments, this must be inline (hence the
  84. // definition of the macro). Also note that it expects that the message buffer
  85. // "message" is declared in the compilation unit.
  86. // Output methods
  87. void
  88. Logger::debug(int dbglevel, const isc::log::MessageID& ident, ...) {
  89. if (isDebugEnabled(dbglevel)) {
  90. va_list ap;
  91. va_start(ap, ident);
  92. getLoggerPtr()->debug(ident, ap);
  93. va_end(ap);
  94. }
  95. }
  96. void
  97. Logger::info(const isc::log::MessageID& ident, ...) {
  98. if (isInfoEnabled()) {
  99. va_list ap;
  100. va_start(ap, ident);
  101. getLoggerPtr()->info(ident, ap);
  102. va_end(ap);
  103. }
  104. }
  105. void
  106. Logger::warn(const isc::log::MessageID& ident, ...) {
  107. if (isWarnEnabled()) {
  108. va_list ap;
  109. va_start(ap, ident);
  110. getLoggerPtr()->warn(ident, ap);
  111. va_end(ap);
  112. }
  113. }
  114. void
  115. Logger::error(const isc::log::MessageID& ident, ...) {
  116. if (isErrorEnabled()) {
  117. va_list ap;
  118. va_start(ap, ident);
  119. getLoggerPtr()->error(ident, ap);
  120. va_end(ap);
  121. }
  122. }
  123. void
  124. Logger::fatal(const isc::log::MessageID& ident, ...) {
  125. if (isFatalEnabled()) {
  126. va_list ap;
  127. va_start(ap, ident);
  128. getLoggerPtr()->fatal(ident, ap);
  129. va_end(ap);
  130. }
  131. }
  132. bool Logger::operator==(Logger& other) {
  133. return (*getLoggerPtr() == *other.getLoggerPtr());
  134. }
  135. // Protected methods (used for testing)
  136. void
  137. Logger::reset() {
  138. LoggerImpl::reset();
  139. }
  140. } // namespace log
  141. } // namespace isc