socketappender.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // -*- C++ -*-
  2. // Module: LOG4CPLUS
  3. // File: socketappender.h
  4. // Created: 5/2003
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2003-2015 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. #include <log4cplus/helpers/connectorthread.h>
  33. namespace log4cplus
  34. {
  35. #ifndef UNICODE
  36. std::size_t const LOG4CPLUS_MAX_MESSAGE_SIZE = 8*1024;
  37. #else
  38. std::size_t const LOG4CPLUS_MAX_MESSAGE_SIZE = 2*8*1024;
  39. #endif
  40. /**
  41. * Sends {@link spi::InternalLoggingEvent} objects to a remote a log server.
  42. *
  43. * The SocketAppender has the following properties:
  44. *
  45. * <ul>
  46. *
  47. * <li>Remote logging is non-intrusive as far as the log event
  48. * is concerned. In other words, the event will be logged with
  49. * the same time stamp, NDC, location info as if it were logged
  50. * locally by the client.
  51. *
  52. * <li>SocketAppenders do not use a layout.
  53. *
  54. * <li>Remote logging uses the TCP protocol. Consequently, if
  55. * the server is reachable, then log events will eventually arrive
  56. * at the server.
  57. *
  58. * <li>If the remote server is down, the logging requests are
  59. * simply dropped. However, if and when the server comes back up,
  60. * then event transmission is resumed transparently. This
  61. * transparent reconneciton is performed by a <em>connector</em>
  62. * thread which periodically attempts to connect to the server.
  63. *
  64. * <li>Logging events are automatically <em>buffered</em> by the
  65. * native TCP implementation. This means that if the link to server
  66. * is slow but still faster than the rate of (log) event production
  67. * by the client, the client will not be affected by the slow
  68. * network connection. However, if the network connection is slower
  69. * then the rate of event production, then the client can only
  70. * progress at the network rate. In particular, if the network link
  71. * to the the server is down, the client will be blocked.
  72. *
  73. * <li>On the other hand, if the network link is up, but the server
  74. * is down, the client will not be blocked when making log requests
  75. * but the log events will be lost due to server unavailability.
  76. * </ul>
  77. *
  78. * <h3>Properties</h3>
  79. * <dl>
  80. * <dt><tt>host</tt></dt>
  81. * <dd>Remote host name to connect and send events to.</dd>
  82. *
  83. * <dt><tt>port</tt></dt>
  84. * <dd>Port on remote host to send events to.</dd>
  85. *
  86. * <dt><tt>ServerName</tt></dt>
  87. * <dd>Host name of event's origin prepended to each event.</dd>
  88. *
  89. * </dl>
  90. */
  91. class LOG4CPLUS_EXPORT SocketAppender
  92. : public Appender
  93. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  94. , protected virtual helpers::IConnectorThreadClient
  95. #endif
  96. {
  97. public:
  98. // Ctors
  99. SocketAppender(const log4cplus::tstring& host, unsigned short port,
  100. const log4cplus::tstring& serverName = tstring());
  101. SocketAppender(const log4cplus::helpers::Properties & properties);
  102. // Dtor
  103. ~SocketAppender();
  104. // Methods
  105. virtual void close();
  106. protected:
  107. void openSocket();
  108. void initConnector ();
  109. virtual void append(const spi::InternalLoggingEvent& event);
  110. // Data
  111. log4cplus::helpers::Socket socket;
  112. log4cplus::tstring host;
  113. unsigned int port;
  114. log4cplus::tstring serverName;
  115. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  116. virtual thread::Mutex const & ctcGetAccessMutex () const;
  117. virtual helpers::Socket & ctcGetSocket ();
  118. virtual helpers::Socket ctcConnect ();
  119. virtual void ctcSetConnected ();
  120. volatile bool connected;
  121. helpers::SharedObjectPtr<helpers::ConnectorThread> connector;
  122. #endif
  123. private:
  124. // Disallow copying of instances of this class
  125. SocketAppender(const SocketAppender&);
  126. SocketAppender& operator=(const SocketAppender&);
  127. };
  128. namespace helpers {
  129. LOG4CPLUS_EXPORT
  130. void convertToBuffer (SocketBuffer & buffer,
  131. const log4cplus::spi::InternalLoggingEvent& event,
  132. const log4cplus::tstring& serverName);
  133. LOG4CPLUS_EXPORT
  134. log4cplus::spi::InternalLoggingEvent readFromBuffer(SocketBuffer& buffer);
  135. } // end namespace helpers
  136. } // end namespace log4cplus
  137. #endif // LOG4CPLUS_SOCKET_APPENDER_HEADER_