/src/logging-log4cxx/src/main/cpp/telnetappender.cpp
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 | | #include <log4cxx/net/telnetappender.h> |
19 | | #include <log4cxx/helpers/loglog.h> |
20 | | #include <log4cxx/helpers/optionconverter.h> |
21 | | #include <log4cxx/helpers/stringhelper.h> |
22 | | #include <log4cxx/helpers/serversocket.h> |
23 | | #include <log4cxx/helpers/charsetencoder.h> |
24 | | #include <log4cxx/helpers/bytebuffer.h> |
25 | | #include <log4cxx/helpers/threadutility.h> |
26 | | #include <log4cxx/private/appenderskeleton_priv.h> |
27 | | #if LOG4CXX_ABI_VERSION <= 15 |
28 | | #include <log4cxx/private/aprsocket.h> |
29 | | #endif |
30 | | #include <mutex> |
31 | | #include <thread> |
32 | | #include <vector> |
33 | | |
34 | | #if LOG4CXX_EVENTS_AT_EXIT |
35 | | #include <log4cxx/private/atexitregistry.h> |
36 | | #endif |
37 | | |
38 | | using namespace LOG4CXX_NS; |
39 | | using namespace LOG4CXX_NS::helpers; |
40 | | using namespace LOG4CXX_NS::net; |
41 | | |
42 | | struct Connection |
43 | | { |
44 | | helpers::SocketPtr s; |
45 | | size_t sentCount; |
46 | | }; |
47 | | LOG4CXX_LIST_DEF(ConnectionList, Connection); |
48 | | |
49 | | IMPLEMENT_LOG4CXX_OBJECT(TelnetAppender) |
50 | | |
51 | | struct TelnetAppender::TelnetAppenderPriv : public AppenderSkeletonPrivate |
52 | | { |
53 | 0 | TelnetAppenderPriv( int port, int maxConnections ) : AppenderSkeletonPrivate(), |
54 | 0 | port(port), |
55 | 0 | connections(maxConnections), |
56 | 0 | encoding(LOG4CXX_STR("UTF-8")), |
57 | 0 | encoder(CharsetEncoder::getUTF8Encoder()), |
58 | 0 | activeConnections(0) |
59 | | #if LOG4CXX_EVENTS_AT_EXIT |
60 | | , atExitRegistryRaii([this] |
61 | | { |
62 | | if (setClosed()) |
63 | | stopAcceptingConnections(); |
64 | | }) |
65 | | #endif |
66 | 0 | { } |
67 | | |
68 | | ~TelnetAppenderPriv() |
69 | 0 | { stopAcceptingConnections(); } |
70 | | |
71 | | int port; |
72 | | LogString hostname; |
73 | | bool reuseAddress = false; |
74 | | bool nonBlocking = false; |
75 | | ConnectionList connections; |
76 | | LogString encoding; |
77 | | LOG4CXX_NS::helpers::CharsetEncoderPtr encoder; |
78 | | std::unique_ptr<helpers::ServerSocket> serverSocket; |
79 | | std::thread sh; |
80 | | size_t activeConnections; |
81 | | size_t eventCount{ 0 }; |
82 | | |
83 | | #if LOG4CXX_EVENTS_AT_EXIT |
84 | | helpers::AtExitRegistry::Raii atExitRegistryRaii; |
85 | | #endif |
86 | | |
87 | | void stopAcceptingConnections() |
88 | 0 | { |
89 | 0 | if (!this->serverSocket) |
90 | 0 | return; |
91 | | // Interrupt accept() |
92 | 0 | try |
93 | 0 | { |
94 | 0 | this->serverSocket->close(); |
95 | 0 | } |
96 | 0 | catch (Exception&) |
97 | 0 | { |
98 | 0 | } |
99 | 0 | if (this->sh.joinable()) |
100 | 0 | this->sh.join(); |
101 | 0 | } |
102 | | |
103 | | void close(); |
104 | | }; |
105 | | |
106 | 0 | #define _priv static_cast<TelnetAppenderPriv*>(m_priv.get()) |
107 | | |
108 | | /** The default telnet server port */ |
109 | | const int TelnetAppender::DEFAULT_PORT = 23; |
110 | | |
111 | | /** The maximum number of concurrent connections */ |
112 | | const int TelnetAppender::MAX_CONNECTIONS = 20; |
113 | | |
114 | | TelnetAppender::TelnetAppender() |
115 | 0 | : AppenderSkeleton (std::make_unique<TelnetAppenderPriv>(DEFAULT_PORT, MAX_CONNECTIONS)) |
116 | 0 | { |
117 | 0 | } Unexecuted instantiation: log4cxx::net::TelnetAppender::TelnetAppender() Unexecuted instantiation: log4cxx::net::TelnetAppender::TelnetAppender() |
118 | | |
119 | | TelnetAppender::~TelnetAppender() |
120 | 0 | { |
121 | 0 | if (_priv->setClosed()) |
122 | 0 | _priv->close(); |
123 | 0 | } |
124 | | |
125 | | void TelnetAppender::activateOptions(Pool& /* p */) |
126 | 0 | { |
127 | 0 | if (_priv->serverSocket == NULL) |
128 | 0 | { |
129 | 0 | _priv->serverSocket = ServerSocket::create(_priv->port, _priv->reuseAddress, _priv->hostname); |
130 | 0 | _priv->serverSocket->setSoTimeout(1000); |
131 | 0 | } |
132 | |
|
133 | 0 | if (!_priv->sh.joinable()) |
134 | 0 | _priv->sh = ThreadUtility::instance()->createThread( LOG4CXX_STR("TelnetAppender"), &TelnetAppender::acceptConnections, this ); |
135 | 0 | } |
136 | | |
137 | | void TelnetAppender::setOption(const LogString& option, |
138 | | const LogString& value) |
139 | 0 | { |
140 | 0 | if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("PORT"), LOG4CXX_STR("port"))) |
141 | 0 | { |
142 | 0 | setPort(OptionConverter::toInt(value, DEFAULT_PORT)); |
143 | 0 | } |
144 | 0 | else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("MAXCONNECTIONS"), LOG4CXX_STR("maxconnections"))) |
145 | 0 | { |
146 | 0 | setMaxConnections(OptionConverter::toInt(value, MAX_CONNECTIONS)); |
147 | 0 | } |
148 | 0 | else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("ENCODING"), LOG4CXX_STR("encoding"))) |
149 | 0 | { |
150 | 0 | setEncoding(value); |
151 | 0 | } |
152 | 0 | else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("NONBLOCKING"), LOG4CXX_STR("nonblocking"))) |
153 | 0 | { |
154 | 0 | setNonBlocking(OptionConverter::toBoolean(value, true)); |
155 | 0 | } |
156 | 0 | else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("REUSEADDRESS"), LOG4CXX_STR("reuseaddress"))) |
157 | 0 | { |
158 | 0 | setReuseAddress(OptionConverter::toBoolean(value, true)); |
159 | 0 | } |
160 | 0 | else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("HOSTNAME"), LOG4CXX_STR("hostname"))) |
161 | 0 | { |
162 | 0 | setHostname(value); |
163 | 0 | } |
164 | 0 | else |
165 | 0 | { |
166 | 0 | AppenderSkeleton::setOption(option, value); |
167 | 0 | } |
168 | 0 | } |
169 | | |
170 | | LogString TelnetAppender::getEncoding() const |
171 | 0 | { |
172 | 0 | std::lock_guard<std::recursive_mutex> lock(_priv->mutex); |
173 | 0 | return _priv->encoding; |
174 | 0 | } |
175 | | |
176 | | void TelnetAppender::setEncoding(const LogString& value) |
177 | 0 | { |
178 | 0 | std::lock_guard<std::recursive_mutex> lock(_priv->mutex); |
179 | 0 | _priv->encoder = CharsetEncoder::getEncoder(value); |
180 | 0 | _priv->encoding = value; |
181 | 0 | } |
182 | | |
183 | | |
184 | | void TelnetAppender::close() |
185 | 0 | { |
186 | 0 | if (_priv->setClosed()) |
187 | 0 | _priv->close(); |
188 | 0 | } |
189 | | |
190 | | void TelnetAppender::TelnetAppenderPriv::close() |
191 | 0 | { |
192 | 0 | this->stopAcceptingConnections(); |
193 | 0 | std::lock_guard<std::recursive_mutex> lock(this->mutex); |
194 | 0 | if (this->eventCount && helpers::LogLog::isDebugEnabled()) |
195 | 0 | { |
196 | 0 | Pool p; |
197 | 0 | LogString msg = LOG4CXX_STR("TelnetAppender eventCount "); |
198 | 0 | helpers::StringHelper::toString(this->eventCount, p, msg); |
199 | 0 | helpers::LogLog::debug(msg); |
200 | 0 | } |
201 | 0 | SocketPtr nullSocket; |
202 | 0 | int connectionNumber{ 0 }; |
203 | 0 | for (auto& item : this->connections) |
204 | 0 | { |
205 | 0 | ++connectionNumber; |
206 | 0 | if (item.s) |
207 | 0 | { |
208 | 0 | item.s->close(); |
209 | 0 | if (this->eventCount && helpers::LogLog::isDebugEnabled()) |
210 | 0 | { |
211 | 0 | Pool p; |
212 | 0 | LogString msg = LOG4CXX_STR("TelnetAppender connection "); |
213 | 0 | helpers::StringHelper::toString(connectionNumber, p, msg); |
214 | 0 | msg += LOG4CXX_STR(" sentCount "); |
215 | 0 | helpers::StringHelper::toString(item.sentCount, p, msg); |
216 | 0 | helpers::LogLog::debug(msg); |
217 | 0 | } |
218 | 0 | item = Connection{ nullSocket, 0 }; |
219 | 0 | } |
220 | 0 | } |
221 | 0 | this->activeConnections = 0; |
222 | 0 | } |
223 | | |
224 | | |
225 | | void TelnetAppender::write(ByteBuffer& buf) |
226 | 0 | { |
227 | 0 | int connectionNumber{ 0 }; |
228 | 0 | for (auto& item :_priv->connections) |
229 | 0 | { |
230 | 0 | ++connectionNumber; |
231 | 0 | if (item.s) |
232 | 0 | { |
233 | 0 | try |
234 | 0 | { |
235 | 0 | ByteBuffer b(buf.current(), buf.remaining()); |
236 | 0 | item.s->write(b); |
237 | 0 | ++item.sentCount; |
238 | 0 | } |
239 | 0 | catch (const Exception& e) |
240 | 0 | { |
241 | 0 | if (helpers::LogLog::isDebugEnabled()) |
242 | 0 | { |
243 | 0 | Pool p; |
244 | 0 | LogString msg(LOG4CXX_STR("TelnetAppender connection ")); |
245 | 0 | helpers::StringHelper::toString(connectionNumber, p, msg); |
246 | 0 | msg += LOG4CXX_STR(" sentCount "); |
247 | 0 | helpers::StringHelper::toString(item.sentCount, p, msg); |
248 | 0 | msg += LOG4CXX_STR("/"); |
249 | 0 | helpers::StringHelper::toString(_priv->eventCount, p, msg); |
250 | 0 | helpers::LogLog::warn(msg, e); |
251 | 0 | } |
252 | 0 | item.s.reset(); |
253 | 0 | _priv->activeConnections--; |
254 | 0 | } |
255 | 0 | } |
256 | 0 | } |
257 | 0 | } |
258 | | |
259 | | void TelnetAppender::writeStatus(const SocketPtr& socket, const LogString& msg, Pool& p) |
260 | 0 | { |
261 | 0 | size_t bytesSize = msg.size() * 2; |
262 | 0 | char* bytes = p.pstralloc(bytesSize); |
263 | |
|
264 | 0 | LogString::const_iterator msgIter(msg.begin()); |
265 | 0 | ByteBuffer buf(bytes, bytesSize); |
266 | |
|
267 | 0 | while (msgIter != msg.end()) |
268 | 0 | { |
269 | 0 | _priv->encoder->encode(msg, msgIter, buf); |
270 | 0 | buf.flip(); |
271 | 0 | socket->write(buf); |
272 | 0 | buf.clear(); |
273 | 0 | } |
274 | 0 | } |
275 | | |
276 | | void TelnetAppender::append(const spi::LoggingEventPtr& event, Pool& p) |
277 | 0 | { |
278 | 0 | ++_priv->eventCount; |
279 | 0 | if (0 < _priv->activeConnections) |
280 | 0 | { |
281 | 0 | LogString msg; |
282 | 0 | if (_priv->layout) |
283 | 0 | _priv->layout->format(msg, event, p); |
284 | 0 | else |
285 | 0 | msg = event->getRenderedMessage(); |
286 | 0 | msg.append(LOG4CXX_STR("\r\n")); |
287 | 0 | size_t bytesSize = msg.size() * 2; |
288 | 0 | char* bytes = p.pstralloc(bytesSize); |
289 | |
|
290 | 0 | LogString::const_iterator msgIter(msg.begin()); |
291 | 0 | ByteBuffer buf(bytes, bytesSize); |
292 | |
|
293 | 0 | std::lock_guard<std::recursive_mutex> lock(_priv->mutex); |
294 | |
|
295 | 0 | while (msgIter != msg.end()) |
296 | 0 | { |
297 | 0 | log4cxx_status_t stat = _priv->encoder->encode(msg, msgIter, buf); |
298 | 0 | buf.flip(); |
299 | 0 | write(buf); |
300 | 0 | buf.clear(); |
301 | |
|
302 | 0 | if (CharsetEncoder::isError(stat)) |
303 | 0 | { |
304 | 0 | LogString unrepresented(1, 0x3F /* '?' */); |
305 | 0 | LogString::const_iterator unrepresentedIter(unrepresented.begin()); |
306 | 0 | stat = _priv->encoder->encode(unrepresented, unrepresentedIter, buf); |
307 | 0 | buf.flip(); |
308 | 0 | write(buf); |
309 | 0 | buf.clear(); |
310 | 0 | msgIter++; |
311 | 0 | } |
312 | 0 | } |
313 | 0 | } |
314 | 0 | } |
315 | | |
316 | | void TelnetAppender::acceptConnections() |
317 | 0 | { |
318 | | |
319 | | // main loop; is left when This->closed is != 0 after an accept() |
320 | 0 | while (true) |
321 | 0 | { |
322 | 0 | try |
323 | 0 | { |
324 | 0 | SocketPtr newClient = _priv->serverSocket->accept(); |
325 | | #if 15 < LOG4CXX_ABI_VERSION |
326 | | newClient->setNonBlocking(_priv->nonBlocking); |
327 | | #else |
328 | 0 | if (auto p = dynamic_cast<APRSocket*>(newClient.get())) |
329 | 0 | p->setNonBlocking(_priv->nonBlocking); |
330 | 0 | #endif |
331 | 0 | bool done = _priv->closed; |
332 | |
|
333 | 0 | if (done) |
334 | 0 | { |
335 | 0 | Pool p; |
336 | 0 | writeStatus(newClient, LOG4CXX_STR("Log closed.\r\n"), p); |
337 | 0 | newClient->close(); |
338 | |
|
339 | 0 | break; |
340 | 0 | } |
341 | | |
342 | 0 | size_t count = _priv->activeConnections; |
343 | |
|
344 | 0 | if (count >= _priv->connections.size()) |
345 | 0 | { |
346 | 0 | Pool p; |
347 | 0 | writeStatus(newClient, LOG4CXX_STR("Too many connections.\r\n"), p); |
348 | 0 | newClient->close(); |
349 | 0 | } |
350 | 0 | else |
351 | 0 | { |
352 | | // |
353 | | // find unoccupied connection |
354 | | // |
355 | 0 | std::lock_guard<std::recursive_mutex> lock(_priv->mutex); |
356 | |
|
357 | 0 | int connectionNumber{ 0 }; |
358 | 0 | for (auto& item : _priv->connections) |
359 | 0 | { |
360 | 0 | ++connectionNumber; |
361 | 0 | if (!item.s) |
362 | 0 | { |
363 | 0 | item = Connection{ newClient, 0 }; |
364 | 0 | _priv->activeConnections++; |
365 | 0 | if (helpers::LogLog::isDebugEnabled()) |
366 | 0 | { |
367 | 0 | Pool p; |
368 | 0 | LogString msg = LOG4CXX_STR("TelnetAppender new connection "); |
369 | 0 | helpers::StringHelper::toString(connectionNumber, p, msg); |
370 | 0 | msg += LOG4CXX_STR("/"); |
371 | 0 | helpers::StringHelper::toString(_priv->activeConnections, p, msg); |
372 | 0 | helpers::LogLog::debug(msg); |
373 | 0 | } |
374 | |
|
375 | 0 | break; |
376 | 0 | } |
377 | 0 | } |
378 | |
|
379 | 0 | Pool p; |
380 | 0 | LogString oss(LOG4CXX_STR("TelnetAppender v1.0 (")); |
381 | 0 | StringHelper::toString((int) count + 1, p, oss); |
382 | 0 | oss += LOG4CXX_STR(" active connections)\r\n\r\n"); |
383 | 0 | writeStatus(newClient, oss, p); |
384 | 0 | } |
385 | 0 | } |
386 | 0 | catch (InterruptedIOException&) |
387 | 0 | { |
388 | 0 | if (_priv->closed) |
389 | 0 | { |
390 | 0 | break; |
391 | 0 | } |
392 | 0 | } |
393 | 0 | catch (Exception& e) |
394 | 0 | { |
395 | 0 | if (!_priv->closed) |
396 | 0 | { |
397 | 0 | LogLog::error(LOG4CXX_STR("Encountered error while in SocketHandler loop."), e); |
398 | 0 | } |
399 | 0 | else |
400 | 0 | { |
401 | 0 | break; |
402 | 0 | } |
403 | 0 | } |
404 | 0 | } |
405 | |
|
406 | 0 | } |
407 | | |
408 | | int TelnetAppender::getPort() const |
409 | 0 | { |
410 | 0 | return _priv->port; |
411 | 0 | } |
412 | | |
413 | | void TelnetAppender::setPort(int port1) |
414 | 0 | { |
415 | 0 | _priv->port = port1; |
416 | 0 | } |
417 | | |
418 | | LogString TelnetAppender::getHostname() const |
419 | 0 | { |
420 | 0 | return _priv->hostname; |
421 | 0 | } |
422 | | |
423 | | void TelnetAppender::setHostname(const LogString& hostname) |
424 | 0 | { |
425 | 0 | _priv->hostname = hostname; |
426 | 0 | } |
427 | | |
428 | | int TelnetAppender::getMaxConnections() const |
429 | 0 | { |
430 | 0 | return static_cast<int>(_priv->connections.size()); |
431 | 0 | } |
432 | | |
433 | | void TelnetAppender::setMaxConnections(int newValue) |
434 | 0 | { |
435 | 0 | std::lock_guard<std::recursive_mutex> lock(_priv->mutex); |
436 | 0 | if (_priv->connections.size() < newValue) |
437 | 0 | _priv->connections.resize(newValue); |
438 | 0 | else while (newValue < _priv->connections.size()) |
439 | 0 | { |
440 | 0 | auto item = _priv->connections.back(); |
441 | 0 | _priv->connections.pop_back(); |
442 | 0 | if (item.s) |
443 | 0 | { |
444 | 0 | item.s->close(); |
445 | 0 | --_priv->activeConnections; |
446 | 0 | } |
447 | 0 | } |
448 | 0 | } |
449 | | |
450 | | void TelnetAppender::setNonBlocking(bool newValue) |
451 | 0 | { |
452 | 0 | _priv->nonBlocking = newValue; |
453 | 0 | } |
454 | | |
455 | | void TelnetAppender::setReuseAddress(bool reuseAddress) |
456 | 0 | { |
457 | 0 | _priv->reuseAddress = reuseAddress; |
458 | 0 | } |
459 | | |
460 | | bool TelnetAppender::requiresLayout() const |
461 | 0 | { |
462 | 0 | return false; |
463 | 0 | } |