Coverage Report

Created: 2025-07-12 06:12

/src/logging-log4cxx/src/main/include/log4cxx/net/socketappenderskeleton.h
Line
Count
Source (jump to first uncovered line)
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/socket.h>
23
#include <thread>
24
#include <condition_variable>
25
26
namespace LOG4CXX_NS
27
{
28
29
namespace net
30
{
31
32
/**
33
 *  Abstract base class for SocketAppender and XMLSocketAppender
34
 */
35
class LOG4CXX_EXPORT SocketAppenderSkeleton : public AppenderSkeleton
36
{
37
  protected:
38
    struct SocketAppenderSkeletonPriv;
39
40
  public:
41
    SocketAppenderSkeleton(int defaultPort, int reconnectionDelay);
42
    ~SocketAppenderSkeleton();
43
44
    /**
45
    Connects to remote server at <code>address</code> and <code>port</code>.
46
    */
47
    SocketAppenderSkeleton(helpers::InetAddressPtr address, int port, int reconnectionDelay);
48
49
    /**
50
    Connects to remote server at <code>host</code> and <code>port</code>.
51
    */
52
    SocketAppenderSkeleton(const LogString& host, int port, int reconnectionDelay);
53
54
    /**
55
    \copybrief AppenderSkeleton::activateOptions()
56
57
    Connects to the specified <b>RemoteHost</b> and <b>Port</b>.
58
    */
59
    void activateOptions(helpers::Pool& p) override;
60
61
    void close() override;
62
63
64
    /**
65
    * This appender does not use a layout. Hence, this method
66
    * returns <code>false</code>.
67
    *
68
         */
69
    bool requiresLayout() const override
70
0
    {
71
0
      return false;
72
0
    }
73
74
    /**
75
    * The <b>RemoteHost</b> option takes a string value which should be
76
    * the host name of the server where a
77
    * Apache Chainsaw or compatible is running.
78
    * */
79
    void setRemoteHost(const LogString& host);
80
81
    /**
82
    Returns value of the <b>RemoteHost</b> option.
83
    */
84
    const LogString& getRemoteHost() const;
85
86
    /**
87
    The <b>Port</b> option takes a positive integer representing
88
    the port where the server is waiting for connections.
89
    */
90
    void setPort(int port1);
91
92
    /**
93
    Returns value of the <b>Port</b> option.
94
    */
95
    int getPort() const;
96
97
    /**
98
    The <b>LocationInfo</b> option takes a boolean value. If true,
99
    the information sent to the remote host will include location
100
    information. By default no location information is sent to the server.
101
    */
102
    void setLocationInfo(bool locationInfo1);
103
104
    /**
105
    Returns value of the <b>LocationInfo</b> option.
106
    */
107
    bool getLocationInfo() const;
108
109
    /**
110
    The <b>ReconnectionDelay</b> option takes a positive integer
111
    representing the number of milliseconds to wait between each
112
    failed connection attempt to the server. The default value of
113
    this option is 30000 which corresponds to 30 seconds.
114
115
    <p>Setting this option to zero turns off reconnection
116
    capability.
117
    */
118
    void setReconnectionDelay(int reconnectionDelay1);
119
120
    /**
121
    Returns value of the <b>ReconnectionDelay</b> option.
122
    */
123
    int getReconnectionDelay() const;
124
125
    void fireConnector();
126
127
    /**
128
    \copybrief AppenderSkeleton::setOption()
129
130
    Supported options | Supported values | Default value
131
    -------------- | ---------------- | ---------------
132
    RemoteHost |  (\ref inetAddress "1") | -
133
    Port | {int} | (\ref defaultPort "2")
134
    LocationInfo | True,False | False
135
136
    \anchor inetAddress (1) A valid internet address.
137
138
    \anchor defaultPort (2) Provided by the derived class.
139
140
    \sa AppenderSkeleton::setOption()
141
    */
142
    void setOption(const LogString& option, const LogString& value) override;
143
144
  protected:
145
    SocketAppenderSkeleton(std::unique_ptr<SocketAppenderSkeletonPriv> priv);
146
147
    virtual void setSocket(LOG4CXX_NS::helpers::SocketPtr& socket, LOG4CXX_NS::helpers::Pool& p) = 0;
148
149
    virtual void cleanUp(LOG4CXX_NS::helpers::Pool& p) = 0;
150
151
    virtual int getDefaultDelay() const = 0;
152
153
    virtual int getDefaultPort() const = 0;
154
155
  private:
156
    void connect(LOG4CXX_NS::helpers::Pool& p);
157
    /**
158
         The Connector will reconnect when the server becomes available
159
         again.  It does this by attempting to open a new connection every
160
         <code>reconnectionDelay</code> milliseconds.
161
162
         <p>It stops trying whenever a connection is established. It will
163
         restart to try reconnect to the server when previously open
164
         connection is droppped.
165
         */
166
167
    void retryConnect();
168
    bool is_closed();
169
    SocketAppenderSkeleton(const SocketAppenderSkeleton&);
170
    SocketAppenderSkeleton& operator=(const SocketAppenderSkeleton&);
171
172
}; // class SocketAppenderSkeleton
173
} // namespace net
174
} // namespace log4cxx
175
176
#endif // _LOG4CXX_NET_SOCKET_APPENDER_SKELETON_H
177