/src/logging-log4cxx/src/main/cpp/xmlsocketappender.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/xmlsocketappender.h> | 
| 19 |  | #include <log4cxx/helpers/loglog.h> | 
| 20 |  | #include <log4cxx/helpers/outputstreamwriter.h> | 
| 21 |  | #include <log4cxx/helpers/charsetencoder.h> | 
| 22 |  | #include <log4cxx/helpers/optionconverter.h> | 
| 23 |  | #include <log4cxx/helpers/stringhelper.h> | 
| 24 |  | #include <log4cxx/xml/xmllayout.h> | 
| 25 |  | #include <log4cxx/level.h> | 
| 26 |  | #include <log4cxx/helpers/transform.h> | 
| 27 |  | #include <log4cxx/helpers/transcoder.h> | 
| 28 |  | #include <log4cxx/helpers/socketoutputstream.h> | 
| 29 |  | #include <log4cxx/private/appenderskeleton_priv.h> | 
| 30 |  | #include <log4cxx/private/socketappenderskeleton_priv.h> | 
| 31 |  |  | 
| 32 |  | using namespace LOG4CXX_NS; | 
| 33 |  | using namespace LOG4CXX_NS::helpers; | 
| 34 |  | using namespace LOG4CXX_NS::net; | 
| 35 |  | using namespace LOG4CXX_NS::xml; | 
| 36 |  |  | 
| 37 |  | struct XMLSocketAppender::XMLSocketAppenderPriv : public SocketAppenderSkeletonPriv | 
| 38 |  | { | 
| 39 |  |   XMLSocketAppenderPriv(int defaultPort, int reconnectionDelay) : | 
| 40 | 0 |     SocketAppenderSkeletonPriv(defaultPort, reconnectionDelay) {} | 
| 41 |  |  | 
| 42 |  |   XMLSocketAppenderPriv(InetAddressPtr address, int defaultPort, int reconnectionDelay) : | 
| 43 | 0 |     SocketAppenderSkeletonPriv( address, defaultPort, reconnectionDelay ) {} | 
| 44 |  |  | 
| 45 |  |   XMLSocketAppenderPriv(const LogString& host, int port, int delay) : | 
| 46 | 0 |     SocketAppenderSkeletonPriv( host, port, delay ) {} | 
| 47 |  |  | 
| 48 |  |   LOG4CXX_NS::helpers::WriterPtr writer; | 
| 49 |  | }; | 
| 50 |  |  | 
| 51 |  | IMPLEMENT_LOG4CXX_OBJECT(XMLSocketAppender) | 
| 52 |  |  | 
| 53 | 0 | #define _priv static_cast<XMLSocketAppenderPriv*>(m_priv.get()) | 
| 54 |  |  | 
| 55 |  | // The default port number of remote logging server (4560) | 
| 56 |  | int XMLSocketAppender::DEFAULT_PORT                 = 4560; | 
| 57 |  |  | 
| 58 |  | // The default reconnection delay (30000 milliseconds or 30 seconds). | 
| 59 |  | int XMLSocketAppender::DEFAULT_RECONNECTION_DELAY   = 30000; | 
| 60 |  |  | 
| 61 |  | const int XMLSocketAppender::MAX_EVENT_LEN          = 1024; | 
| 62 |  |  | 
| 63 |  | XMLSocketAppender::XMLSocketAppender() | 
| 64 | 0 |   : SocketAppenderSkeleton(std::make_unique<XMLSocketAppenderPriv>(DEFAULT_PORT, DEFAULT_RECONNECTION_DELAY)) | 
| 65 | 0 | { | 
| 66 | 0 |   _priv->layout = std::make_shared<XMLLayout>(); | 
| 67 | 0 | } Unexecuted instantiation: log4cxx::net::XMLSocketAppender::XMLSocketAppender()Unexecuted instantiation: log4cxx::net::XMLSocketAppender::XMLSocketAppender() | 
| 68 |  |  | 
| 69 |  | XMLSocketAppender::XMLSocketAppender(InetAddressPtr address1, int port1) | 
| 70 | 0 |   : SocketAppenderSkeleton(std::make_unique<XMLSocketAppenderPriv>(address1, port1, DEFAULT_RECONNECTION_DELAY)) | 
| 71 | 0 | { | 
| 72 | 0 |   _priv->layout = std::make_shared<XMLLayout>(); | 
| 73 | 0 |   Pool p; | 
| 74 | 0 |   activateOptions(p); | 
| 75 | 0 | } Unexecuted instantiation: log4cxx::net::XMLSocketAppender::XMLSocketAppender(std::__1::shared_ptr<log4cxx::helpers::InetAddress>, int)Unexecuted instantiation: log4cxx::net::XMLSocketAppender::XMLSocketAppender(std::__1::shared_ptr<log4cxx::helpers::InetAddress>, int) | 
| 76 |  |  | 
| 77 |  | XMLSocketAppender::XMLSocketAppender(const LogString& host, int port1) | 
| 78 | 0 |   : SocketAppenderSkeleton(std::make_unique<XMLSocketAppenderPriv>(host, port1, DEFAULT_RECONNECTION_DELAY)) | 
| 79 | 0 | { | 
| 80 | 0 |   _priv->layout = std::make_shared<XMLLayout>(); | 
| 81 | 0 |   Pool p; | 
| 82 | 0 |   activateOptions(p); | 
| 83 | 0 | } Unexecuted instantiation: log4cxx::net::XMLSocketAppender::XMLSocketAppender(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)Unexecuted instantiation: log4cxx::net::XMLSocketAppender::XMLSocketAppender(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int) | 
| 84 |  |  | 
| 85 |  | XMLSocketAppender::~XMLSocketAppender() | 
| 86 | 0 | { | 
| 87 | 0 |   finalize(); | 
| 88 | 0 | } | 
| 89 |  |  | 
| 90 |  |  | 
| 91 |  | int XMLSocketAppender::getDefaultDelay() const | 
| 92 | 0 | { | 
| 93 | 0 |   return DEFAULT_RECONNECTION_DELAY; | 
| 94 | 0 | } | 
| 95 |  |  | 
| 96 |  | int XMLSocketAppender::getDefaultPort() const | 
| 97 | 0 | { | 
| 98 | 0 |   return DEFAULT_PORT; | 
| 99 | 0 | } | 
| 100 |  |  | 
| 101 |  | void XMLSocketAppender::setSocket(LOG4CXX_NS::helpers::SocketPtr& socket, Pool& p) | 
| 102 | 0 | { | 
| 103 | 0 |   OutputStreamPtr os = std::make_shared<SocketOutputStream>(socket); | 
| 104 | 0 |   CharsetEncoderPtr charset(CharsetEncoder::getUTF8Encoder()); | 
| 105 | 0 |   std::lock_guard<std::recursive_mutex> lock(_priv->mutex); | 
| 106 | 0 |   _priv->writer = std::make_shared<OutputStreamWriter>(os, charset); | 
| 107 | 0 | } | 
| 108 |  |  | 
| 109 |  | void XMLSocketAppender::cleanUp(Pool& p) | 
| 110 | 0 | { | 
| 111 | 0 |   if (_priv->writer) | 
| 112 | 0 |   { | 
| 113 | 0 |     try | 
| 114 | 0 |     { | 
| 115 | 0 |       _priv->writer->close(p); | 
| 116 | 0 |       _priv->writer = nullptr; | 
| 117 | 0 |     } | 
| 118 | 0 |     catch (std::exception&) | 
| 119 | 0 |     { | 
| 120 | 0 |     } | 
| 121 | 0 |   } | 
| 122 | 0 | } | 
| 123 |  |  | 
| 124 |  | void XMLSocketAppender::append(const spi::LoggingEventPtr& event, LOG4CXX_NS::helpers::Pool& p) | 
| 125 | 0 | { | 
| 126 | 0 |   if (_priv->writer) | 
| 127 | 0 |   { | 
| 128 | 0 |     LogString output; | 
| 129 | 0 |     _priv->layout->format(output, event, p); | 
| 130 |  | 
 | 
| 131 | 0 |     try | 
| 132 | 0 |     { | 
| 133 | 0 |       _priv->writer->write(output, p); | 
| 134 | 0 |       _priv->writer->flush(p); | 
| 135 | 0 |     } | 
| 136 | 0 |     catch (std::exception& e) | 
| 137 | 0 |     { | 
| 138 | 0 |       _priv->writer = nullptr; | 
| 139 | 0 |       LogLog::warn(LOG4CXX_STR("Detected problem with connection: "), e); | 
| 140 |  | 
 | 
| 141 | 0 |       if (getReconnectionDelay() > 0) | 
| 142 | 0 |       { | 
| 143 | 0 |         fireConnector(); | 
| 144 | 0 |       } | 
| 145 | 0 |     } | 
| 146 | 0 |   } | 
| 147 | 0 | } | 
| 148 |  |  | 
| 149 |  |  | 
| 150 |  |  | 
| 151 |  |  |