< Previous by Date Date Index Next by Date >
  Thread Index  

[reSIProcate] rutil Log: give app control over syslog facility; don't hardcode LOCAL6


When I use the Log object to send logs to the syslog, the facility is hardcoded to LOG_LOCAL6 in SysLogBuf.cxx. Our app would rather use LOG_LOCAL0, given the filter we like to use in rsyslog. Rather than hardcode it to our preferred value, I have made it settable by the app. This entailed adding the new optional argument to Log::initialize, and down through the ThreadData object, then to SysLogStream and finally to SysLogBuf. The idea is quite simple, the messy part is all that new parameter passing, along with proper #ifdef WIN32. I didn't test with WIN32, but it should work OK, harmlessly ignoring the new facility.

The enclosed diffs are against the 1.9.6 versions in rutil.

-John Gregg

Index: Log.cxx
===================================================================
--- Log.cxx	(revision 27632)
+++ Log.cxx	(working copy)
@@ -119,14 +119,15 @@
 }
 
 void
-Log::initialize(const char* typed, const char* leveld, const char* appName, const char *logFileName, ExternalLogger* externalLogger)
+Log::initialize(const char* typed, const char* leveld, const char* appName, const char *logFileName, ExternalLogger* externalLogger, unsigned int syslogFacility)
 {
-   Log::initialize(Data(typed), Data(leveld), Data(appName), logFileName, externalLogger);
+    Log::initialize(Data(typed), Data(leveld), Data(appName), logFileName, externalLogger, syslogFacility);
 }
 
 void
 Log::initialize(const Data& typed, const Data& leveld, const Data& appName, 
-                const char *logFileName, ExternalLogger* externalLogger)
+                const char *logFileName, ExternalLogger* externalLogger,
+                unsigned int syslogFacility)
 {
    Type type = Log::Cout;
    if (isEqualNoCase(typed, "cout")) type = Log::Cout;
@@ -137,18 +138,19 @@
    Level level = Log::Info;
    level = toLevel(leveld);
 
-   Log::initialize(type, level, appName, logFileName, externalLogger);
+   Log::initialize(type, level, appName, logFileName, externalLogger, syslogFacility);
 }
 
 void 
 Log::initialize(Type type, Level level, const Data& appName, 
                 const char * logFileName,
-                ExternalLogger* externalLogger)
+                ExternalLogger* externalLogger,
+                unsigned int syslogFacility)
 {
    Lock lock(_mutex);
    mDefaultLoggerData.reset();   
    
-   mDefaultLoggerData.set(type, level, logFileName, externalLogger);
+   mDefaultLoggerData.set(type, level, logFileName, externalLogger, syslogFacility);
 
    ParseBuffer pb(appName);
    pb.skipToEnd();
@@ -173,9 +175,10 @@
 Log::initialize(Type type,
                 Level level,
                 const Data& appName,
-                ExternalLogger& logger)
+                ExternalLogger& logger,
+                unsigned int syslogFacility)
 {
-   initialize(type, level, appName, 0, &logger);
+    initialize(type, level, appName, 0, &logger, syslogFacility);
 }
 
 void
@@ -585,18 +588,20 @@
 Log::LocalLoggerId Log::localLoggerCreate(Log::Type type,
                                           Log::Level level,
                                           const char * logFileName,
-                                          ExternalLogger* externalLogger)
+                                          ExternalLogger* externalLogger,
+                                          unsigned int syslogFacility)
 {
-   return mLocalLoggerMap.create(type, level, logFileName, externalLogger);
+   return mLocalLoggerMap.create(type, level, logFileName, externalLogger, syslogFacility);
 }
 
 int Log::localLoggerReinitialize(Log::LocalLoggerId loggerId,
                                  Log::Type type,
                                  Log::Level level,
                                  const char * logFileName,
-                                 ExternalLogger* externalLogger)
+                                 ExternalLogger* externalLogger,
+                                 unsigned int syslogFacility)
 {
-   return mLocalLoggerMap.reinitialize(loggerId, type, level, logFileName, externalLogger);
+    return mLocalLoggerMap.reinitialize(loggerId, type, level, logFileName, externalLogger, syslogFacility);
 }
 
 int Log::localLoggerRemove(Log::LocalLoggerId loggerId)
@@ -673,12 +678,13 @@
 Log::LocalLoggerId Log::LocalLoggerMap::create(Log::Type type,
                                                     Log::Level level,
                                                     const char * logFileName,
-                                                    ExternalLogger* externalLogger)
+                                                    ExternalLogger* externalLogger,
+                                                    unsigned int syslogFacility)
 {
    Lock lock(mLoggerInstancesMapMutex);
    Log::LocalLoggerId id = ++mLastLocalLoggerId;
    Log::ThreadData *pNewData = new Log::ThreadData(id, type, level, logFileName,
-                                                   externalLogger);
+                                                   externalLogger, syslogFacility);
    mLoggerInstancesMap[id].first = pNewData;
    mLoggerInstancesMap[id].second = 0;
    return id;
