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