2 // File: loggingserver.cxx
4 // Author: Tad E. Smith
7 // Copyright 2003-2010 Tad E. Smith
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
13 // http://www.apache.org/licenses/LICENSE-2.0
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
23 #include <log4cplus/config.hxx>
24 #include <log4cplus/configurator.h>
25 #include <log4cplus/consoleappender.h>
26 #include <log4cplus/socketappender.h>
27 #include <log4cplus/helpers/loglog.h>
28 #include <log4cplus/helpers/socket.h>
29 #include <log4cplus/helpers/threads.h>
30 #include <log4cplus/spi/loggerimpl.h>
31 #include <log4cplus/spi/loggingevent.h>
35 using namespace log4cplus
;
36 using namespace log4cplus::helpers
;
37 using namespace log4cplus::thread
;
40 namespace loggingserver
{
41 class ClientThread
: public AbstractThread
{
43 ClientThread(Socket clientsock_
)
44 : clientsock(clientsock_
)
46 cout
<< "Received a client connection!!!!" << endl
;
51 cout
<< "Client connection closed." << endl
;
66 main(int argc
, char** argv
)
69 cout
<< "Usage: port config_file" << endl
;
72 int port
= std::atoi(argv
[1]);
73 tstring configFile
= LOG4CPLUS_C_STR_TO_TSTRING(argv
[2]);
75 PropertyConfigurator
config(configFile
);
78 ServerSocket
serverSocket(port
);
79 if (!serverSocket
.isOpen()) {
80 cout
<< "Could not open server socket, maybe port "
81 << port
<< " is already in use." << endl
;
86 loggingserver::ClientThread
*thr
=
87 new loggingserver::ClientThread(serverSocket
.accept());
95 ////////////////////////////////////////////////////////////////////////////////
96 // loggingserver::ClientThread implementation
97 ////////////////////////////////////////////////////////////////////////////////
101 loggingserver::ClientThread::run()
104 if(!clientsock
.isOpen()) {
107 SocketBuffer
msgSizeBuffer(sizeof(unsigned int));
108 if(!clientsock
.read(msgSizeBuffer
)) {
112 unsigned int msgSize
= msgSizeBuffer
.readInt();
114 SocketBuffer
buffer(msgSize
);
115 if(!clientsock
.read(buffer
)) {
119 spi::InternalLoggingEvent event
= readFromBuffer(buffer
);
120 Logger logger
= Logger::getInstance(event
.getLoggerName());
121 logger
.callAppenders(event
);