1 // Module:  LOG4CPLUS
  2 // File:    socketappender.h
  3 // Created: 5/2003
  4 // Author:  Tad E. Smith
  5 //
  6 //
  7 // Copyright (C) Tad E. Smith  All rights reserved.
  8 //
  9 // This software is published under the terms of the Apache Software
 10 // License version 1.1, a copy of which has been included with this
 11 // distribution in the LICENSE.APL file.
 12 //
 13 
 14 /** @file */
 15 
 16 #ifndef _LOG4CPLUS_SOCKET_APPENDER_HEADER_
 17 #define _LOG4CPLUS_SOCKET_APPENDER_HEADER_
 18 
 19 #include <log4cplus/config.h>
 20 #include <log4cplus/appender.h>
 21 #include <log4cplus/helpers/socket.h>
 22 
 23 #ifndef UNICODE
 24 #  define LOG4CPLUS_MAX_MESSAGE_SIZE (8*1024)
 25 #else
 26 #  define LOG4CPLUS_MAX_MESSAGE_SIZE (2*8*1024)
 27 #endif
 28 
 29 
 30 namespace log4cplus {
 31 
 32     /**
 33      * Sends {@link LoggingEvent} objects to a remote a log server.
 34      *
 35      * <p>The SocketAppender has the following properties:
 36      *
 37      * <ul>
 38      *   <p><li>Remote logging is non-intrusive as far as the log event is
 39      *   concerned. In other words, the event will be logged with the same
 40      *   time stamp, {@link org.apache.log4j.NDC}, location info as if it
 41      *   were logged locally by the client.
 42      *
 43      *   <p><li>SocketAppenders do not use a layout.
 44      *
 45      *   <p><li>Remote logging uses the TCP protocol. Consequently, if
 46      *   the server is reachable, then log events will eventually arrive
 47      *   at the server.
 48      *
 49      *   <p><li>If the remote server is down, the logging requests are
 50      *   simply dropped. However, if and when the server comes back up,
 51      *   then event transmission is resumed transparently. This
 52      *   transparent reconneciton is performed by a <em>connector</em>
 53      *   thread which periodically attempts to connect to the server.
 54      *
 55      *   <p><li>Logging events are automatically <em>buffered</em> by the
 56      *   native TCP implementation. This means that if the link to server
 57      *   is slow but still faster than the rate of (log) event production
 58      *   by the client, the client will not be affected by the slow
 59      *   network connection. However, if the network connection is slower
 60      *   then the rate of event production, then the client can only
 61      *   progress at the network rate. In particular, if the network link
 62      *   to the the server is down, the client will be blocked.
 63      *
 64      *   <p>On the other hand, if the network link is up, but the server
 65      *   is down, the client will not be blocked when making log requests
 66      *   but the log events will be lost due to server unavailability.
 67      */
 68     class LOG4CPLUS_EXPORT SocketAppender : public Appender {
 69     public:
 70       // Ctors
 71         SocketAppender(const log4cplus::tstring& host, int port, 
 72                        const log4cplus::tstring& serverName = tstring());
 73         SocketAppender(const log4cplus::helpers::Properties properties);
 74 
 75       // Dtor
 76         ~SocketAppender();
 77 
 78       // Methods
 79         virtual void close();
 80 
 81     protected:
 82         void openSocket();
 83         virtual void append(const spi::InternalLoggingEvent& event);
 84 
 85       // Data
 86         log4cplus::helpers::Socket socket;
 87         log4cplus::tstring host;
 88         int port;
 89         log4cplus::tstring serverName;
 90 
 91     private:
 92       // Disallow copying of instances of this class
 93         SocketAppender(const SocketAppender&);
 94         SocketAppender& operator=(const SocketAppender&);
 95     };
 96 
 97     namespace helpers {
 98         LOG4CPLUS_EXPORT
 99         SocketBuffer convertToBuffer(const log4cplus::spi::InternalLoggingEvent& event,
100                                      const log4cplus::tstring& serverName);
101 
102         LOG4CPLUS_EXPORT
103         log4cplus::spi::InternalLoggingEvent readFromBuffer(SocketBuffer& buffer);
104     } // end namespace helpers
105 
106 } // end namespace log4cplus
107 
108 #endif // _LOG4CPLUS_SOCKET_APPENDER_HEADER_


syntax highlighted by Code2HTML, v. 0.9.1