SplitterChannel.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // SplitterChannel.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/SplitterChannel.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: SplitterChannel
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/SplitterChannel.h"
  16. #include "Poco/LoggingRegistry.h"
  17. #include "Poco/StringTokenizer.h"
  18. namespace Poco {
  19. SplitterChannel::SplitterChannel()
  20. {
  21. }
  22. SplitterChannel::~SplitterChannel()
  23. {
  24. try
  25. {
  26. close();
  27. }
  28. catch (...)
  29. {
  30. poco_unexpected();
  31. }
  32. }
  33. void SplitterChannel::addChannel(Channel* pChannel)
  34. {
  35. poco_check_ptr (pChannel);
  36. FastMutex::ScopedLock lock(_mutex);
  37. pChannel->duplicate();
  38. _channels.push_back(pChannel);
  39. }
  40. void SplitterChannel::removeChannel(Channel* pChannel)
  41. {
  42. FastMutex::ScopedLock lock(_mutex);
  43. for (ChannelVec::iterator it = _channels.begin(); it != _channels.end(); ++it)
  44. {
  45. if (*it == pChannel)
  46. {
  47. pChannel->release();
  48. _channels.erase(it);
  49. break;
  50. }
  51. }
  52. }
  53. void SplitterChannel::setProperty(const std::string& name, const std::string& value)
  54. {
  55. if (name.compare(0, 7, "channel") == 0)
  56. {
  57. StringTokenizer tokenizer(value, ",;", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
  58. for (StringTokenizer::Iterator it = tokenizer.begin(); it != tokenizer.end(); ++it)
  59. {
  60. addChannel(LoggingRegistry::defaultRegistry().channelForName(*it));
  61. }
  62. }
  63. else Channel::setProperty(name, value);
  64. }
  65. void SplitterChannel::log(const Message& msg)
  66. {
  67. FastMutex::ScopedLock lock(_mutex);
  68. for (ChannelVec::iterator it = _channels.begin(); it != _channels.end(); ++it)
  69. {
  70. (*it)->log(msg);
  71. }
  72. }
  73. void SplitterChannel::close()
  74. {
  75. FastMutex::ScopedLock lock(_mutex);
  76. for (ChannelVec::iterator it = _channels.begin(); it != _channels.end(); ++it)
  77. {
  78. (*it)->release();
  79. }
  80. _channels.clear();
  81. }
  82. int SplitterChannel::count() const
  83. {
  84. FastMutex::ScopedLock lock(_mutex);
  85. return (int) _channels.size();
  86. }
  87. } // namespace Poco