log4cplus
1.1.0
|
00001 // -*- C++ -*- 00002 // Module: LOG4CPLUS 00003 // File: socketappender.h 00004 // Created: 5/2003 00005 // Author: Tad E. Smith 00006 // 00007 // 00008 // Copyright 2003-2010 Tad E. Smith 00009 // 00010 // Licensed under the Apache License, Version 2.0 (the "License"); 00011 // you may not use this file except in compliance with the License. 00012 // You may obtain a copy of the License at 00013 // 00014 // http://www.apache.org/licenses/LICENSE-2.0 00015 // 00016 // Unless required by applicable law or agreed to in writing, software 00017 // distributed under the License is distributed on an "AS IS" BASIS, 00018 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 // See the License for the specific language governing permissions and 00020 // limitations under the License. 00021 00024 #ifndef LOG4CPLUS_SOCKET_APPENDER_HEADER_ 00025 #define LOG4CPLUS_SOCKET_APPENDER_HEADER_ 00026 00027 #include <log4cplus/config.hxx> 00028 00029 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE) 00030 #pragma once 00031 #endif 00032 00033 #include <log4cplus/appender.h> 00034 #include <log4cplus/helpers/socket.h> 00035 #include <log4cplus/thread/syncprims.h> 00036 #include <log4cplus/thread/threads.h> 00037 00038 00039 namespace log4cplus 00040 { 00041 00042 00043 #ifndef UNICODE 00044 std::size_t const LOG4CPLUS_MAX_MESSAGE_SIZE = 8*1024; 00045 #else 00046 std::size_t const LOG4CPLUS_MAX_MESSAGE_SIZE = 2*8*1024; 00047 #endif 00048 00049 00101 class LOG4CPLUS_EXPORT SocketAppender : public Appender { 00102 public: 00103 // Ctors 00104 SocketAppender(const log4cplus::tstring& host, unsigned short port, 00105 const log4cplus::tstring& serverName = tstring()); 00106 SocketAppender(const log4cplus::helpers::Properties & properties); 00107 00108 // Dtor 00109 ~SocketAppender(); 00110 00111 // Methods 00112 virtual void close(); 00113 00114 protected: 00115 void openSocket(); 00116 void initConnector (); 00117 virtual void append(const spi::InternalLoggingEvent& event); 00118 00119 // Data 00120 log4cplus::helpers::Socket socket; 00121 log4cplus::tstring host; 00122 unsigned int port; 00123 log4cplus::tstring serverName; 00124 00125 #if ! defined (LOG4CPLUS_SINGLE_THREADED) 00126 class LOG4CPLUS_EXPORT ConnectorThread; 00127 friend class ConnectorThread; 00128 00129 class LOG4CPLUS_EXPORT ConnectorThread 00130 : public thread::AbstractThread 00131 { 00132 public: 00133 ConnectorThread (SocketAppender &); 00134 virtual ~ConnectorThread (); 00135 00136 virtual void run(); 00137 00138 void terminate (); 00139 void trigger (); 00140 00141 protected: 00142 SocketAppender & sa; 00143 thread::ManualResetEvent trigger_ev; 00144 bool exit_flag; 00145 }; 00146 00147 volatile bool connected; 00148 helpers::SharedObjectPtr<ConnectorThread> connector; 00149 #endif 00150 00151 private: 00152 // Disallow copying of instances of this class 00153 SocketAppender(const SocketAppender&); 00154 SocketAppender& operator=(const SocketAppender&); 00155 }; 00156 00157 namespace helpers { 00158 LOG4CPLUS_EXPORT 00159 void convertToBuffer (SocketBuffer & buffer, 00160 const log4cplus::spi::InternalLoggingEvent& event, 00161 const log4cplus::tstring& serverName); 00162 00163 LOG4CPLUS_EXPORT 00164 log4cplus::spi::InternalLoggingEvent readFromBuffer(SocketBuffer& buffer); 00165 } // end namespace helpers 00166 00167 } // end namespace log4cplus 00168 00169 #endif // LOG4CPLUS_SOCKET_APPENDER_HEADER_ 00170