/src/logging-log4cxx/src/main/include/log4cxx/net/socketappenderskeleton.h
Line | Count | Source |
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_APPENDER_SKELETON_H |
19 | | #define _LOG4CXX_NET_SOCKET_APPENDER_SKELETON_H |
20 | | |
21 | | #include <log4cxx/appenderskeleton.h> |
22 | | #include <log4cxx/helpers/inetaddress.h> |
23 | | #if LOG4CXX_ABI_VERSION <= 15 |
24 | | #include <log4cxx/helpers/socket.h> |
25 | | #endif |
26 | | |
27 | | |
28 | | namespace LOG4CXX_NS |
29 | | { |
30 | | |
31 | | namespace net |
32 | | { |
33 | | |
34 | | /** |
35 | | Abstract base class that sends spi::LoggingEvent elements to a remote server. |
36 | | |
37 | | \anchor socket_appender_properties |
38 | | This appender has the following properties: |
39 | | |
40 | | - The event will be logged with the same time stamp, |
41 | | NDC, location info as if it were logged locally by |
42 | | the client. |
43 | | |
44 | | - Remote logging uses the TCP protocol, |
45 | | so if the server is reachable, |
46 | | log events will eventually arrive at the server. |
47 | | |
48 | | - If the remote server is down, the logging requests are simply dropped. |
49 | | However, if and when the server comes back up, |
50 | | then event transmission is resumed transparently. |
51 | | This transparent reconneciton is performed by a <em>connector</em> |
52 | | task which periodically attempts to connect to the server. |
53 | | |
54 | | - Logging events are automatically <em>buffered</em> by the |
55 | | native TCP implementation. This means that if the link to server |
56 | | is slow but still faster than the rate of (log) event production |
57 | | by the client, the client will not be affected by the slow |
58 | | network connection. However, if the network connection is slower |
59 | | then the rate of event production, then the client can only |
60 | | progress at the network rate. In particular, if the network link |
61 | | to the the server is down, the client will be blocked. |
62 | | On the other hand, if the network link is up, but the server |
63 | | is down, the client will not be blocked when making log requests |
64 | | but the log events will be lost due to server unavailability. |
65 | | |
66 | | - If the application hosting this appender exits before it is closed, |
67 | | either explicitly or subsequent to destruction, |
68 | | then there might be untransmitted data in the pipe which might be lost. |
69 | | |
70 | | To avoid lost data, it is usually sufficient to |
71 | | #close the appender either explicitly or by |
72 | | calling the LogManager#shutdown method |
73 | | before exiting the application. |
74 | | |
75 | | A periodic task will connect when the server becomes available. |
76 | | It does this by attempting to open a new connection every |
77 | | <code>reconnectionDelay</code> milliseconds. |
78 | | |
79 | | The periodic task stops trying whenever a connection is established. |
80 | | It will restart attempting to open a new connection to the server |
81 | | when a previously open connection is droppped. |
82 | | */ |
83 | | class LOG4CXX_EXPORT SocketAppenderSkeleton : public AppenderSkeleton |
84 | | { |
85 | | protected: |
86 | | struct SocketAppenderSkeletonPriv; |
87 | | |
88 | | public: |
89 | | SocketAppenderSkeleton(int defaultPort, int reconnectionDelay); |
90 | | ~SocketAppenderSkeleton(); |
91 | | |
92 | | /** |
93 | | Connects to remote server at <code>address</code> and <code>port</code>. |
94 | | */ |
95 | | #if LOG4CXX_ABI_VERSION <= 15 |
96 | | SocketAppenderSkeleton(helpers::InetAddressPtr address, int port, int reconnectionDelay); |
97 | | #else |
98 | | SocketAppenderSkeleton(const helpers::InetAddressPtr& address, int port, int reconnectionDelay); |
99 | | #endif |
100 | | /** |
101 | | Connects to remote server at <code>host</code> and <code>port</code>. |
102 | | */ |
103 | | SocketAppenderSkeleton(const LogString& host, int port, int reconnectionDelay); |
104 | | |
105 | | using spi::OptionHandler::activateOptions; |
106 | | /** |
107 | | \copybrief AppenderSkeleton::activateOptions() |
108 | | |
109 | | Connects to the specified <b>RemoteHost</b> and <b>Port</b>. |
110 | | */ |
111 | | void activateOptions( LOG4CXX_ACTIVATE_OPTIONS_FORMAL_PARAMETERS ) override; |
112 | | |
113 | | void close() override; |
114 | | |
115 | | #if LOG4CXX_ABI_VERSION <= 15 |
116 | | /** |
117 | | * This appender does not use a layout. Hence, this method |
118 | | * returns <code>false</code>. |
119 | | * |
120 | | */ |
121 | | bool requiresLayout() const override |
122 | 0 | { |
123 | 0 | return false; |
124 | 0 | } |
125 | | #endif |
126 | | /** |
127 | | * The <b>RemoteHost</b> option takes a string value which should be |
128 | | * the host name of the server where a |
129 | | * Apache Chainsaw or compatible is running. |
130 | | * */ |
131 | | void setRemoteHost(const LogString& host); |
132 | | |
133 | | /** |
134 | | Returns value of the <b>RemoteHost</b> option. |
135 | | */ |
136 | | const LogString& getRemoteHost() const; |
137 | | |
138 | | /** |
139 | | The <b>Port</b> option takes a positive integer representing |
140 | | the port where the server is waiting for connections. |
141 | | */ |
142 | | void setPort(int port1); |
143 | | |
144 | | /** |
145 | | Returns value of the <b>Port</b> option. |
146 | | */ |
147 | | int getPort() const; |
148 | | |
149 | | /** |
150 | | The <b>LocationInfo</b> option takes a boolean value. If true, |
151 | | the information sent to the remote host will include location |
152 | | information. By default no location information is sent to the server. |
153 | | */ |
154 | | void setLocationInfo(bool locationInfo1); |
155 | | |
156 | | /** |
157 | | Returns value of the <b>LocationInfo</b> option. |
158 | | */ |
159 | | bool getLocationInfo() const; |
160 | | |
161 | | /** |
162 | | The <b>ReconnectionDelay</b> option takes a positive integer |
163 | | representing the number of milliseconds to wait between each |
164 | | failed connection attempt to the server. The default value of |
165 | | this option is 30000 which corresponds to 30 seconds. |
166 | | |
167 | | <p>Setting this option to zero turns off reconnection |
168 | | capability. |
169 | | */ |
170 | | void setReconnectionDelay(int reconnectionDelay1); |
171 | | |
172 | | /** |
173 | | Returns value of the <b>ReconnectionDelay</b> option. |
174 | | */ |
175 | | int getReconnectionDelay() const; |
176 | | |
177 | | #if LOG4CXX_ABI_VERSION <= 15 |
178 | | /** |
179 | | @deprecated This method will be removed in a future version. |
180 | | */ |
181 | | void fireConnector(); |
182 | | #else |
183 | | /** |
184 | | * Use \c newSubclass as the helpers::Socket interface instead of the default implementation. |
185 | | * */ |
186 | | void setSocketSubclass(const LogString& newSubclass); |
187 | | |
188 | | /** |
189 | | The class name used for the helpers::Socket interface implemention. |
190 | | */ |
191 | | const LogString& getSocketSubclass() const; |
192 | | #endif |
193 | | |
194 | | /** |
195 | | \copybrief AppenderSkeleton::setOption() |
196 | | |
197 | | Supported options | Supported values | Default value | |
198 | | -------------- | ---------------- | --------------- | |
199 | | RemoteHost | (\ref inetAddress "1") | - | |
200 | | Port | {int} | (\ref defaultPort "2") | |
201 | | LocationInfo | True,False | False | |
202 | | SocketSubclass | (\ref socketSubclass "3") | APRSocket | |
203 | | |
204 | | \anchor inetAddress (1) A valid internet address. |
205 | | |
206 | | \anchor defaultPort (2) Provided by the derived class. |
207 | | |
208 | | \anchor socketSubclass (3) A registered class derived from helpers::Socket. |
209 | | |
210 | | \sa AppenderSkeleton::setOption() |
211 | | */ |
212 | | void setOption(const LogString& option, const LogString& value) override; |
213 | | |
214 | | protected: |
215 | | SocketAppenderSkeleton(std::unique_ptr<SocketAppenderSkeletonPriv> priv); |
216 | | |
217 | | #if LOG4CXX_ABI_VERSION <= 15 |
218 | | /** |
219 | | @deprecated This method will be removed in a future version. |
220 | | */ |
221 | | virtual void setSocket(helpers::SocketPtr& socket, helpers::Pool& p) = 0; |
222 | | |
223 | | /** |
224 | | @deprecated This method will be removed in a future version. |
225 | | */ |
226 | | virtual void cleanUp(LOG4CXX_NS::helpers::Pool& p) = 0; |
227 | | #endif |
228 | | virtual int getDefaultDelay() const = 0; |
229 | | |
230 | | virtual int getDefaultPort() const = 0; |
231 | | |
232 | | private: |
233 | | |
234 | | SocketAppenderSkeleton(const SocketAppenderSkeleton&); |
235 | | SocketAppenderSkeleton& operator=(const SocketAppenderSkeleton&); |
236 | | |
237 | | }; // class SocketAppenderSkeleton |
238 | | } // namespace net |
239 | | } // namespace log4cxx |
240 | | |
241 | | #endif // _LOG4CXX_NET_SOCKET_APPENDER_SKELETON_H |
242 | | |