You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
4.2 KiB

  1. // Module: Log4CPLUS
  2. // File: loggerimpl.cxx
  3. // Created: 6/2001
  4. // Author: Tad E. Smith
  5. //
  6. //
  7. // Copyright 2001-2013 Tad E. Smith
  8. //
  9. // Licensed under the Apache License, Version 2.0 (the "License");
  10. // you may not use this file except in compliance with the License.
  11. // You may obtain a copy of the License at
  12. //
  13. // http://www.apache.org/licenses/LICENSE-2.0
  14. //
  15. // Unless required by applicable law or agreed to in writing, software
  16. // distributed under the License is distributed on an "AS IS" BASIS,
  17. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. // See the License for the specific language governing permissions and
  19. // limitations under the License.
  20. #include <log4cplus/internal/internal.h>
  21. #include <log4cplus/spi/loggerimpl.h>
  22. #include <log4cplus/appender.h>
  23. #include <log4cplus/hierarchy.h>
  24. #include <log4cplus/helpers/loglog.h>
  25. #include <log4cplus/spi/loggingevent.h>
  26. #include <log4cplus/spi/rootlogger.h>
  27. #include <log4cplus/thread/syncprims-pub-impl.h>
  28. namespace log4cplus { namespace spi {
  29. //////////////////////////////////////////////////////////////////////////////
  30. // Logger Constructors and Destructor
  31. //////////////////////////////////////////////////////////////////////////////
  32. LoggerImpl::LoggerImpl(const log4cplus::tstring& name_, Hierarchy& h)
  33. : name(name_),
  34. ll(NOT_SET_LOG_LEVEL),
  35. parent(NULL),
  36. additive(true),
  37. hierarchy(h)
  38. {
  39. }
  40. LoggerImpl::~LoggerImpl()
  41. {
  42. }
  43. //////////////////////////////////////////////////////////////////////////////
  44. // Logger Methods
  45. //////////////////////////////////////////////////////////////////////////////
  46. void
  47. LoggerImpl::callAppenders(const InternalLoggingEvent& event)
  48. {
  49. int writes = 0;
  50. for(const LoggerImpl* c = this; c != NULL; c=c->parent.get()) {
  51. writes += c->appendLoopOnAppenders(event);
  52. if(!c->additive) {
  53. break;
  54. }
  55. }
  56. // No appenders in hierarchy, warn user only once.
  57. if(!hierarchy.emittedNoAppenderWarning && writes == 0) {
  58. helpers::getLogLog().error(
  59. LOG4CPLUS_TEXT("No appenders could be found for logger (")
  60. + getName()
  61. + LOG4CPLUS_TEXT(")."));
  62. helpers::getLogLog().error(
  63. LOG4CPLUS_TEXT("Please initialize the log4cplus system properly."));
  64. hierarchy.emittedNoAppenderWarning = true;
  65. }
  66. }
  67. void
  68. LoggerImpl::closeNestedAppenders()
  69. {
  70. SharedAppenderPtrList appenders = getAllAppenders();
  71. for(SharedAppenderPtrList::iterator it=appenders.begin();
  72. it!=appenders.end(); ++it)
  73. {
  74. Appender & appender = **it;
  75. if (! appender.isClosed())
  76. appender.close();
  77. }
  78. }
  79. bool
  80. LoggerImpl::isEnabledFor(LogLevel loglevel) const
  81. {
  82. if(hierarchy.disableValue >= loglevel) {
  83. return false;
  84. }
  85. return loglevel >= getChainedLogLevel();
  86. }
  87. void
  88. LoggerImpl::log(LogLevel loglevel,
  89. const log4cplus::tstring& message,
  90. const char* file,
  91. int line)
  92. {
  93. if(isEnabledFor(loglevel)) {
  94. forcedLog(loglevel, message, file, line);
  95. }
  96. }
  97. void
  98. LoggerImpl::log(spi::InternalLoggingEvent const & ev)
  99. {
  100. if (isEnabledFor(ev.getLogLevel ()))
  101. forcedLog(ev);
  102. }
  103. LogLevel
  104. LoggerImpl::getChainedLogLevel() const
  105. {
  106. for(const LoggerImpl *c=this; c != NULL; c=c->parent.get()) {
  107. if(c->ll != NOT_SET_LOG_LEVEL) {
  108. return c->ll;
  109. }
  110. }
  111. helpers::getLogLog().error(
  112. LOG4CPLUS_TEXT("LoggerImpl::getChainedLogLevel()- No valid LogLevel found"),
  113. true);
  114. return NOT_SET_LOG_LEVEL;
  115. }
  116. Hierarchy&
  117. LoggerImpl::getHierarchy() const
  118. {
  119. return hierarchy;
  120. }
  121. bool
  122. LoggerImpl::getAdditivity() const
  123. {
  124. return additive;
  125. }
  126. void
  127. LoggerImpl::setAdditivity(bool additive_)
  128. {
  129. additive = additive_;
  130. }
  131. void
  132. LoggerImpl::forcedLog(LogLevel loglevel,
  133. const log4cplus::tstring& message,
  134. const char* file,
  135. int line)
  136. {
  137. spi::InternalLoggingEvent & ev = internal::get_ptd ()->forced_log_ev;
  138. ev.setLoggingEvent (this->getName(), loglevel, message, file, line);
  139. callAppenders(ev);
  140. }
  141. void
  142. LoggerImpl::forcedLog(spi::InternalLoggingEvent const & ev)
  143. {
  144. callAppenders(ev);
  145. }
  146. } } // namespace log4cplus { namespace spi {