log4cplus
2.0.0
|
00001 // -*- C++ -*- 00002 // Module: LOG4CPLUS 00003 // File: socketappender.h 00004 // Created: 5/2003 00005 // Author: Tad E. Smith 00006 // 00007 // 00008 // Copyright 2003-2015 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 #include <log4cplus/helpers/connectorthread.h> 00038 00039 00040 namespace log4cplus 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 00105 class LOG4CPLUS_EXPORT SocketAppender 00106 : public Appender 00107 #if ! defined (LOG4CPLUS_SINGLE_THREADED) 00108 , protected virtual helpers::IConnectorThreadClient 00109 #endif 00110 { 00111 public: 00112 // Ctors 00113 SocketAppender(const log4cplus::tstring& host, unsigned short port, 00114 const log4cplus::tstring& serverName = tstring(), 00115 bool ipv6 = false); 00116 SocketAppender(const log4cplus::helpers::Properties & properties); 00117 00118 // Dtor 00119 ~SocketAppender(); 00120 00121 // Methods 00122 virtual void close(); 00123 00124 protected: 00125 void openSocket(); 00126 void initConnector (); 00127 virtual void append(const spi::InternalLoggingEvent& event); 00128 00129 // Data 00130 log4cplus::helpers::Socket socket; 00131 log4cplus::tstring host; 00132 unsigned int port; 00133 log4cplus::tstring serverName; 00134 bool ipv6 = false; 00135 00136 #if ! defined (LOG4CPLUS_SINGLE_THREADED) 00137 virtual thread::Mutex const & ctcGetAccessMutex () const; 00138 virtual helpers::Socket & ctcGetSocket (); 00139 virtual helpers::Socket ctcConnect (); 00140 virtual void ctcSetConnected (); 00141 00142 volatile bool connected; 00143 helpers::SharedObjectPtr<helpers::ConnectorThread> connector; 00144 #endif 00145 00146 private: 00147 // Disallow copying of instances of this class 00148 SocketAppender(const SocketAppender&); 00149 SocketAppender& operator=(const SocketAppender&); 00150 }; 00151 00152 namespace helpers { 00153 LOG4CPLUS_EXPORT 00154 void convertToBuffer (SocketBuffer & buffer, 00155 const log4cplus::spi::InternalLoggingEvent& event, 00156 const log4cplus::tstring& serverName); 00157 00158 LOG4CPLUS_EXPORT 00159 log4cplus::spi::InternalLoggingEvent readFromBuffer(SocketBuffer& buffer); 00160 } // end namespace helpers 00161 00162 } // end namespace log4cplus 00163 00164 #endif // LOG4CPLUS_SOCKET_APPENDER_HEADER_