AsyncChannel.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef Foundation_AsyncChannel_INCLUDED
  16. #define Foundation_AsyncChannel_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/Channel.h"
  19. #include "Poco/Thread.h"
  20. #include "Poco/Mutex.h"
  21. #include "Poco/Runnable.h"
  22. #include "Poco/NotificationQueue.h"
  23. namespace Poco {
  24. class Foundation_API AsyncChannel: public Channel, public Runnable
  25. /// A channel uses a separate thread for logging.
  26. ///
  27. /// Using this channel can help to improve the performance of
  28. /// applications that produce huge amounts of log messages or
  29. /// that write log messages to multiple channels simultaneously.
  30. ///
  31. /// All log messages are put into a queue and this queue is
  32. /// then processed by a separate thread.
  33. {
  34. public:
  35. AsyncChannel(Channel* pChannel = 0, Thread::Priority prio = Thread::PRIO_NORMAL);
  36. /// Creates the AsyncChannel and connects it to
  37. /// the given channel.
  38. void setChannel(Channel* pChannel);
  39. /// Connects the AsyncChannel to the given target channel.
  40. /// All messages will be forwarded to this channel.
  41. Channel* getChannel() const;
  42. /// Returns the target channel.
  43. void open();
  44. /// Opens the channel and creates the
  45. /// background logging thread.
  46. void close();
  47. /// Closes the channel and stops the background
  48. /// logging thread.
  49. void log(const Message& msg);
  50. /// Queues the message for processing by the
  51. /// background thread.
  52. void setProperty(const std::string& name, const std::string& value);
  53. /// Sets or changes a configuration property.
  54. ///
  55. /// The "channel" property allows setting the target
  56. /// channel via the LoggingRegistry.
  57. /// The "channel" property is set-only.
  58. ///
  59. /// The "priority" property allows setting the thread
  60. /// priority. The following values are supported:
  61. /// * lowest
  62. /// * low
  63. /// * normal (default)
  64. /// * high
  65. /// * highest
  66. ///
  67. /// The "priority" property is set-only.
  68. protected:
  69. ~AsyncChannel();
  70. void run();
  71. void setPriority(const std::string& value);
  72. private:
  73. Channel* _pChannel;
  74. Thread _thread;
  75. FastMutex _threadMutex;
  76. FastMutex _channelMutex;
  77. NotificationQueue _queue;
  78. };
  79. } // namespace Poco
  80. #endif // Foundation_AsyncChannel_INCLUDED