[80] | 1 | // |
---|
| 2 | // AsyncChannel.cpp |
---|
| 3 | // |
---|
| 4 | // $Id: //poco/1.3/Foundation/src/AsyncChannel.cpp#4 $ |
---|
| 5 | // |
---|
| 6 | // Library: Foundation |
---|
| 7 | // Package: Logging |
---|
| 8 | // Module: AsyncChannel |
---|
| 9 | // |
---|
| 10 | // Copyright (c) 2004-2007, Applied Informatics Software Engineering GmbH. |
---|
| 11 | // and Contributors. |
---|
| 12 | // |
---|
| 13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
| 14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
| 15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
| 16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
| 17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
| 18 | // do so, all subject to the following: |
---|
| 19 | // |
---|
| 20 | // The copyright notices in the Software and this entire statement, including |
---|
| 21 | // the above license grant, this restriction and the following disclaimer, |
---|
| 22 | // must be included in all copies of the Software, in whole or in part, and |
---|
| 23 | // all derivative works of the Software, unless such copies or derivative |
---|
| 24 | // works are solely in the form of machine-executable object code generated by |
---|
| 25 | // a source language processor. |
---|
| 26 | // |
---|
| 27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
| 30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
| 31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
| 32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
| 33 | // DEALINGS IN THE SOFTWARE. |
---|
| 34 | // |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | #include <Poco/AsyncChannel.h> |
---|
| 38 | #include <Poco/Notification.h> |
---|
| 39 | #include <Poco/Message.h> |
---|
| 40 | #include <Poco/Formatter.h> |
---|
| 41 | #include <Poco/AutoPtr.h> |
---|
| 42 | #include <Poco/LoggingRegistry.h> |
---|
| 43 | #include <Poco/Exception.h> |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | namespace Poco { |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | class MessageNotification: public Notification |
---|
| 50 | { |
---|
| 51 | public: |
---|
| 52 | MessageNotification(const Message& msg) |
---|
| 53 | { |
---|
| 54 | _msg = msg; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | ~MessageNotification() |
---|
| 58 | { |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | const Message& message() const |
---|
| 62 | { |
---|
| 63 | return _msg; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | private: |
---|
| 67 | Message _msg; |
---|
| 68 | }; |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | AsyncChannel::AsyncChannel(Channel* pChannel, Thread::Priority prio): |
---|
| 72 | _pChannel(pChannel), |
---|
| 73 | _thread("AsyncChannel") |
---|
| 74 | { |
---|
| 75 | if (_pChannel) _pChannel->duplicate(); |
---|
| 76 | _thread.setPriority(prio); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | AsyncChannel::~AsyncChannel() |
---|
| 81 | { |
---|
| 82 | close(); |
---|
| 83 | if (_pChannel) _pChannel->release(); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | void AsyncChannel::setChannel(Channel* pChannel) |
---|
| 88 | { |
---|
| 89 | FastMutex::ScopedLock lock(_mutex); |
---|
| 90 | |
---|
| 91 | if (_pChannel) _pChannel->release(); |
---|
| 92 | _pChannel = pChannel; |
---|
| 93 | if (_pChannel) _pChannel->duplicate(); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | Channel* AsyncChannel::getChannel() const |
---|
| 98 | { |
---|
| 99 | return _pChannel; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | void AsyncChannel::open() |
---|
| 104 | { |
---|
| 105 | FastMutex::ScopedLock lock(_mutex); |
---|
| 106 | |
---|
| 107 | if (!_thread.isRunning()) |
---|
| 108 | _thread.start(*this); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | void AsyncChannel::close() |
---|
| 113 | { |
---|
| 114 | if (_thread.isRunning()) |
---|
| 115 | { |
---|
| 116 | while (!_queue.empty()) Thread::sleep(100); |
---|
| 117 | |
---|
| 118 | do |
---|
| 119 | { |
---|
| 120 | _queue.wakeUpAll(); |
---|
| 121 | } |
---|
| 122 | while (!_thread.tryJoin(100)); |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | void AsyncChannel::log(const Message& msg) |
---|
| 128 | { |
---|
| 129 | open(); |
---|
| 130 | |
---|
| 131 | _queue.enqueueNotification(new MessageNotification(msg)); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | void AsyncChannel::setProperty(const std::string& name, const std::string& value) |
---|
| 136 | { |
---|
| 137 | if (name == "channel") |
---|
| 138 | setChannel(LoggingRegistry::defaultRegistry().channelForName(value)); |
---|
| 139 | else if (name == "priority") |
---|
| 140 | setPriority(value); |
---|
| 141 | else |
---|
| 142 | Channel::setProperty(name, value); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | void AsyncChannel::run() |
---|
| 147 | { |
---|
| 148 | AutoPtr<Notification> nf = _queue.waitDequeueNotification(); |
---|
| 149 | while (nf) |
---|
| 150 | { |
---|
| 151 | MessageNotification* pNf = dynamic_cast<MessageNotification*>(nf.get()); |
---|
| 152 | { |
---|
| 153 | FastMutex::ScopedLock lock(_mutex); |
---|
| 154 | |
---|
| 155 | if (pNf && _pChannel) _pChannel->log(pNf->message()); |
---|
| 156 | } |
---|
| 157 | nf = _queue.waitDequeueNotification(); |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | void AsyncChannel::setPriority(const std::string& value) |
---|
| 163 | { |
---|
| 164 | Thread::Priority prio = Thread::PRIO_NORMAL; |
---|
| 165 | |
---|
| 166 | if (value == "lowest") |
---|
| 167 | prio = Thread::PRIO_LOWEST; |
---|
| 168 | else if (value == "low") |
---|
| 169 | prio = Thread::PRIO_LOW; |
---|
| 170 | else if (value == "normal") |
---|
| 171 | prio = Thread::PRIO_NORMAL; |
---|
| 172 | else if (value == "high") |
---|
| 173 | prio = Thread::PRIO_HIGH; |
---|
| 174 | else if (value == "highest") |
---|
| 175 | prio = Thread::PRIO_HIGHEST; |
---|
| 176 | else |
---|
| 177 | throw InvalidArgumentException("thread priority", value); |
---|
| 178 | |
---|
| 179 | _thread.setPriority(prio); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | } // namespace Poco |
---|