1 | // |
2 | // AsyncChannel.h |
3 | // |
4 | // Library: Foundation |
5 | // Package: Logging |
6 | // Module: AsyncChannel |
7 | // |
8 | // Definition of the AsyncChannel class. |
9 | // |
10 | // Copyright (c) 2004-2007, Applied Informatics Software Engineering GmbH. |
11 | // and Contributors. |
12 | // |
13 | // SPDX-License-Identifier: BSL-1.0 |
14 | // |
15 | |
16 | |
17 | #ifndef Foundation_AsyncChannel_INCLUDED |
18 | #define Foundation_AsyncChannel_INCLUDED |
19 | |
20 | |
21 | #include "Poco/Foundation.h" |
22 | #include "Poco/Channel.h" |
23 | #include "Poco/Thread.h" |
24 | #include "Poco/Mutex.h" |
25 | #include "Poco/Runnable.h" |
26 | #include "Poco/AutoPtr.h" |
27 | #include "Poco/NotificationQueue.h" |
28 | |
29 | |
30 | namespace Poco { |
31 | |
32 | |
33 | class Foundation_API AsyncChannel: public Channel, public Runnable |
34 | /// A channel uses a separate thread for logging. |
35 | /// |
36 | /// Using this channel can help to improve the performance of |
37 | /// applications that produce huge amounts of log messages or |
38 | /// that write log messages to multiple channels simultaneously. |
39 | /// |
40 | /// All log messages are put into a queue and this queue is |
41 | /// then processed by a separate thread. |
42 | { |
43 | public: |
44 | typedef AutoPtr<AsyncChannel> Ptr; |
45 | |
46 | AsyncChannel(Channel::Ptr pChannel = 0, Thread::Priority prio = Thread::PRIO_NORMAL); |
47 | /// Creates the AsyncChannel and connects it to |
48 | /// the given channel. |
49 | |
50 | void setChannel(Channel::Ptr pChannel); |
51 | /// Connects the AsyncChannel to the given target channel. |
52 | /// All messages will be forwarded to this channel. |
53 | |
54 | Channel::Ptr getChannel() const; |
55 | /// Returns the target channel. |
56 | |
57 | void open(); |
58 | /// Opens the channel and creates the |
59 | /// background logging thread. |
60 | |
61 | void close(); |
62 | /// Closes the channel and stops the background |
63 | /// logging thread. |
64 | |
65 | void log(const Message& msg); |
66 | /// Queues the message for processing by the |
67 | /// background thread. |
68 | |
69 | void setProperty(const std::string& name, const std::string& value); |
70 | /// Sets or changes a configuration property. |
71 | /// |
72 | /// The "channel" property allows setting the target |
73 | /// channel via the LoggingRegistry. |
74 | /// The "channel" property is set-only. |
75 | /// |
76 | /// The "priority" property allows setting the thread |
77 | /// priority. The following values are supported: |
78 | /// * lowest |
79 | /// * low |
80 | /// * normal (default) |
81 | /// * high |
82 | /// * highest |
83 | /// |
84 | /// The "priority" property is set-only. |
85 | |
86 | protected: |
87 | ~AsyncChannel(); |
88 | void run(); |
89 | void setPriority(const std::string& value); |
90 | |
91 | private: |
92 | Channel::Ptr _pChannel; |
93 | Thread _thread; |
94 | FastMutex _threadMutex; |
95 | FastMutex _channelMutex; |
96 | NotificationQueue _queue; |
97 | }; |
98 | |
99 | |
100 | } // namespace Poco |
101 | |
102 | |
103 | #endif // Foundation_AsyncChannel_INCLUDED |
104 | |