Coverage Report

Created: 2026-04-12 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
  void close() override;
51
};
52
53
IMPLEMENT_LOG4CXX_OBJECT(XMLSocketAppender)
54
55
0
#define _priv static_cast<XMLSocketAppenderPriv*>(m_priv.get())
56
57
// The default port number of remote logging server (4560)
58
int XMLSocketAppender::DEFAULT_PORT                 = 4560;
59
60
// The default reconnection delay (30000 milliseconds or 30 seconds).
61
int XMLSocketAppender::DEFAULT_RECONNECTION_DELAY   = 30000;
62
63
const int XMLSocketAppender::MAX_EVENT_LEN          = 1024;
64
65
XMLSocketAppender::XMLSocketAppender()
66
0
  : SocketAppenderSkeleton(std::make_unique<XMLSocketAppenderPriv>(DEFAULT_PORT, DEFAULT_RECONNECTION_DELAY))
67
0
{
68
0
  _priv->layout = std::make_shared<XMLLayout>();
69
0
}
Unexecuted instantiation: log4cxx::net::XMLSocketAppender::XMLSocketAppender()
Unexecuted instantiation: log4cxx::net::XMLSocketAppender::XMLSocketAppender()
70
71
XMLSocketAppender::XMLSocketAppender(InetAddressPtr address1, int port1)
72
0
  : SocketAppenderSkeleton(std::make_unique<XMLSocketAppenderPriv>(address1, port1, DEFAULT_RECONNECTION_DELAY))
73
0
{
74
0
  _priv->layout = std::make_shared<XMLLayout>();
75
0
  Pool p;
76
0
  activateOptions(p);
77
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)
78
79
XMLSocketAppender::XMLSocketAppender(const LogString& host, int port1)
80
0
  : SocketAppenderSkeleton(std::make_unique<XMLSocketAppenderPriv>(host, port1, DEFAULT_RECONNECTION_DELAY))
81
0
{
82
0
  _priv->layout = std::make_shared<XMLLayout>();
83
0
  Pool p;
84
0
  activateOptions(p);
85
0
}
Unexecuted instantiation: log4cxx::net::XMLSocketAppender::XMLSocketAppender(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, int)
Unexecuted instantiation: log4cxx::net::XMLSocketAppender::XMLSocketAppender(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, int)
86
87
XMLSocketAppender::~XMLSocketAppender()
88
0
{
89
0
  if (_priv->setClosed())
90
0
    _priv->close();
91
0
}
92
93
94
int XMLSocketAppender::getDefaultDelay() const
95
0
{
96
0
  return DEFAULT_RECONNECTION_DELAY;
97
0
}
98
99
int XMLSocketAppender::getDefaultPort() const
100
0
{
101
0
  return DEFAULT_PORT;
102
0
}
103
104
void XMLSocketAppender::setSocket(LOG4CXX_NS::helpers::SocketPtr& socket, Pool& p)
105
0
{
106
0
  OutputStreamPtr os = std::make_shared<SocketOutputStream>(socket);
107
0
  CharsetEncoderPtr charset(CharsetEncoder::getUTF8Encoder());
108
0
  std::lock_guard<std::recursive_mutex> lock(_priv->mutex);
109
0
  _priv->writer = std::make_shared<OutputStreamWriter>(os, charset);
110
0
}
111
112
#if LOG4CXX_ABI_VERSION <= 15
113
void XMLSocketAppender::cleanUp(Pool& p)
114
0
{
115
0
  _priv->close();
116
0
}
117
#endif
118
119
void XMLSocketAppender::XMLSocketAppenderPriv::close()
120
0
{
121
0
  SocketAppenderSkeletonPriv::close();
122
0
  if (this->writer)
123
0
  {
124
0
    try
125
0
    {
126
0
      this->writer->close(this->pool);
127
0
      this->writer = nullptr;
128
0
    }
129
0
    catch (std::exception&)
130
0
    {
131
0
    }
132
0
  }
133
0
}
134
135
void XMLSocketAppender::append(const spi::LoggingEventPtr& event, LOG4CXX_NS::helpers::Pool& p)
136
0
{
137
0
  if (_priv->writer)
138
0
  {
139
0
    LogString output;
140
0
    _priv->layout->format(output, event, p);
141
142
0
    try
143
0
    {
144
0
      _priv->writer->write(output, p);
145
0
      _priv->writer->flush(p);
146
0
    }
147
0
    catch (std::exception& e)
148
0
    {
149
0
      _priv->writer = nullptr;
150
0
      LogLog::warn(LOG4CXX_STR("Detected problem with connection: "), e);
151
152
0
      if (getReconnectionDelay() > 0)
153
0
      {
154
0
        fireConnector();
155
0
      }
156
0
    }
157
0
  }
158
0
}
159
160
161
162