mcatan      2004/04/03 08:41:15

  Modified:    include/log4cxx/net syslogappender.h
               src      syslogappender.cpp
  Log:
  On the local host, we directly use the system function 'syslog' if it is 
available.
  
  Revision  Changes    Path
  1.8       +8 -53     logging-log4cxx/include/log4cxx/net/syslogappender.h
  
  Index: syslogappender.h
  ===================================================================
  RCS file: /home/cvs/logging-log4cxx/include/log4cxx/net/syslogappender.h,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- syslogappender.h  7 Mar 2004 06:12:48 -0000       1.7
  +++ syslogappender.h  3 Apr 2004 16:41:15 -0000       1.8
  @@ -20,6 +20,9 @@
   #include <log4cxx/appenderskeleton.h>
   #include <log4cxx/helpers/syslogwriter.h>
   
  +#ifndef HAVE_SYSLOG
  +#endif
  +
   namespace log4cxx
   {
        namespace net
  @@ -37,59 +40,12 @@
                                LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton)
                        END_LOG4CXX_CAST_MAP()
   
  -                     typedef enum
  -                     {
  -                             /** Kernel messages */
  -                             LOG_KERN     = 0,
  -                             /** Random user-level messages */
  -                             LOG_USER     = 1<<3,
  -                             /** Mail system */
  -                             LOG_MAIL     = 2<<3,
  -                             /** System daemons */
  -                             LOG_DAEMON   = 3<<3,
  -                             /** security/authorization messages */
  -                             LOG_AUTH     = 4<<3,
  -                             /** messages generated internally by syslogd */
  -                             LOG_SYSLOG   = 5<<3,
  -
  -                             /** line printer subsystem */
  -                             LOG_LPR      = 6<<3,
  -                             /** network news subsystem */
  -                             LOG_NEWS     = 7<<3,
  -                             /** UUCP subsystem */
  -                             LOG_UUCP     = 8<<3,
  -                             /** clock daemon */
  -                             LOG_CRON     = 9<<3,
  -                             /** security/authorization  messages (private) 
*/
  -                             LOG_AUTHPRIV = 10<<3,
  -                             /** ftp daemon */
  -                             LOG_FTP      = 11<<3,
  -
  -                             // other codes through 15 reserved for system 
use
  -                             /** reserved for local use */
  -                             LOG_LOCAL0 = 16<<3,
  -                             /** reserved for local use */
  -                             LOG_LOCAL1 = 17<<3,
  -                             /** reserved for local use */
  -                             LOG_LOCAL2 = 18<<3,
  -                             /** reserved for local use */
  -                             LOG_LOCAL3 = 19<<3,
  -                             /** reserved for local use */
  -                             LOG_LOCAL4 = 20<<3,
  -                             /** reserved for local use */
  -                             LOG_LOCAL5 = 21<<3,
  -                             /** reserved for local use */
  -                             LOG_LOCAL6 = 22<<3,
  -                             /** reserved for local use*/
  -                             LOG_LOCAL7 = 23<<3,
   
  -                             LOG_UNDEF = -1
  -                     } SyslogFacility;
   
                        SyslogAppender();
  -                     SyslogAppender(const LayoutPtr& layout, SyslogFacility 
syslogFacility);
  +                     SyslogAppender(const LayoutPtr& layout, int 
syslogFacility);
                        SyslogAppender(const LayoutPtr& layout,
  -                             const String& syslogHost, SyslogFacility 
syslogFacility);
  +                             const String& syslogHost, int syslogFacility);
                        ~SyslogAppender();
                        /** Release any resources held by this SyslogAppender.*/
                        void close();
  @@ -98,7 +54,7 @@
                        Returns the specified syslog facility as a lower-case 
String,
                        e.g. "kern", "user", etc.
                        */
  -                     static String getFacilityString(SyslogFacility 
syslogFacility);
  +                     static String getFacilityString(int syslogFacility);
   
                        /**
                        Returns the integer value corresponding to the named 
syslog
  @@ -108,7 +64,7 @@
                        LOCAL1, LOCAL2, LOCAL3, LOCAL4, LOCAL5, LOCAL6, LOCAL7.
                        The matching is case-insensitive.
                        */
  -                     static SyslogFacility getFacility(const String 
&facilityName);
  +                     static int getFacility(const String &facilityName);
   
                        void append(const spi::LoggingEventPtr& event);
   
  @@ -173,12 +129,11 @@
                protected:
                        void initSyslogFacilityStr();
   
  -                     SyslogFacility syslogFacility; // Have LOG_USER as 
