00001 // Module: LOG4CPLUS 00002 // File: socketappender.h 00003 // Created: 5/2003 00004 // Author: Tad E. Smith 00005 // 00006 // 00007 // Copyright 2003-2009 Tad E. Smith 00008 // 00009 // Licensed under the Apache License, Version 2.0 (the "License"); 00010 // you may not use this file except in compliance with the License. 00011 // You may obtain a copy of the License at 00012 // 00013 // http://www.apache.org/licenses/LICENSE-2.0 00014 // 00015 // Unless required by applicable law or agreed to in writing, software 00016 // distributed under the License is distributed on an "AS IS" BASIS, 00017 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00018 // See the License for the specific language governing permissions and 00019 // limitations under the License. 00020 00023 #ifndef _LOG4CPLUS_SOCKET_APPENDER_HEADER_ 00024 #define _LOG4CPLUS_SOCKET_APPENDER_HEADER_ 00025 00026 #include "dcmtk/oflog/config.h" 00027 #include "dcmtk/oflog/appender.h" 00028 #include "dcmtk/oflog/helpers/socket.h" 00029 #include "dcmtk/oflog/helpers/syncprims.h" 00030 00031 00032 #define LOG4CPLUS_MAX_MESSAGE_SIZE (8*1024) 00033 00034 00035 namespace log4cplus { 00036 00088 class LOG4CPLUS_EXPORT SocketAppender : public Appender { 00089 public: 00090 // Ctors 00091 SocketAppender(const log4cplus::tstring& host, int port, 00092 const log4cplus::tstring& serverName = tstring()); 00093 SocketAppender(const log4cplus::helpers::Properties & properties, log4cplus::tstring& error); 00094 00095 // Dtor 00096 ~SocketAppender(); 00097 00098 // Methods 00099 virtual void close(); 00100 00101 protected: 00102 void openSocket(); 00103 void initConnector (); 00104 virtual void append(const spi::InternalLoggingEvent& event); 00105 00106 // Data 00107 log4cplus::helpers::Socket socket; 00108 log4cplus::tstring host; 00109 int port; 00110 log4cplus::tstring serverName; 00111 00112 #if ! defined (LOG4CPLUS_SINGLE_THREADED) 00113 class LOG4CPLUS_EXPORT ConnectorThread; 00114 friend class ConnectorThread; 00115 00116 class LOG4CPLUS_EXPORT ConnectorThread 00117 : public thread::AbstractThread 00118 , public helpers::LogLogUser 00119 { 00120 public: 00121 ConnectorThread (SocketAppender &); 00122 virtual ~ConnectorThread (); 00123 00124 virtual void run(); 00125 00126 void terminate (); 00127 void trigger (); 00128 00129 protected: 00130 SocketAppender & sa; 00131 thread::ManualResetEvent trigger_ev; 00132 bool exit_flag; 00133 }; 00134 00135 volatile bool connected; 00136 helpers::SharedObjectPtr<ConnectorThread> connector; 00137 #endif 00138 00139 private: 00140 // Disallow copying of instances of this class 00141 SocketAppender(const SocketAppender&); 00142 SocketAppender& operator=(const SocketAppender&); 00143 }; 00144 00145 namespace helpers { 00146 LOG4CPLUS_EXPORT 00147 SocketBuffer convertToBuffer(const log4cplus::spi::InternalLoggingEvent& event, 00148 const log4cplus::tstring& serverName); 00149 00150 LOG4CPLUS_EXPORT 00151 log4cplus::spi::InternalLoggingEvent readFromBuffer(SocketBuffer& buffer); 00152 } // end namespace helpers 00153 00154 } // end namespace log4cplus 00155 00156 #endif // _LOG4CPLUS_SOCKET_APPENDER_HEADER_ 00157