1 | /*
|
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
4 | * this work for additional information regarding copyright ownership.
|
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
6 | * (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef _LOG4CXX_NET_SOCKET_HUB_APPENDER_H
|
---|
19 | #define _LOG4CXX_NET_SOCKET_HUB_APPENDER_H
|
---|
20 |
|
---|
21 | #if defined(_MSC_VER)
|
---|
22 | #pragma warning ( push )
|
---|
23 | #pragma warning ( disable: 4231 4251 4275 4786 )
|
---|
24 | #endif
|
---|
25 |
|
---|
26 |
|
---|
27 | #include <log4cxx/appenderskeleton.h>
|
---|
28 | #include <vector>
|
---|
29 | #include <log4cxx/helpers/thread.h>
|
---|
30 | #include <log4cxx/helpers/objectoutputstream.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | namespace log4cxx
|
---|
34 | {
|
---|
35 | namespace helpers {
|
---|
36 | class ObjectOutputStream;
|
---|
37 | typedef ObjectPtrT<ObjectOutputStream> ObjectOutputStreamPtr;
|
---|
38 | }
|
---|
39 | namespace net
|
---|
40 | {
|
---|
41 | LOG4CXX_LIST_DEF(ObjectOutputStreamList, log4cxx::helpers::ObjectOutputStreamPtr);
|
---|
42 |
|
---|
43 | /**
|
---|
44 | Sends {@link log4cxx::spi::LoggingEvent LoggingEvent} objects to a set of remote log
|
---|
45 | servers, usually a SocketNode.
|
---|
46 |
|
---|
47 | <p>Acts just like SocketAppender except that instead of
|
---|
48 | connecting to a given remote log server,
|
---|
49 | <code>SocketHubAppender</code> accepts connections from the remote
|
---|
50 | log servers as clients. It can accept more than one connection.
|
---|
51 | When a log event is received, the event is sent to the set of
|
---|
52 | currently connected remote log servers. Implemented this way it does
|
---|
53 | not require any update to the configuration file to send data to
|
---|
54 | another remote log server. The remote log server simply connects to
|
---|
55 | the host and port the <code>SocketHubAppender</code> is running on.
|
---|
56 |
|
---|
57 | <p>The <code>SocketHubAppender</code> does not store events such
|
---|
58 | that the remote side will events that arrived after the
|
---|
59 | establishment of its connection. Once connected, events arrive in
|
---|
60 | order as guaranteed by the TCP protocol.
|
---|
61 |
|
---|
62 | <p>This implementation borrows heavily from the SocketAppender.
|
---|
63 |
|
---|
64 | <p>The SocketHubAppender has the following characteristics:
|
---|
65 |
|
---|
66 | - If sent to a SocketNode, logging is non-intrusive as
|
---|
67 | far as the log event is concerned. In other words, the event will be
|
---|
68 | logged with the same time stamp, NDC,
|
---|
69 | location info as if it were logged locally.
|
---|
70 |
|
---|
71 | - <code>SocketHubAppender</code> does not use a layout. It
|
---|
72 | ships a serialized spi::LoggingEvent object to the remote side.
|
---|
73 |
|
---|
74 | - <code>SocketHubAppender</code> relies on the TCP
|
---|
75 | protocol. Consequently, if the remote side is reachable, then log
|
---|
76 | events will eventually arrive at remote client.
|
---|
77 |
|
---|
78 | - If no remote clients are attached, the logging requests are
|
---|
79 | simply dropped.
|
---|
80 |
|
---|
81 | - Logging events are automatically <em>buffered</em> by the
|
---|
82 | native TCP implementation. This means that if the link to remote
|
---|
83 | client is slow but still faster than the rate of (log) event
|
---|
84 | production, the application will not be affected by the slow network
|
---|
85 | connection. However, if the network connection is slower then the
|
---|
86 | rate of event production, then the local application can only
|
---|
87 | progress at the network rate. In particular, if the network link to
|
---|
88 | the the remote client is down, the application will be blocked.
|
---|
89 | @n @n On the other hand, if the network link is up, but the remote
|
---|
90 | client is down, the client will not be blocked when making log
|
---|
91 | requests but the log events will be lost due to client
|
---|
92 | unavailability.
|
---|
93 | @n @n The single remote client case extends to multiple clients
|
---|
94 | connections. The rate of logging will be determined by the slowest
|
---|
95 | link.
|
---|
96 |
|
---|
97 | - If the application hosting the <code>SocketHubAppender</code>
|
---|
98 | exits before the <code>SocketHubAppender</code> is closed either
|
---|
99 | explicitly or subsequent to garbage collection, then there might
|
---|
100 | be untransmitted data in the pipe which might be lost. This is a
|
---|
101 | common problem on Windows based systems.
|
---|
102 | @n @n To avoid lost data, it is usually sufficient to #close
|
---|
103 | the <code>SocketHubAppender</code> either explicitly or by calling
|
---|
104 | the LogManager#shutdown method before
|
---|
105 | exiting the application.
|
---|
106 | */
|
---|
107 |
|
---|
108 | class LOG4CXX_EXPORT SocketHubAppender : public AppenderSkeleton
|
---|
109 | {
|
---|
110 | private:
|
---|
111 | /**
|
---|
112 | The default port number of the ServerSocket will be created on.
|
---|
113 | */
|
---|
114 | static int DEFAULT_PORT;
|
---|
115 |
|
---|
116 | int port;
|
---|
117 | ObjectOutputStreamList streams;
|
---|
118 | bool locationInfo;
|
---|
119 |
|
---|
120 | public:
|
---|
121 | DECLARE_LOG4CXX_OBJECT(SocketHubAppender)
|
---|
122 | BEGIN_LOG4CXX_CAST_MAP()
|
---|
123 | LOG4CXX_CAST_ENTRY(SocketHubAppender)
|
---|
124 | LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton)
|
---|
125 | END_LOG4CXX_CAST_MAP()
|
---|
126 |
|
---|
127 | SocketHubAppender();
|
---|
128 | ~SocketHubAppender();
|
---|
129 |
|
---|
130 | /**
|
---|
131 | Connects to remote server at <code>address</code> and <code>port</code>.
|
---|
132 | */
|
---|
133 | SocketHubAppender(int port) ;
|
---|
134 |
|
---|
135 | /**
|
---|
136 | Set up the socket server on the specified port.
|
---|
137 | */
|
---|
138 | virtual void activateOptions(log4cxx::helpers::Pool& p);
|
---|
139 |
|
---|
140 | /**
|
---|
141 | Set options
|
---|
142 | */
|
---|
143 | virtual void setOption(const LogString& option, const LogString& value);
|
---|
144 |
|
---|
145 | virtual void close();
|
---|
146 |
|
---|
147 | /**
|
---|
148 | Append an event to all of current connections. */
|
---|
149 | virtual void append(const spi::LoggingEventPtr& event, log4cxx::helpers::Pool& p);
|
---|
150 |
|
---|
151 | /**
|
---|
152 | The SocketHubAppender does not use a layout. Hence, this method returns
|
---|
153 | <code>false</code>. */
|
---|
154 | virtual bool requiresLayout() const
|
---|
155 | { return false; }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | The <b>Port</b> option takes a positive integer representing
|
---|
159 | the port where the server is waiting for connections. */
|
---|
160 | inline void setPort(int port1)
|
---|
161 | { this->port = port1; }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | Returns value of the <b>Port</b> option. */
|
---|
165 | inline int getPort() const
|
---|
166 | { return port; }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | The <b>LocationInfo</b> option takes a boolean value. If true,
|
---|
170 | the information sent to the remote host will include location
|
---|
171 | information. By default no location information is sent to the server. */
|
---|
172 | inline void setLocationInfo(bool locationInfo1)
|
---|
173 | { this->locationInfo = locationInfo1; }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | Returns value of the <b>LocationInfo</b> option. */
|
---|
177 | inline bool getLocationInfo() const
|
---|
178 | { return locationInfo; }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | Start the ServerMonitor thread. */
|
---|
182 | private:
|
---|
183 | void startServer();
|
---|
184 |
|
---|
185 | helpers::Thread thread;
|
---|
186 | static void* LOG4CXX_THREAD_FUNC monitor(apr_thread_t* thread, void* data);
|
---|
187 |
|
---|
188 | }; // class SocketHubAppender
|
---|
189 | LOG4CXX_PTR_DEF(SocketHubAppender);
|
---|
190 | } // namespace net
|
---|
191 | } // namespace log4cxx
|
---|
192 |
|
---|
193 |
|
---|
194 | #if defined(_MSC_VER)
|
---|
195 | #pragma warning ( pop )
|
---|
196 | #endif
|
---|
197 |
|
---|
198 | #endif // _LOG4CXX_NET_SOCKET_HUB_APPENDER_H
|
---|