You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.0 KiB

  1. // Module: LOG4CPLUS
  2. // File: loggingserver.cxx
  3. // Created: 5/2003
  4. // Author: Tad E. Smith
  5. //
  6. //
  7. // Copyright 2003-2013 Tad E. Smith
  8. //
  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
  12. //
  13. // http://www.apache.org/licenses/LICENSE-2.0
  14. //
  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.
  20. #include <cstdlib>
  21. #include <iostream>
  22. #include <log4cplus/configurator.h>
  23. #include <log4cplus/socketappender.h>
  24. #include <log4cplus/helpers/socket.h>
  25. #include <log4cplus/thread/threads.h>
  26. #include <log4cplus/spi/loggingevent.h>
  27. namespace loggingserver
  28. {
  29. class ClientThread : public log4cplus::thread::AbstractThread
  30. {
  31. public:
  32. ClientThread(log4cplus::helpers::Socket clientsock)
  33. : clientsock(clientsock)
  34. {
  35. std::cout << "Received a client connection!!!!" << std::endl;
  36. }
  37. ~ClientThread()
  38. {
  39. std::cout << "Client connection closed." << std::endl;
  40. }
  41. virtual void run();
  42. private:
  43. log4cplus::helpers::Socket clientsock;
  44. };
  45. }
  46. int
  47. main(int argc, char** argv)
  48. {
  49. if(argc < 3) {
  50. std::cout << "Usage: port config_file" << std::endl;
  51. return 1;
  52. }
  53. int port = std::atoi(argv[1]);
  54. const log4cplus::tstring configFile = LOG4CPLUS_C_STR_TO_TSTRING(argv[2]);
  55. log4cplus::PropertyConfigurator config(configFile);
  56. config.configure();
  57. log4cplus::helpers::ServerSocket serverSocket(port);
  58. if (!serverSocket.isOpen()) {
  59. std::cout << "Could not open server socket, maybe port "
  60. << port << " is already in use." << std::endl;
  61. return 2;
  62. }
  63. while(1) {
  64. loggingserver::ClientThread *thr =
  65. new loggingserver::ClientThread(serverSocket.accept());
  66. thr->start();
  67. }
  68. return 0;
  69. }
  70. ////////////////////////////////////////////////////////////////////////////////
  71. // loggingserver::ClientThread implementation
  72. ////////////////////////////////////////////////////////////////////////////////
  73. void
  74. loggingserver::ClientThread::run()
  75. {
  76. while(1) {
  77. if(!clientsock.isOpen()) {
  78. return;
  79. }
  80. log4cplus::helpers::SocketBuffer msgSizeBuffer(sizeof(unsigned int));
  81. if(!clientsock.read(msgSizeBuffer)) {
  82. return;
  83. }
  84. unsigned int msgSize = msgSizeBuffer.readInt();
  85. log4cplus::helpers::SocketBuffer buffer(msgSize);
  86. if(!clientsock.read(buffer)) {
  87. return;
  88. }
  89. log4cplus::spi::InternalLoggingEvent event
  90. = log4cplus::helpers::readFromBuffer(buffer);
  91. log4cplus::Logger logger
  92. = log4cplus::Logger::getInstance(event.getLoggerName());
  93. logger.callAppenders(event);
  94. }
  95. }