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.

174 lines
3.7 KiB

  1. // Module: Log4CPLUS
  2. // File: appenderattachableimpl.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/appender.h>
  21. #include <log4cplus/helpers/appenderattachableimpl.h>
  22. #include <log4cplus/helpers/loglog.h>
  23. #include <log4cplus/spi/loggingevent.h>
  24. #include <log4cplus/thread/syncprims-pub-impl.h>
  25. #include <algorithm>
  26. namespace log4cplus
  27. {
  28. namespace spi
  29. {
  30. AppenderAttachable::~AppenderAttachable()
  31. { }
  32. } // namespace spi
  33. namespace helpers
  34. {
  35. //////////////////////////////////////////////////////////////////////////////
  36. // log4cplus::helpers::AppenderAttachableImpl ctor and dtor
  37. //////////////////////////////////////////////////////////////////////////////
  38. AppenderAttachableImpl::AppenderAttachableImpl()
  39. { }
  40. AppenderAttachableImpl::~AppenderAttachableImpl()
  41. { }
  42. ///////////////////////////////////////////////////////////////////////////////
  43. // log4cplus::helpers::AppenderAttachableImpl public methods
  44. ///////////////////////////////////////////////////////////////////////////////
  45. void
  46. AppenderAttachableImpl::addAppender(SharedAppenderPtr newAppender)
  47. {
  48. if(newAppender == NULL) {
  49. getLogLog().warn( LOG4CPLUS_TEXT("Tried to add NULL appender") );
  50. return;
  51. }
  52. thread::MutexGuard guard (appender_list_mutex);
  53. ListType::iterator it =
  54. std::find(appenderList.begin(), appenderList.end(), newAppender);
  55. if(it == appenderList.end()) {
  56. appenderList.push_back(newAppender);
  57. }
  58. }
  59. AppenderAttachableImpl::ListType
  60. AppenderAttachableImpl::getAllAppenders()
  61. {
  62. thread::MutexGuard guard (appender_list_mutex);
  63. return appenderList;
  64. }
  65. SharedAppenderPtr
  66. AppenderAttachableImpl::getAppender(const log4cplus::tstring& name)
  67. {
  68. thread::MutexGuard guard (appender_list_mutex);
  69. for(ListType::iterator it=appenderList.begin();
  70. it!=appenderList.end();
  71. ++it)
  72. {
  73. if((*it)->getName() == name) {
  74. return *it;
  75. }
  76. }
  77. return SharedAppenderPtr(NULL);
  78. }
  79. void
  80. AppenderAttachableImpl::removeAllAppenders()
  81. {
  82. thread::MutexGuard guard (appender_list_mutex);
  83. appenderList.erase(appenderList.begin(), appenderList.end());
  84. }
  85. void
  86. AppenderAttachableImpl::removeAppender(SharedAppenderPtr appender)
  87. {
  88. if(appender == NULL) {
  89. getLogLog().warn( LOG4CPLUS_TEXT("Tried to remove NULL appender") );
  90. return;
  91. }
  92. thread::MutexGuard guard (appender_list_mutex);
  93. ListType::iterator it =
  94. std::find(appenderList.begin(), appenderList.end(), appender);
  95. if(it != appenderList.end()) {
  96. appenderList.erase(it);
  97. }
  98. }
  99. void
  100. AppenderAttachableImpl::removeAppender(const log4cplus::tstring& name)
  101. {
  102. removeAppender(getAppender(name));
  103. }
  104. int
  105. AppenderAttachableImpl::appendLoopOnAppenders(const spi::InternalLoggingEvent& event) const
  106. {
  107. int count = 0;
  108. thread::MutexGuard guard (appender_list_mutex);
  109. for(ListType::const_iterator it=appenderList.begin();
  110. it!=appenderList.end();
  111. ++it)
  112. {
  113. ++count;
  114. (*it)->doAppend(event);
  115. }
  116. return count;
  117. }
  118. } // namespace helpers
  119. } // namespace log4cplus