log4cplus  2.0.0
syslogappender.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // Module:  Log4CPLUS
00003 // File:    syslogappender.h
00004 // Created: 6/2001
00005 // Author:  Tad E. Smith
00006 //
00007 //
00008 // Copyright 2001-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_SYSLOG_APPENDER_HEADER_
00025 #define LOG4CPLUS_SYSLOG_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/helpers/connectorthread.h>
00036 
00037 
00038 namespace log4cplus
00039 {
00040 
00080     class LOG4CPLUS_EXPORT SysLogAppender
00081       : public Appender
00082 #if ! defined (LOG4CPLUS_SINGLE_THREADED)
00083       , protected virtual helpers::IConnectorThreadClient
00084 #endif
00085     {
00086     public:
00088         enum RemoteSyslogType
00089         {
00090             RSTUdp,
00091             RSTTcp
00092         };
00093 
00094       // Ctors
00095 #if defined (LOG4CPLUS_HAVE_SYSLOG_H)
00096         SysLogAppender(const tstring& ident);
00097 #endif
00098         SysLogAppender(const tstring& ident, const tstring & host,
00099             int port = 514, const tstring & facility = tstring (),
00100             RemoteSyslogType remoteSyslogType = RSTUdp, bool ipv6 = false);
00101         SysLogAppender(const log4cplus::helpers::Properties & properties);
00102 
00103       // Dtor
00104         virtual ~SysLogAppender();
00105 
00106       // Methods
00107         virtual void close();
00108 
00109     protected:
00110         virtual int getSysLogLevel(const LogLevel& ll) const;
00111         virtual void append(const spi::InternalLoggingEvent& event);
00112 #if defined (LOG4CPLUS_HAVE_SYSLOG_H)
00113 
00114         void appendLocal(const spi::InternalLoggingEvent& event);
00115 #endif
00116 
00117         void appendRemote(const spi::InternalLoggingEvent& event);
00118 
00119       // Data
00120         tstring ident;
00121         int facility;
00122 
00123         typedef void (SysLogAppender:: * AppendFuncType) (
00124             const spi::InternalLoggingEvent&);
00125         AppendFuncType appendFunc;
00126 
00127         tstring host;
00128         int port;
00129         RemoteSyslogType remoteSyslogType;
00130         helpers::Socket syslogSocket;
00131         bool connected;
00132         bool ipv6 = false;
00133 
00134         static tstring const remoteTimeFormat;
00135 
00136         void initConnector ();
00137         void openSocket ();
00138 
00139 #if ! defined (LOG4CPLUS_SINGLE_THREADED)
00140         virtual thread::Mutex const & ctcGetAccessMutex () const;
00141         virtual helpers::Socket & ctcGetSocket ();
00142         virtual helpers::Socket ctcConnect ();
00143         virtual void ctcSetConnected ();
00144 
00145         helpers::SharedObjectPtr<helpers::ConnectorThread> connector;
00146 #endif
00147 
00148     private:
00149       // Disallow copying of instances of this class
00150         SysLogAppender(const SysLogAppender&);
00151         SysLogAppender& operator=(const SysLogAppender&);
00152 
00153         std::string identStr;
00154         tstring hostname;
00155     };
00156 
00157 } // end namespace log4cplus
00158 
00159 
00160 #endif // LOG4CPLUS_SYSLOG_APPENDER_HEADER_