logger.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/logger_name.h>
  19. #include <log/logger_support.h>
  20. #include <log/message_dictionary.h>
  21. #include <log/message_types.h>
  22. #include <util/strutil.h>
  23. using namespace std;
  24. namespace isc {
  25. namespace log {
  26. // Initialize underlying logger, but only if logging has been initialized.
  27. void Logger::initLoggerImpl() {
  28. if (isLoggingInitialized()) {
  29. loggerptr_ = new LoggerImpl(name_);
  30. } else {
  31. isc_throw(LoggingNotInitialized, "attempt to access logging function "
  32. "before logging has been initialized");
  33. }
  34. }
  35. // Destructor.
  36. Logger::~Logger() {
  37. delete loggerptr_;
  38. }
  39. // Get Name of Logger
  40. std::string
  41. Logger::getName() {
  42. return (getLoggerPtr()->getName());
  43. }
  44. // Set the severity for logging.
  45. void
  46. Logger::setSeverity(isc::log::Severity severity, int dbglevel) {
  47. getLoggerPtr()->setSeverity(severity, dbglevel);
  48. }
  49. // Return the severity of the logger.
  50. isc::log::Severity
  51. Logger::getSeverity() {
  52. return (getLoggerPtr()->getSeverity());
  53. }
  54. // Get Effective Severity Level for Logger
  55. isc::log::Severity
  56. Logger::getEffectiveSeverity() {
  57. return (getLoggerPtr()->getEffectiveSeverity());
  58. }
  59. // Debug level (only relevant if messages of severity DEBUG are being logged).
  60. int
  61. Logger::getDebugLevel() {
  62. return (getLoggerPtr()->getDebugLevel());
  63. }
  64. // Effective debug level (only relevant if messages of severity DEBUG are being
  65. // logged).
  66. int
  67. Logger::getEffectiveDebugLevel() {
  68. return (getLoggerPtr()->getEffectiveDebugLevel());
  69. }
  70. // Check on the current severity settings
  71. bool
  72. Logger::isDebugEnabled(int dbglevel) {
  73. return (getLoggerPtr()->isDebugEnabled(dbglevel));
  74. }
  75. bool
  76. Logger::isInfoEnabled() {
  77. return (getLoggerPtr()->isInfoEnabled());
  78. }
  79. bool
  80. Logger::isWarnEnabled() {
  81. return (getLoggerPtr()->isWarnEnabled());
  82. }
  83. bool
  84. Logger::isErrorEnabled() {
  85. return (getLoggerPtr()->isErrorEnabled());
  86. }
  87. bool
  88. Logger::isFatalEnabled() {
  89. return (getLoggerPtr()->isFatalEnabled());
  90. }
  91. // Format a message: looks up the message text in the dictionary and formats
  92. // it, replacing tokens with arguments.
  93. //
  94. // Owing to the use of variable arguments, this must be inline (hence the
  95. // definition of the macro). Also note that it expects that the message buffer
  96. // "message" is declared in the compilation unit.
  97. // Output methods
  98. void
  99. Logger::output(const Severity& severity, const std::string& message) {
  100. getLoggerPtr()->outputRaw(severity, message);
  101. }
  102. Logger::Formatter
  103. Logger::debug(int dbglevel, const isc::log::MessageID& ident) {
  104. if (isDebugEnabled(dbglevel)) {
  105. return (Formatter(DEBUG, getLoggerPtr()->lookupMessage(ident),
  106. this));
  107. } else {
  108. return (Formatter());
  109. }
  110. }
  111. Logger::Formatter
  112. Logger::info(const isc::log::MessageID& ident) {
  113. if (isInfoEnabled()) {
  114. return (Formatter(INFO, getLoggerPtr()->lookupMessage(ident),
  115. this));
  116. } else {
  117. return (Formatter());
  118. }
  119. }
  120. Logger::Formatter
  121. Logger::warn(const isc::log::MessageID& ident) {
  122. if (isWarnEnabled()) {
  123. return (Formatter(WARN, getLoggerPtr()->lookupMessage(ident),
  124. this));
  125. } else {
  126. return (Formatter());
  127. }
  128. }
  129. Logger::Formatter
  130. Logger::error(const isc::log::MessageID& ident) {
  131. if (isErrorEnabled()) {
  132. return (Formatter(ERROR, getLoggerPtr()->lookupMessage(ident),
  133. this));
  134. } else {
  135. return (Formatter());
  136. }
  137. }
  138. Logger::Formatter
  139. Logger::fatal(const isc::log::MessageID& ident) {
  140. if (isFatalEnabled()) {
  141. return (Formatter(FATAL, getLoggerPtr()->lookupMessage(ident),
  142. this));
  143. } else {
  144. return (Formatter());
  145. }
  146. }
  147. // Replace the interprocess synchronization object
  148. void
  149. Logger::setInterprocessSync(isc::log::internal::InterprocessSync* sync) {
  150. getLoggerPtr()->setInterprocessSync(sync);
  151. }
  152. // Comparison (testing only)
  153. bool
  154. Logger::operator==(Logger& other) {
  155. return (*getLoggerPtr() == *other.getLoggerPtr());
  156. }
  157. } // namespace log
  158. } // namespace isc