You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.3 KiB

  1. // -*- C++ -*-
  2. // Module: LOG4CPLUS
  3. // File: socketappender.h
  4. // Created: 5/2003
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2003-2013 Tad E. Smith
  9. //
  10. // Licensed under the Apache License, Version 2.0 (the "License");
  11. // you may not use this file except in compliance with the License.
  12. // You may obtain a copy of the License at
  13. //
  14. // http://www.apache.org/licenses/LICENSE-2.0
  15. //
  16. // Unless required by applicable law or agreed to in writing, software
  17. // distributed under the License is distributed on an "AS IS" BASIS,
  18. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. // See the License for the specific language governing permissions and
  20. // limitations under the License.
  21. /** @file */
  22. #ifndef LOG4CPLUS_SOCKET_APPENDER_HEADER_
  23. #define LOG4CPLUS_SOCKET_APPENDER_HEADER_
  24. #include <log4cplus/config.hxx>
  25. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  26. #pragma once
  27. #endif
  28. #include <log4cplus/appender.h>
  29. #include <log4cplus/helpers/socket.h>
  30. #include <log4cplus/thread/syncprims.h>
  31. #include <log4cplus/thread/threads.h>
  32. namespace log4cplus
  33. {
  34. #ifndef UNICODE
  35. std::size_t const LOG4CPLUS_MAX_MESSAGE_SIZE = 8*1024;
  36. #else
  37. std::size_t const LOG4CPLUS_MAX_MESSAGE_SIZE = 2*8*1024;
  38. #endif
  39. /**
  40. * Sends {@link spi::InternalLoggingEvent} objects to a remote a log server.
  41. *
  42. * The SocketAppender has the following properties:
  43. *
  44. * <ul>
  45. *
  46. * <li>Remote logging is non-intrusive as far as the log event
  47. * is concerned. In other words, the event will be logged with
  48. * the same time stamp, NDC, location info as if it were logged
  49. * locally by the client.
  50. *
  51. * <li>SocketAppenders do not use a layout.
  52. *
  53. * <li>Remote logging uses the TCP protocol. Consequently, if
  54. * the server is reachable, then log events will eventually arrive
  55. * at the server.
  56. *
  57. * <li>If the remote server is down, the logging requests are
  58. * simply dropped. However, if and when the server comes back up,
  59. * then event transmission is resumed transparently. This
  60. * transparent reconneciton is performed by a <em>connector</em>
  61. * thread which periodically attempts to connect to the server.
  62. *
  63. * <li>Logging events are automatically <em>buffered</em> by the
  64. * native TCP implementation. This means that if the link to server
  65. * is slow but still faster than the rate of (log) event production
  66. * by the client, the client will not be affected by the slow
  67. * network connection. However, if the network connection is slower
  68. * then the rate of event production, then the client can only
  69. * progress at the network rate. In particular, if the network link
  70. * to the the server is down, the client will be blocked.
  71. *
  72. * <li>On the other hand, if the network link is up, but the server
  73. * is down, the client will not be blocked when making log requests
  74. * but the log events will be lost due to server unavailability.
  75. * </ul>
  76. *
  77. * <h3>Properties</h3>
  78. * <dl>
  79. * <dt><tt>host</tt></dt>
  80. * <dd>Remote host name to connect and send events to.</dd>
  81. *
  82. * <dt><tt>port</tt></dt>
  83. * <dd>Port on remote host to send events to.</dd>
  84. *
  85. * <dt><tt>ServerName</tt></dt>
  86. * <dd>Host name of event's origin prepended to each event.</dd>
  87. *
  88. * </dl>
  89. */
  90. class LOG4CPLUS_EXPORT SocketAppender : public Appender {
  91. public:
  92. // Ctors
  93. SocketAppender(const log4cplus::tstring& host, unsigned short port,
  94. const log4cplus::tstring& serverName = tstring());
  95. SocketAppender(const log4cplus::helpers::Properties & properties);
  96. // Dtor
  97. ~SocketAppender();
  98. // Methods
  99. virtual void close();
  100. protected:
  101. void openSocket();
  102. void initConnector ();
  103. virtual void append(const spi::InternalLoggingEvent& event);
  104. // Data
  105. log4cplus::helpers::Socket socket;
  106. log4cplus::tstring host;
  107. unsigned int port;
  108. log4cplus::tstring serverName;
  109. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  110. class LOG4CPLUS_EXPORT ConnectorThread;
  111. friend class ConnectorThread;
  112. class LOG4CPLUS_EXPORT ConnectorThread
  113. : public thread::AbstractThread
  114. {
  115. public:
  116. ConnectorThread (SocketAppender &);
  117. virtual ~ConnectorThread ();
  118. virtual void run();
  119. void terminate ();
  120. void trigger ();
  121. protected:
  122. SocketAppender & sa;
  123. thread::ManualResetEvent trigger_ev;
  124. bool exit_flag;
  125. };
  126. volatile bool connected;
  127. helpers::SharedObjectPtr<ConnectorThread> connector;
  128. #endif
  129. private:
  130. // Disallow copying of instances of this class
  131. SocketAppender(const SocketAppender&);
  132. SocketAppender& operator=(const SocketAppender&);
  133. };
  134. namespace helpers {
  135. LOG4CPLUS_EXPORT
  136. void convertToBuffer (SocketBuffer & buffer,
  137. const log4cplus::spi::InternalLoggingEvent& event,
  138. const log4cplus::tstring& serverName);
  139. LOG4CPLUS_EXPORT
  140. log4cplus::spi::InternalLoggingEvent readFromBuffer(SocketBuffer& buffer);
  141. } // end namespace helpers
  142. } // end namespace log4cplus
  143. #endif // LOG4CPLUS_SOCKET_APPENDER_HEADER_