default
  +                     int syslogFacility; // Have LOG_USER as default
                        String facilityStr;
                        bool facilityPrinting;
                        helpers::SyslogWriter * sw;
                        String syslogHost;
  -
                }; // class SyslogAppender
       } // namespace net
   }; // namespace log4cxx
  
  
  
  1.6       +68 -7     logging-log4cxx/src/syslogappender.cpp
  
  Index: syslogappender.cpp
  ===================================================================
  RCS file: /home/cvs/logging-log4cxx/src/syslogappender.cpp,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- syslogappender.cpp        7 Mar 2004 06:12:48 -0000       1.5
  +++ syslogappender.cpp        3 Apr 2004 16:41:15 -0000       1.6
  @@ -11,7 +11,7 @@
    *                                                                         *
    * This software is published under the terms of the Apache Software       *
    * License version 1.1, a copy of which has been included with this        *
  - * distribution in the LICENSE.txt file.                                   *
  + * distribution in the license.apl file.                                   *
    ***************************************************************************/
   
   #include <log4cxx/net/syslogappender.h>
  @@ -21,6 +21,36 @@
   #include <log4cxx/spi/loggingevent.h>
   #include <log4cxx/level.h>
   
  +#ifdef HAVE_SYSLOG
  +     #include <syslog.h>     
  +#else
  +     /* facility codes */
  +     #define LOG_KERN        (0<<3)  /* kernel messages */
  +     #define LOG_USER        (1<<3)  /* random user-level messages */
  +     #define LOG_MAIL        (2<<3)  /* mail system */
  +     #define LOG_DAEMON      (3<<3)  /* system daemons */
  +     #define LOG_AUTH        (4<<3)  /* security/authorization messages */
  +     #define LOG_SYSLOG      (5<<3)  /* messages generated internally by 
syslogd */
  +     #define LOG_LPR         (6<<3)  /* line printer subsystem */
  +     #define LOG_NEWS        (7<<3)  /* network news subsystem */
  +     #define LOG_UUCP        (8<<3)  /* UUCP subsystem */
  +     #define LOG_CRON        (9<<3)  /* clock daemon */
  +     #define LOG_AUTHPRIV    (10<<3) /* security/authorization messages 
(private) */
  +     #define LOG_FTP         (11<<3) /* ftp daemon */
  +     
  +             /* other codes through 15 reserved for system use */
  +     #define LOG_LOCAL0      (16<<3) /* reserved for local use */
  +     #define LOG_LOCAL1      (17<<3) /* reserved for local use */
  +     #define LOG_LOCAL2      (18<<3) /* reserved for local use */
  +     #define LOG_LOCAL3      (19<<3) /* reserved for local use */
  +     #define LOG_LOCAL4      (20<<3) /* reserved for local use */
  +     #define LOG_LOCAL5      (21<<3) /* reserved for local use */
  +     #define LOG_LOCAL6      (22<<3) /* reserved for local use */
  +     #define LOG_LOCAL7      (23<<3) /* reserved for local use */
  +#endif
  +
  +#define LOG_UNDEF -1
  +
   using namespace log4cxx;
   using namespace log4cxx::helpers;
   using namespace log4cxx::net;
  @@ -31,10 +61,11 @@
   : syslogFacility(LOG_USER), facilityPrinting(false), sw(0)
   {
        this->initSyslogFacilityStr();
  +
   }
   
   SyslogAppender::SyslogAppender(const LayoutPtr& layout,
  -     SyslogAppender::SyslogFacility syslogFacility)
  +     int syslogFacility)
   : syslogFacility(syslogFacility), facilityPrinting(false), sw(0)
   {
        this->layout = layout;
  @@ -42,7 +73,7 @@
   }
   
   SyslogAppender::SyslogAppender(const LayoutPtr& layout,
  -     const String& syslogHost, SyslogAppender::SyslogFacility syslogFacility)
  +     const String& syslogHost, int syslogFacility)
   : syslogFacility(syslogFacility), facilityPrinting(false), sw(0)
   {
        this->layout = layout;
  @@ -88,7 +119,7 @@
   e.g. "kern", "user", etc.
   */
   String SyslogAppender::getFacilityString(
  -     SyslogAppender::SyslogFacility syslogFacility)
  +     int syslogFacility)
   {
        switch(syslogFacility)
        {
  @@ -116,7 +147,7 @@
        }
   }
   
  -SyslogAppender::SyslogFacility SyslogAppender::getFacility(
  +int SyslogAppender::getFacility(
        const String &facilityName)
   {
        String s = StringHelper::toUpperCase(StringHelper::trim(facilityName));
  @@ -212,7 +243,24 @@
        if      (!isAsSevereAsThreshold(event->getLevel()))
                return;
   
  -     // We must not attempt to append if sqw is null.
  +// On the local host, we can directly use the system function 'syslog'
  +// if it is available
  +#ifdef HAVE_SYSLOG   
  +     if (sw == 0)
  +     {
  +             StringBuffer sbuf;
  +             layout->format(sbuf, event);
  +             USES_CONVERSION;
  +             
  +             // use of "%s" to avoid a security hole
  +        ::syslog(syslogFacility | event->getLevel()->getSyslogEquivalent(), 
  +                     "%s", T2A(sbuf.str().c_str()));
  +                     
  +             return;
  +     }
  +#endif
  +
  +     // We must not attempt to append if sw is null.
        if(sw == 0)
        {
                errorHandler->error(_T("No syslog host is set for 
SyslogAppedender named \"")+
  @@ -254,7 +302,20 @@
   
   void SyslogAppender::setSyslogHost(const String& syslogHost)
   {
  -     this->sw = new SyslogWriter(syslogHost);
  +     if (this->sw != 0)
  +     {
  +             delete this->sw;
  +             this->sw = 0;
  +     }
  +     
  +// On the local host, we can directly use the system function 'syslog'
  +// if it is available (cf. append)
  +#ifdef HAVE_SYSLOG   
  +     if (syslogHost != _T("localhost") && syslogHost != _T("127.0.0.1")
  +     && !syslogHost.empty())
  +#endif               
  +             this->sw = new SyslogWriter(syslogHost);
  +             
        this->syslogHost = syslogHost;
   }
   
  
  
  

Reply via email to