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_TELNET_APPENDER_H
|
---|
19 | #define _LOG4CXX_NET_TELNET_APPENDER_H
|
---|
20 |
|
---|
21 | #if defined(_MSC_VER)
|
---|
22 | #pragma warning ( push )
|
---|
23 | #pragma warning ( disable: 4231 4251 4275 4786 )
|
---|
24 | #endif
|
---|
25 |
|
---|
26 |
|
---|
27 |
|
---|
28 | #include <log4cxx/appenderskeleton.h>
|
---|
29 | #include <log4cxx/helpers/socket.h>
|
---|
30 | #include <log4cxx/helpers/serversocket.h>
|
---|
31 | #include <log4cxx/helpers/thread.h>
|
---|
32 | #include <vector>
|
---|
33 | #include <log4cxx/helpers/charsetencoder.h>
|
---|
34 |
|
---|
35 | namespace log4cxx
|
---|
36 | {
|
---|
37 | namespace helpers {
|
---|
38 | class ByteBuffer;
|
---|
39 | }
|
---|
40 | namespace net
|
---|
41 | {
|
---|
42 | LOG4CXX_LIST_DEF(ConnectionList, log4cxx::helpers::SocketPtr);
|
---|
43 |
|
---|
44 | /**
|
---|
45 | <p>The TelnetAppender is a log4cxx appender that specializes in
|
---|
46 | writing to a read-only socket. The output is provided in a
|
---|
47 | telnet-friendly way so that a log can be monitored over TCP/IP.
|
---|
48 | Clients using telnet connect to the socket and receive log data.
|
---|
49 | This is handy for remote monitoring, especially when monitoring a
|
---|
50 | servlet.
|
---|
51 |
|
---|
52 | <p>Here is a list of the available configuration options:
|
---|
53 |
|
---|
54 | <table border=1>
|
---|
55 | <tr>
|
---|
56 | <td align=center><b>Name</b></td>
|
---|
57 | <td align=center><b>Requirement</b></td>
|
---|
58 | <td align=center><b>Description</b></td>
|
---|
59 | <td align=center><b>Sample Value</b></td>
|
---|
60 | </tr>
|
---|
61 |
|
---|
62 | <tr>
|
---|
63 | <td>Port</td>
|
---|
64 | <td>optional</td>
|
---|
65 | <td>This parameter determines the port to use for announcing log events. The default port is 23 (telnet).</td>
|
---|
66 | <td>5875</td>
|
---|
67 | </table>
|
---|
68 | */
|
---|
69 | class LOG4CXX_EXPORT TelnetAppender : public AppenderSkeleton
|
---|
70 | {
|
---|
71 | class SocketHandler;
|
---|
72 | friend class SocketHandler;
|
---|
73 | private:
|
---|
74 | static const int DEFAULT_PORT;
|
---|
75 | static const int MAX_CONNECTIONS;
|
---|
76 | int port;
|
---|
77 |
|
---|
78 | public:
|
---|
79 | DECLARE_LOG4CXX_OBJECT(TelnetAppender)
|
---|
80 | BEGIN_LOG4CXX_CAST_MAP()
|
---|
81 | LOG4CXX_CAST_ENTRY(TelnetAppender)
|
---|
82 | LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton)
|
---|
83 | END_LOG4CXX_CAST_MAP()
|
---|
84 |
|
---|
85 | TelnetAppender();
|
---|
86 | ~TelnetAppender();
|
---|
87 |
|
---|
88 | /**
|
---|
89 | This appender requires a layout to format the text to the
|
---|
90 | attached client(s). */
|
---|
91 | virtual bool requiresLayout() const
|
---|
92 | { return true; }
|
---|
93 |
|
---|
94 | LogString getEncoding() const;
|
---|
95 | void setEncoding(const LogString& value);
|
---|
96 |
|
---|
97 |
|
---|
98 | /** all of the options have been set, create the socket handler and
|
---|
99 | wait for connections. */
|
---|
100 | void activateOptions(log4cxx::helpers::Pool& p);
|
---|
101 |
|
---|
102 | /**
|
---|
103 | Set options
|
---|
104 | */
|
---|
105 | virtual void setOption(const LogString& option, const LogString& value);
|
---|
106 |
|
---|
107 | /**
|
---|
108 | Returns value of the <b>Port</b> option.
|
---|
109 | */
|
---|
110 | int getPort() const
|
---|
111 | { return port; }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | The <b>Port</b> option takes a positive integer representing
|
---|
115 | the port where the server is waiting for connections.
|
---|
116 | */
|
---|
117 | void setPort(int port1)
|
---|
118 | { this->port = port1; }
|
---|
119 |
|
---|
120 |
|
---|
121 | /** shuts down the appender. */
|
---|
122 | void close();
|
---|
123 |
|
---|
124 | protected:
|
---|
125 | /** Handles a log event. For this appender, that means writing the
|
---|
126 | message to each connected client. */
|
---|
127 | virtual void append(const spi::LoggingEventPtr& event, log4cxx::helpers::Pool& p) ;
|
---|
128 |
|
---|
129 | //---------------------------------------------------------- SocketHandler:
|
---|
130 |
|
---|
131 | private:
|
---|
132 | // prevent copy and assignment statements
|
---|
133 | TelnetAppender(const TelnetAppender&);
|
---|
134 | TelnetAppender& operator=(const TelnetAppender&);
|
---|
135 |
|
---|
136 | typedef log4cxx::helpers::SocketPtr Connection;
|
---|
137 |
|
---|
138 | void write(log4cxx::helpers::ByteBuffer&);
|
---|
139 | void writeStatus(const log4cxx::helpers::SocketPtr& socket, const LogString& msg, log4cxx::helpers::Pool& p);
|
---|
140 | ConnectionList connections;
|
---|
141 | LogString encoding;
|
---|
142 | log4cxx::helpers::CharsetEncoderPtr encoder;
|
---|
143 | helpers::ServerSocket* serverSocket;
|
---|
144 | helpers::Thread sh;
|
---|
145 | size_t activeConnections;
|
---|
146 | static void* LOG4CXX_THREAD_FUNC acceptConnections(apr_thread_t* thread, void* data);
|
---|
147 | }; // class TelnetAppender
|
---|
148 |
|
---|
149 | LOG4CXX_PTR_DEF(TelnetAppender);
|
---|
150 | } // namespace net
|
---|
151 | } // namespace log4cxx
|
---|
152 |
|
---|
153 |
|
---|
154 | #if defined(_MSC_VER)
|
---|
155 | #pragma warning ( pop )
|
---|
156 | #endif
|
---|
157 |
|
---|
158 | #endif // _LOG4CXX_NET_TELNET_APPENDER_H
|
---|
159 |
|
---|