@@ -688,7 +694,8 @@
                                       Log::Type type,
                                       Log::Level level,
                                       const char * logFileName,
-                                      ExternalLogger* externalLogger)
+                                      ExternalLogger* externalLogger,
+                                      unsigned int syslogFacility)
 {
    Lock lock(mLoggerInstancesMapMutex);
    LoggerInstanceMap::iterator it = mLoggerInstancesMap.find(loggerId);
@@ -699,7 +706,7 @@
       return 1;
    }
    it->second.first->reset();
-   it->second.first->set(type, level, logFileName, externalLogger);
+   it->second.first->set(type, level, logFileName, externalLogger, syslogFacility);
    return 0;
 }
 
@@ -828,7 +835,7 @@
          if (mLogger == 0)
          {
             std::cerr << "Creating a syslog stream" << std::endl;
-            mLogger = new SysLogStream;
+            mLogger = new SysLogStream(mSyslogFacility);
          }
          return *mLogger;
 
Index: Log.hxx
===================================================================
--- Log.hxx	(revision 27632)
+++ Log.hxx	(working copy)
@@ -173,21 +173,45 @@
                              Level level,
                              const Data& appName,
                              const char * logFileName = 0,
-                             ExternalLogger* externalLogger = 0);
+                             ExternalLogger* externalLogger = 0,
+#ifdef WIN32
+                             unsigned int syslogFacility = 0
+#else
+                             unsigned int syslogFacility = LOG_LOCAL6
+#endif
+                             );
       static void initialize(const Data& type,
                              const Data& level,
                              const Data& appName,
                              const char * logFileName = 0,
-                             ExternalLogger* externalLogger = 0);
+                             ExternalLogger* externalLogger = 0,
+#ifdef WIN32
+                             unsigned int syslogFacility = 0
+#else
+                             unsigned int syslogFacility = LOG_LOCAL6
+#endif
+                             );
       static void initialize(const char* type,
                              const char* level,
                              const char* appName,
                              const char * logFileName = 0,
-                             ExternalLogger* externalLogger = 0);
+                             ExternalLogger* externalLogger = 0,
+#ifdef WIN32
+                             unsigned int syslogFacility = 0
+#else
+                             unsigned int syslogFacility = LOG_LOCAL6
+#endif
+                             );
       static void initialize(Type type,
                              Level level,
                              const Data& appName,
-                             ExternalLogger& logger);
+                             ExternalLogger& logger,
+#ifdef WIN32
+                             unsigned int syslogFacility = 0
+#else
+                             unsigned int syslogFacility = LOG_LOCAL6
+#endif
+                             );
 
       /** @brief Set logging level for current thread.
       * If thread has no local logger attached, then set global logging level.
@@ -226,7 +250,13 @@
       static LocalLoggerId localLoggerCreate(Type type,
                                              Level level,
                                              const char * logFileName = NULL,
-                                             ExternalLogger* externalLogger = NULL);
+                                             ExternalLogger* externalLogger = NULL,
+#ifdef WIN32
+                                             unsigned int syslogFacility = 0
+#else
+                                             unsigned int syslogFacility = LOG_LOCAL6
+#endif
+                                             );
 
       /** Reinitialize all new setting for a local logger instance
       * @retval 0 on success
@@ -236,7 +266,13 @@
                                          Type type,
                                          Level level,
                                          const char * logFileName = NULL,
-                                         ExternalLogger* externalLogger = NULL);						
+                                         ExternalLogger* externalLogger = NULL,
+#ifdef WIN32
+                                         unsigned int syslogFacility = 0
+#else
+                                         unsigned int syslogFacility = LOG_LOCAL6
+#endif
+                                         );
 
       /** Destroy existing logger instance.
       * @retval 0 on success
@@ -277,7 +313,13 @@
          public:
             ThreadData(LocalLoggerId id, Type type=Cout, Level level=Info,
                        const char *logFileName=NULL,
-                       ExternalLogger *pExternalLogger=NULL)
+                       ExternalLogger *pExternalLogger=NULL,
+#ifdef WIN32
+                       unsigned int syslogFacility = 0
+#else
+                       unsigned int syslogFacility = LOG_LOCAL6
+#endif
+                       )
                : mLevel(level),
                  mMaxLineCount(0),
                  mMaxByteCount(0),
@@ -285,7 +327,8 @@
                  mId(id),
                  mType(type),
                  mLogger(NULL),
-                 mLineCount(0)
+                 mLineCount(0),
+                 mSyslogFacility(syslogFacility)
             {
                if (logFileName)
                {
@@ -296,7 +339,13 @@
 
             void set(Type type=Cout, Level level=Info,
                      const char *logFileName=NULL,
-                     ExternalLogger *pExternalLogger=NULL)
+                     ExternalLogger *pExternalLogger=NULL,
+#ifdef WIN32
+                     unsigned int syslogFacility = 0
+#else
+                     unsigned int syslogFacility = LOG_LOCAL6
+#endif
+                     )
             {
                mType = type;
                mLevel = level;
@@ -306,6 +355,7 @@
                   mLogFileName = logFileName;
                }
                mExternalLogger = pExternalLogger;
+               mSyslogFacility = syslogFacility;
             }
 
             LocalLoggerId id() const {return mId;}
@@ -331,6 +381,7 @@
             Data mLogFileName;
             std::ostream* mLogger;
             unsigned int mLineCount;
+            int mSyslogFacility;
       };
 
       static ThreadData mDefaultLoggerData; ///< Default logger settings.
@@ -361,7 +412,13 @@
          LocalLoggerId create(Type type,
                               Level level,
                               const char * logFileName = NULL,
-                              ExternalLogger* externalLogger = NULL);
+                              ExternalLogger* externalLogger = NULL,
+#ifdef WIN32
+                              unsigned int syslogFacility = 0
+#else
+                              unsigned int syslogFacility = LOG_LOCAL6
+#endif
+                              );
 
          /** Reinitialize all new setting for a local logger instance
           * @retval 0 on success
@@ -371,7 +428,13 @@
                           Type type,
                           Level level,
                           const char * logFileName = NULL,
-                          ExternalLogger* externalLogger = NULL);						
+                          ExternalLogger* externalLogger = NULL,
+#ifdef WIN32
+                          unsigned int syslogFacility = 0
+#else
+                          unsigned int syslogFacility = LOG_LOCAL6
+#endif
+                          );
 
          /** Remove existing logger instance from map and destroy.
          * @retval 0 on success
Index: SysLogBuf.cxx
===================================================================
--- SysLogBuf.cxx	(revision 27529)
+++ SysLogBuf.cxx	(working copy)
@@ -12,11 +12,11 @@
 
 using resip::SysLogBuf;
 
-SysLogBuf::SysLogBuf ()
+SysLogBuf::SysLogBuf (unsigned int facility) : mFacility(facility)
 {
 #if !defined(WIN32)
    setp(buffer,buffer+Size);
-   openlog (0, LOG_NDELAY, LOG_LOCAL6);
+   openlog (0, LOG_NDELAY, mFacility);
 #endif
 }
       
@@ -29,7 +29,7 @@
 {
 #if !defined(WIN32)
    *(pptr()) = 0;
-   syslog (LOG_LOCAL6 | LOG_DEBUG, "%s", pbase());
+   syslog (mFacility | LOG_DEBUG, "%s", pbase());
    setp(buffer, buffer+Size);
 #else
    assert(0);
Index: SysLogBuf.hxx
===================================================================
--- SysLogBuf.hxx	(revision 27529)
+++ SysLogBuf.hxx	(working copy)
@@ -9,7 +9,7 @@
 class SysLogBuf : public std::streambuf 
 {
    public:
-      SysLogBuf();
+      SysLogBuf(unsigned int facility);
       virtual ~SysLogBuf();
       
       int sync ();
@@ -22,6 +22,7 @@
    private:
       enum { Size=4095 }; 
       char buffer[Size+1];
+      unsigned int mFacility;
 };
  
 }
Index: SysLogStream.cxx
===================================================================
--- SysLogStream.cxx	(revision 27529)
+++ SysLogStream.cxx	(working copy)
@@ -9,8 +9,8 @@
 #pragma warning( disable : 4355 ) // using this in base member initializer list 
 #endif
 
-SysLogStream::SysLogStream() :
-   std::ostream (this)
+SysLogStream::SysLogStream(unsigned int facility) :
+    SysLogBuf(facility),std::ostream (this)
 {
 }
 
Index: SysLogStream.hxx
===================================================================
--- SysLogStream.hxx	(revision 27529)
+++ SysLogStream.hxx	(working copy)
@@ -10,7 +10,7 @@
 class SysLogStream : private SysLogBuf, public std::ostream
 {
    public:
-      SysLogStream();
+      SysLogStream(unsigned int facility);
       virtual ~SysLogStream();
 
    private: