(file) Return to Logger.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

Diff for /pegasus/src/Pegasus/Common/Logger.cpp between version 1.47 and 1.59

version 1.47, 2006/11/08 20:38:30 version 1.59, 2008/05/27 17:35:41
Line 34 
Line 34 
 #include <iostream> #include <iostream>
 #include <fstream> #include <fstream>
 #include <cstring> #include <cstring>
 #include "Logger.h"  #include <Pegasus/Common/Logger.h>
 #include "System.h"  #include <Pegasus/Common/Tracer.h>
 #include <Pegasus/Common/MessageLoader.h> //l10n  #include <Pegasus/Common/System.h>
   #include <Pegasus/Common/FileSystem.h>
   #include <Pegasus/Common/MessageLoader.h>
   #include <Pegasus/Common/Executor.h>
   #include <Pegasus/Common/Mutex.h>
  
 #if defined(PEGASUS_USE_SYSLOGS) #if defined(PEGASUS_USE_SYSLOGS)
 # include <syslog.h> # include <syslog.h>
Line 46 
Line 50 
  
 PEGASUS_NAMESPACE_BEGIN PEGASUS_NAMESPACE_BEGIN
  
   // Maximum logfile size is defined as 32 MB = 32 * 1024 * 1024
   # define PEGASUS_MAX_LOGFILE_SIZE 0X2000000
   
 const Uint32 Logger::TRACE = (1 << 0); const Uint32 Logger::TRACE = (1 << 0);
 const Uint32 Logger::INFORMATION = (1 << 1); const Uint32 Logger::INFORMATION = (1 << 1);
 const Uint32 Logger::WARNING = (1 << 2); const Uint32 Logger::WARNING = (1 << 2);
Line 66 
Line 73 
  
 const Uint32 Logger::_NUM_LOGLEVEL = 5; const Uint32 Logger::_NUM_LOGLEVEL = 5;
  
 // Set separator  
 const char Logger::_SEPARATOR = '@';  
   
 Uint32 Logger::_severityMask; Uint32 Logger::_severityMask;
  
 Uint32 Logger::_writeControlMask = 0xF;   // Set all on by default  ///////////////////////////////////////////////////////////////////////////////
   //
   // LoggerRep
   //
   ///////////////////////////////////////////////////////////////////////////////
   
   #if defined(PEGASUS_USE_SYSLOGS)
  
 // Set the return codes  class LoggerRep
 const Boolean Logger::_SUCCESS = 1;  {
 const Boolean Logger::_FAILURE = 0;  public:
   
 /* _allocLogFileName. Allocates the name from a name set.      LoggerRep(const String& homeDirectory)
     Today this is static.  However, it should be completely      {
     configerable and driven from the config file so that  # ifdef PEGASUS_OS_ZOS
     Log organization and names are open.          logIdentity = strdup(System::CIMSERVER.getCString());
     ATTN: rewrite this so that names, choice to do logs and          // If System Log is used open it
     mask for level of severity are all driven from configuration          System::openlog(logIdentity, LOG_PID, LOG_DAEMON);
     input.  # endif
 */      }
 static CString _allocLogFileName(  
     const String& homeDirectory,      ~LoggerRep()
     Logger::LogFileType logFileType)      {
   # ifdef PEGASUS_OS_ZOS
           System::closelog();
           free(logIdentity);
   # endif
       }
   
       // Actual logging is done in this routine
       void log(Logger::LogFileType logFileType,
           const String& systemId,
           Uint32 logLevel,
           const String localizedMsg)
 { {
           // Log the message
           System::syslog(systemId, logLevel, localizedMsg.getCString());
       }
   
   private:
   
   # ifdef PEGASUS_OS_ZOS
       char* logIdentity;
   # endif
   };
   
   #else    // !defined(PEGASUS_USE_SYSLOGS)
   
     static const char* fileNames[] =     static const char* fileNames[] =
     {     {
         "PegasusTrace.log",         "PegasusTrace.log",
Line 97 
Line 131 
         "PegasusError.log",         "PegasusError.log",
         "PegasusDebug.log"         "PegasusDebug.log"
     };     };
   static const char* lockFileName = "PegasusLog.lock";
  
     int index = int(logFileType);  /*
       _constructFileName builds the absolute file name from homeDirectory
     if (index > Logger::NUM_LOGS)      and fileName.
         index = Logger::ERROR_LOG;  */
   static CString _constructFileName(
     const char* logFileName = fileNames[index];      const String& homeDirectory,
       const char * fileName)
   {
     String result;     String result;
     result.reserveCapacity(homeDirectory.size() + 1 + strlen(logFileName));      result.reserveCapacity(
           (Uint32)(homeDirectory.size() + 1 + strlen(fileName)));
     result.append(homeDirectory);     result.append(homeDirectory);
     result.append('/');     result.append('/');
     result.append(logFileName);      result.append(fileName);
     return result.getCString();     return result.getCString();
 } }
  
Line 119 
Line 156 
  
     LoggerRep(const String& homeDirectory)     LoggerRep(const String& homeDirectory)
     {     {
 #if !defined(PEGASUS_USE_SYSLOGS)  
         // Add test for home directory set.         // Add test for home directory set.
  
         // If home directory does not exist, create it.         // If home directory does not exist, create it.
Line 130 
Line 166 
  
         // KS: I put the second test in just in case some trys to create         // KS: I put the second test in just in case some trys to create
         // a completly erronous directory.  At least we will get a message         // a completly erronous directory.  At least we will get a message
         if (!System::isDirectory(lgDir)){          if (!System::isDirectory(lgDir))
            //l10n          {
            //cerr << "Logging Disabled";  
            MessageLoaderParms parms("Common.Logger.LOGGING_DISABLED",            MessageLoaderParms parms("Common.Logger.LOGGING_DISABLED",
                "Logging Disabled");                "Logging Disabled");
  
            cerr << MessageLoader::getMessage(parms);            cerr << MessageLoader::getMessage(parms);
         }         }
  
         CString fileName = _allocLogFileName(homeDirectory, Logger::TRACE_LOG);         //Filelocks are not used for VMS
         _logs[Logger::TRACE_LOG].open(fileName, ios::app);  # if !defined(PEGASUS_OS_VMS)
           _loggerLockFileName = _constructFileName(homeDirectory, lockFileName);
         fileName = _allocLogFileName(homeDirectory, Logger::STANDARD_LOG);  
         _logs[Logger::STANDARD_LOG].open(fileName, ios::app);          // Open and close a file to make sure that the file exists, on which
           // file lock is requested
 #ifndef PEGASUS_DISABLE_AUDIT_LOGGER          FILE *fileLockFilePointer;
         fileName = _allocLogFileName(homeDirectory, Logger::AUDIT_LOG);          fileLockFilePointer = fopen(_loggerLockFileName, "a+");
         _logs[Logger::AUDIT_LOG].open(fileName, ios::app);          if(fileLockFilePointer)
           {
               fclose(fileLockFilePointer);
           }
 #endif #endif
  
         fileName = _allocLogFileName(homeDirectory, Logger::ERROR_LOG);          _logFileNames[Logger::TRACE_LOG] = _constructFileName(homeDirectory,
         _logs[Logger::ERROR_LOG].open(fileName, ios::app);                                                 fileNames[Logger::TRACE_LOG]);
   
         fileName = _allocLogFileName(homeDirectory, Logger::DEBUG_LOG);  
         _logs[Logger::DEBUG_LOG].open(fileName, ios::app);  
 #else  
  
 #ifdef PEGASUS_OS_ZOS          _logFileNames[Logger::STANDARD_LOG] = _constructFileName(homeDirectory,
        logIdendity = System::CIMSERVER.getCString();                                                 fileNames[Logger::STANDARD_LOG]);
         // If System Log is used open it  
         System::openlog(logIdendity, LOG_PID, LOG_DAEMON);  
 #endif  
  
   # ifdef PEGASUS_ENABLE_AUDIT_LOGGER
           _logFileNames[Logger::AUDIT_LOG] = _constructFileName(homeDirectory,
                                                  fileNames[Logger::AUDIT_LOG]);
 #endif #endif
  
           _logFileNames[Logger::ERROR_LOG] = _constructFileName(homeDirectory,
                                                  fileNames[Logger::ERROR_LOG]);
     }     }
  
     ~LoggerRep()     ~LoggerRep()
     {     {
 #if !defined(PEGASUS_USE_SYSLOGS)      }
         _logs[Logger::TRACE_LOG].close();  
         _logs[Logger::STANDARD_LOG].close();  
  
 #ifndef PEGASUS_DISABLE_AUDIT_LOGGER      // Actual logging is done in this routine
         _logs[Logger::AUDIT_LOG].close();      void log(Logger::LogFileType logFileType,
 #endif          const String& systemId,
           Uint32 logLevel,
           const String localizedMsg)
       {
           // Prepend the systemId to the incoming message
           String messageString(systemId);
           messageString.append(": ");
           messageString.append(localizedMsg);  // l10n
  
         _logs[Logger::ERROR_LOG].close();          // Get the logLevel String
         _logs[Logger::DEBUG_LOG].close();          // This converts bitmap to string based on highest order
           // bit set
           // ATTN: KS Fix this more efficiently.
           const char* tmp = "";
           if (logLevel & Logger::TRACE) tmp =       "TRACE   ";
           if (logLevel & Logger::INFORMATION) tmp = "INFO    ";
           if (logLevel & Logger::WARNING) tmp =     "WARNING ";
           if (logLevel & Logger::SEVERE) tmp =      "SEVERE  ";
           if (logLevel & Logger::FATAL) tmp =       "FATAL   ";
  
 #else  # ifndef PEGASUS_OS_VMS
           // Acquire AutoMutex (for thread sync)
           // and AutoFileLock (for Process Sync).
           AutoMutex am(_mutex);
           AutoFileLock fileLock(_loggerLockFileName);
  
 #ifdef PEGASUS_OS_ZOS          Uint32  logFileSize = 0;
         System::closelog();  
  
 #endif          // Read logFileSize to check if the logfile needs to be pruned.
           FileSystem::getFileSize(String(_logFileNames[logFileType]),
                                          logFileSize);
  
 #endif          // Check if the size of the logfile is exceeding 32MB.
           if ( logFileSize > PEGASUS_MAX_LOGFILE_SIZE)
           {
               // Prepare appropriate file name based on the logFileType.
               // Eg: if Logfile name is PegasusStandard.log, pruned logfile name
               // will be PegasusStandard-062607-122302.log,where 062607-122302
               // is the time stamp.
               String prunedLogfile(_logFileNames[logFileType],
                                   (Uint32)strlen(_logFileNames[logFileType]) - 4);
               prunedLogfile.append('-');
  
     }              // Get timestamp,remove illegal chars in file name'/' and ':'
               // (: is illegal Open VMS) from the time stamp. Append the time
               // info to the file name.
  
     ostream& logOf(Logger::LogFileType logFileType)              String timeStamp = System::getCurrentASCIITime();
               for (unsigned int i=0; i<=timeStamp.size(); i++)
     {     {
         int index = int(logFileType);                  if(timeStamp[i] == '/' || timeStamp[i] == ':')
                   {
                       timeStamp.remove(i, 1);
                   }
               }
               prunedLogfile.append(timeStamp);
   
               // Append '.log' to the file
               prunedLogfile.append( ".log");
  
         if (index > int(Logger::NUM_LOGS))              // Rename the logfile
             index = Logger::ERROR_LOG;              FileSystem::renameFile(String(_logFileNames[logFileType]),
                                      prunedLogfile);
  
         return _logs[index];          } // Check if the logfile needs to be pruned.
   # endif  // ifndef PEGASUS_OS_VMS
   
           // Open Logfile. Based on the value of logFileType, one of the five
           // Logfiles will be opened.
           ofstream logFileStream;
           logFileStream.open(_logFileNames[logFileType], ios::app);
           logFileStream << System::getCurrentASCIITime()
              << " " << tmp << (const char *)messageString.getCString() << endl;
           logFileStream.close();
     }     }
  
 private: private:
  
 #ifdef PEGASUS_OS_ZOS      CString _logFileNames[int(Logger::NUM_LOGS)];
     CString logIdendity;  
   # ifndef PEGASUS_OS_VMS
       CString _loggerLockFileName;
       Mutex _mutex;
 #endif #endif
     ofstream _logs[int(Logger::NUM_LOGS)];  
 }; };
  
   #endif    // !defined(PEGASUS_USE_SYSLOGS)
   
   
   ///////////////////////////////////////////////////////////////////////////////
   //
   // Logger
   //
   ///////////////////////////////////////////////////////////////////////////////
   
 void Logger::_putInternal( void Logger::_putInternal(
     LogFileType logFileType,     LogFileType logFileType,
     const String& systemId,     const String& systemId,
     const Uint32 logComponent, // TODO: Support logComponent mask in future release      const Uint32 logComponent, // FUTURE: Support logComponent mask
     Uint32 logLevel,     Uint32 logLevel,
     const String& formatString,     const String& formatString,
     const String& messageId,  // l10n      const String& messageId,
     const Formatter::Arg& arg0,     const Formatter::Arg& arg0,
     const Formatter::Arg& arg1,     const Formatter::Arg& arg1,
     const Formatter::Arg& arg2,     const Formatter::Arg& arg2,
Line 234 
Line 329 
         if (!_rep)         if (!_rep)
            _rep = new LoggerRep(_homeDirectory);            _rep = new LoggerRep(_homeDirectory);
  
         // Get the logLevel String  
         // This converts bitmap to string based on highest order  
         // bit set  
         // ATTN: KS Fix this more efficiently.  
         static const char* svNames[] =  
         {  
             "TRACE   ",  
             "INFO    ",  
             "WARNING ",  
             "SEVERE  ",  
             "FATAL   "  
         };  
         // NUM_LEVELS = 5  
         int sizeSvNames = sizeof(svNames) / sizeof(svNames[0]) - 1;  
  
 // l10n start // l10n start
         // The localized message to be sent to the system log.         // The localized message to be sent to the system log.
Line 280 
Line 361 
         }         }
 // l10n end // l10n end
  
 #if defined(PEGASUS_USE_SYSLOGS)         // Call the actual logging routine is in LoggerRep.
          _rep->log(logFileType, systemId, logLevel, localizedMsg);
         // Log the message  
         System::syslog(systemId, logLevel, localizedMsg.getCString());  
   
 #else  
   
         // Prepend the systemId to the incoming message  
         String messageString(systemId);  
         messageString.append(": ");  
         messageString.append(localizedMsg);  // l10n  
   
         const char* tmp = "";  
         if (logLevel & Logger::TRACE) tmp =       "TRACE   ";  
         if (logLevel & Logger::INFORMATION) tmp = "INFO    ";  
         if (logLevel & Logger::WARNING) tmp =     "WARNING ";  
         if (logLevel & Logger::SEVERE) tmp =      "SEVERE  ";  
         if (logLevel & Logger::FATAL) tmp =       "FATAL   ";  
   
         _rep->logOf(logFileType) << System::getCurrentASCIITime()  
            << " " << tmp << (const char *)messageString.getCString() << endl;  
  
 #endif         // route log message to trace too -> component LogMessages
          if (Logger::TRACE_LOG != logFileType)
          {
              // do not write log message to trace when trace facility is
              // the log to avoid double messages
              if (Tracer::TRACE_FACILITY_LOG != Tracer::getTraceFacility())
              {
                  PEG_TRACE_CSTRING(
                      TRC_LOGMSG,
                      Tracer::LEVEL1,
                      (const char*) localizedMsg.getCString());
              }
          }
     }     }
 } }
  
Line 657 
Line 731 
  
 void Logger::setlogLevelMask( const String logLevelList ) void Logger::setlogLevelMask( const String logLevelList )
 { {
     Uint32 position          = 0;  
     Uint32 logLevelType = 0;     Uint32 logLevelType = 0;
     String logLevelName      = logLevelList;     String logLevelName      = logLevelList;
  
Line 707 
Line 780 
             case Logger::FATAL:             case Logger::FATAL:
                   _severityMask |= Logger::FATAL;                   _severityMask |= Logger::FATAL;
         }         }
   
           Executor::updateLogLevel(logLevelName.getCString());
     }     }
     else     else
     {     {
         // Property logLevel not specified, set default value.         // Property logLevel not specified, set default value.
         _severityMask = ~Logger::TRACE;         _severityMask = ~Logger::TRACE;
           Executor::updateLogLevel("INFORMATION");
     }     }
     return ;  
 } }
  
 Boolean Logger::isValidlogLevel(const String logLevel) Boolean Logger::isValidlogLevel(const String logLevel)
Line 721 
Line 796 
     // Validate the logLevel and modify the logLevel argument     // Validate the logLevel and modify the logLevel argument
     // to reflect the invalid logLevel     // to reflect the invalid logLevel
  
     Uint32    position=0;  
     Uint32    index=0;     Uint32    index=0;
     String    logLevelName = String::EMPTY;     String    logLevelName = String::EMPTY;
     Boolean   validlogLevel=false;     Boolean   validlogLevel=false;
     Boolean   retCode=true;  
  
     logLevelName = logLevel;     logLevelName = logLevel;
  
Line 752 
Line 825 
     else     else
     {     {
         // logLevels is empty, it is a valid value so return true         // logLevels is empty, it is a valid value so return true
         return _SUCCESS;          return true;
     }     }
  
     return validlogLevel;     return validlogLevel;


Legend:
Removed from v.1.47  
changed lines
  Added in v.1.59

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2