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

Diff for /pegasus/src/Pegasus/Common/Tracer.cpp between version 1.1.2.1 and 1.6

version 1.1.2.1, 2001/07/31 23:57:08 version 1.6, 2002/03/12 01:14:28
Line 23 
Line 23 
 // //
 // Author: Sushma Fernandes, Hewlett-Packard Company (sushma_fernandes@hp.com) // Author: Sushma Fernandes, Hewlett-Packard Company (sushma_fernandes@hp.com)
 // //
 // Modified By:  // Modified By: Jenny Yu, Hewlett-Packard Company (jenny_yu@hp.com)
 // //
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
   #include <Pegasus/Common/Config.h>
 #include <Pegasus/Common/Tracer.h> #include <Pegasus/Common/Tracer.h>
 #include <Pegasus/Common/Destroyer.h> #include <Pegasus/Common/Destroyer.h>
   #include <Pegasus/Common/Thread.h>
   #include <Pegasus/Common/IPC.h>
   #include <Pegasus/Common/System.h>
  
 PEGASUS_USING_STD; PEGASUS_USING_STD;
  
Line 43 
Line 47 
 const Uint32 Tracer::LEVEL3 = (1 << 2); const Uint32 Tracer::LEVEL3 = (1 << 2);
 const Uint32 Tracer::LEVEL4 = (1 << 3); const Uint32 Tracer::LEVEL4 = (1 << 3);
  
   // Set the return codes
   const Boolean Tracer::_SUCCESS = 1;
   const Boolean Tracer::_FAILURE = 0;
   String  Tracer::_EMPTY_STRING = String::EMPTY;
   
 // Set the Enter and Exit messages // Set the Enter and Exit messages
 const char Tracer::_FUNC_ENTER_MSG[] = "Entering method"; const char Tracer::_FUNC_ENTER_MSG[] = "Entering method";
 const char Tracer::_FUNC_EXIT_MSG[]  = "Exiting method"; const char Tracer::_FUNC_EXIT_MSG[]  = "Exiting method";
  
 // Set Log messages // Set Log messages
 const char Tracer::_LOG_MSG1[] = "LEVEL1 not enabled with Tracer::trace call.";  const char Tracer::_LOG_MSG[] = "LEVEL1 may only be used with trace macros PEG_FUNC_ENTER/PEG_FUNC_EXIT or PEG_METHOD_ENTER/PEG_METHOD_EXIT.";
 const char Tracer::_LOG_MSG2[]="Use trace macros, PEG_FUNC_ENTER/PEG_FUNC_EXIT";  
  
 // Initialize singleton instance of Tracer // Initialize singleton instance of Tracer
 Tracer* Tracer::_tracerInstance = 0; Tracer* Tracer::_tracerInstance = 0;
Line 61 
Line 69 
 const Uint32 Tracer::_NUM_COMPONENTS = const Uint32 Tracer::_NUM_COMPONENTS =
     sizeof(TRACE_COMPONENT_LIST)/sizeof(TRACE_COMPONENT_LIST[0]);     sizeof(TRACE_COMPONENT_LIST)/sizeof(TRACE_COMPONENT_LIST[0]);
  
   // Set the line maximum
   const Uint32 Tracer::_STRLEN_MAX_UNSIGNED_INT = 21;
   
   // Set the max PID and Thread ID Length
   const Uint32 Tracer::_STRLEN_MAX_PID_TID = 20;
   
 //////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
 // Tracer constructor // Tracer constructor
 // Constructor is private to preclude construction of Tracer objects // Constructor is private to preclude construction of Tracer objects
Line 74 
Line 88 
     _traceComponentMask=new Boolean[_NUM_COMPONENTS];     _traceComponentMask=new Boolean[_NUM_COMPONENTS];
  
     // Initialize ComponentMask array to false     // Initialize ComponentMask array to false
     for (int index=0;index < _NUM_COMPONENTS;      for (Uint32 index=0;index < _NUM_COMPONENTS;
         _traceComponentMask[index++]=false);         _traceComponentMask[index++]=false);
 } }
  
Line 102 
Line 116 
     {     {
         // ATTN: Setting the Log file type to DEBUG_LOG         // ATTN: Setting the Log file type to DEBUG_LOG
         // May need to change to an appropriate log file type         // May need to change to an appropriate log file type
         Logger::put(Logger::DEBUG_LOG,"Tracer",Logger::WARNING,"$0 $1",          Logger::put(Logger::DEBUG_LOG,"Tracer",Logger::WARNING,"$0", _LOG_MSG);
            _LOG_MSG1,_LOG_MSG2);  
     }     }
     else     else
     {     {
Line 129 
Line 142 
  
     if ( traceLevel == LEVEL1 )     if ( traceLevel == LEVEL1 )
     {     {
         Logger::put(Logger::DEBUG_LOG,"Tracer",Logger::WARNING,"$0 $1",          Logger::put(Logger::DEBUG_LOG,"Tracer",Logger::WARNING,"$0", _LOG_MSG);
            _LOG_MSG1,_LOG_MSG2);  
      }      }
      else      else
      {      {
          if (_isTraceEnabled(traceComponent,traceLevel))          if (_isTraceEnabled(traceComponent,traceLevel))
          {          {
             message = new char[strlen(fileName)+strlen(ltoa((long)lineNum))+6];              //
             sprintf(message,"[%s:%d]: ",fileName,lineNum);              // Allocate memory for the message string
               // Needs to be updated if additional info is added
               //
               message = new char[ strlen(fileName) +
                   _STRLEN_MAX_UNSIGNED_INT + _STRLEN_MAX_PID_TID ];
               sprintf(
                  message,
                  "[%d:%d:%s:%d]: ",
                  System::getPID(),
                  pegasus_thread_self(),
                  fileName,
                  lineNum);
   
             _trace(traceComponent,message,fmt,argList);             _trace(traceComponent,message,fmt,argList);
             delete []message;             delete []message;
          }          }
Line 145 
Line 169 
 } }
  
 //////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
   //Traces the given buffer
   ////////////////////////////////////////////////////////////////////////////////
   void Tracer::_traceBuffer(
       const Uint32 traceComponent,
       const Uint32 traceLevel,
       const char*  data,
       const Uint32 size)
   {
       if ( traceLevel == LEVEL1 )
       {
           Logger::put(Logger::DEBUG_LOG,"Tracer",Logger::WARNING,"$0", _LOG_MSG);
       }
       else
       {
           if (_isTraceEnabled(traceComponent,traceLevel))
           {
               char* tmpBuf = new char[size+1];
   
               strncpy( tmpBuf, data, size );
               tmpBuf[size] = '\0';
               trace(traceComponent,traceLevel,"%s",tmpBuf);
   
               delete []tmpBuf;
           }
       }
   }
   ////////////////////////////////////////////////////////////////////////////////
   //Traces the given buffer - Overloaded for including FileName and Line number
   ////////////////////////////////////////////////////////////////////////////////
   void Tracer::_traceBuffer(
       const char* fileName,
       const Uint32 lineNum,
       const Uint32 traceComponent,
       const Uint32 traceLevel,
       const char*  data,
       const Uint32 size)
   {
       if ( traceLevel == LEVEL1 )
       {
           Logger::put(Logger::DEBUG_LOG,"Tracer",Logger::WARNING,"$0", _LOG_MSG);
       }
       else
       {
           if ( _isTraceEnabled( traceComponent, traceLevel ) )
           {
               char* tmpBuf = new char[size+1];
   
               strncpy( tmpBuf, data, size );
               tmpBuf[size] = '\0';
               trace(fileName,lineNum,traceComponent,traceLevel,"%s",tmpBuf);
   
               delete []tmpBuf;
           }
       }
   }
   
   ////////////////////////////////////////////////////////////////////////////////
   //Traces the given string
   ////////////////////////////////////////////////////////////////////////////////
   void Tracer::_traceString(
       const Uint32   traceComponent,
       const Uint32   traceLevel,
       const String&  traceString)
   {
       if ( traceLevel == LEVEL1 )
       {
           Logger::put(Logger::DEBUG_LOG,"Tracer",Logger::WARNING,"$0", _LOG_MSG);
       }
       else
       {
           if (_isTraceEnabled(traceComponent,traceLevel))
           {
               String msg = traceString;
               ArrayDestroyer<char> traceMsg(msg.allocateCString());
               trace(traceComponent,traceLevel,"%s",traceMsg);
           }
       }
   }
   
   ////////////////////////////////////////////////////////////////////////////////
   //Traces the given string - Overloaded to include the fileName and line number
   //of trace origin.
   ////////////////////////////////////////////////////////////////////////////////
   void Tracer::_traceString(
       const char*   fileName,
       const Uint32  lineNum,
       const Uint32  traceComponent,
       const Uint32  traceLevel,
       const String& traceString)
   {
       if ( traceLevel == LEVEL1 )
       {
           Logger::put(Logger::DEBUG_LOG,"Tracer",Logger::WARNING,"$0", _LOG_MSG);
       }
       else
       {
           if ( _isTraceEnabled( traceComponent, traceLevel ) )
           {
               String msg = traceString;
               ArrayDestroyer<char> traceMsg(msg.allocateCString());
               trace(fileName,lineNum,traceComponent,traceLevel,"%s",traceMsg);
           }
       }
   }
   
   ////////////////////////////////////////////////////////////////////////////////
   //Traces the message in the given CIMException object.
   ////////////////////////////////////////////////////////////////////////////////
   void Tracer::_traceCIMException(
       const Uint32 traceComponent,
       const Uint32 traceLevel,
       CIMException cimException)
   {
       if ( traceLevel == LEVEL1 )
       {
           Logger::put(Logger::DEBUG_LOG,"Tracer",Logger::WARNING,"$0", _LOG_MSG);
       }
       else
       {
           if ( _isTraceEnabled( traceComponent, traceLevel ) )
           {
               // get the CIMException trace message string
               String traceMsg = cimException.getTraceMessage();
   
               // trace the string
               _traceString(traceComponent, traceLevel, traceMsg);
           }
       }
   }
   
   ////////////////////////////////////////////////////////////////////////////////
 //Traces method entry //Traces method entry
 //////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
 void Tracer::_traceEnter( void Tracer::_traceEnter(
Line 159 
Line 314 
  
     if (_isTraceEnabled(traceComponent,LEVEL1))     if (_isTraceEnabled(traceComponent,LEVEL1))
     {     {
   
         va_start(argList,fmt);         va_start(argList,fmt);
         message = new char[strlen(fileName)+strlen(ltoa((long)lineNum))+6];  
         sprintf(message,"[%s:%d]: ",fileName,lineNum);          //
           // Allocate memory for the message string
           // Needs to be updated if additional info is added
           //
           message = new char[ strlen(fileName) +
               _STRLEN_MAX_UNSIGNED_INT + _STRLEN_MAX_PID_TID ];
           sprintf(
              message,
              "[%d:%d:%s:%d]: ",
              System::getPID(),
              pegasus_thread_self(),
              fileName,
              lineNum);
         _trace(traceComponent,message,fmt,argList);         _trace(traceComponent,message,fmt,argList);
   
         va_end(argList);         va_end(argList);
         delete []message;         delete []message;
     }     }
Line 184 
Line 353 
     if (_isTraceEnabled(traceComponent,LEVEL1))     if (_isTraceEnabled(traceComponent,LEVEL1))
     {     {
         va_start(argList,fmt);         va_start(argList,fmt);
         message = new char[strlen(fileName)+strlen(ltoa((long)lineNum))+6];  
         sprintf(message,"[%s:%d]: ",fileName,lineNum);          //
           // Allocate memory for the message string
           // Needs to be updated if additional info is added
           //
           message = new char[ strlen(fileName) +
               _STRLEN_MAX_UNSIGNED_INT + _STRLEN_MAX_PID_TID ];
           sprintf(
              message,
              "[%d:%d:%s:%d]: ",
              System::getPID(),
              pegasus_thread_self(),
              fileName,
              lineNum);
         _trace(traceComponent,message,fmt,argList);         _trace(traceComponent,message,fmt,argList);
         va_end(argList);         va_end(argList);
   
         delete []message;         delete []message;
     }     }
 } }
Line 199 
Line 381 
     const Uint32 traceLevel)     const Uint32 traceLevel)
 { {
     Tracer* instance = _getInstance();     Tracer* instance = _getInstance();
     if ((traceComponent < 0) || (traceComponent >= _NUM_COMPONENTS ))      if (traceComponent >= _NUM_COMPONENTS)
     {     {
         return false;         return false;
     }     }
Line 217 
Line 399 
     va_list argList)     va_list argList)
 { {
     char* msgHeader;     char* msgHeader;
     Uint32 retCode;  
  
     // Get the current system time and prepend to message     // Get the current system time and prepend to message
     String currentTime = System::getCurrentASCIITime();     String currentTime = System::getCurrentASCIITime();
     ArrayDestroyer<char> timeStamp(currentTime.allocateCString());     ArrayDestroyer<char> timeStamp(currentTime.allocateCString());
  
       //
     // Allocate messageHeader.     // Allocate messageHeader.
       // Needs to be updated if additional info is added
       //
     msgHeader = new char [strlen(message)     msgHeader = new char [strlen(message)
         + strlen(TRACE_COMPONENT_LIST[traceComponent])         + strlen(TRACE_COMPONENT_LIST[traceComponent])
         + strlen(timeStamp.getPointer()) + 6];          + strlen(timeStamp.getPointer()) + _STRLEN_MAX_PID_TID];
  
     // Construct the message header     // Construct the message header
     // The message header is in the following format     // The message header is in the following format
Line 238 
Line 422 
     }     }
     else     else
     {     {
         sprintf(msgHeader,"%s: %s ",timeStamp.getPointer(),          //
             TRACE_COMPONENT_LIST[traceComponent] );          // Since the message is blank form a string using the pid and tid
           //
           char*  tmpBuffer;
   
           //
           // Allocate messageHeader.
           // Needs to be updated if additional info is added
           //
           tmpBuffer = new char[_STRLEN_MAX_PID_TID];
           sprintf(tmpBuffer,"[%d:%d]: ",System::getPID(),pegasus_thread_self());
   
           sprintf(msgHeader,"%s: %s %s ",timeStamp.getPointer(),
               TRACE_COMPONENT_LIST[traceComponent] ,tmpBuffer );
           delete []tmpBuffer;
     }     }
  
     // Call trace file handler to write message     // Call trace file handler to write message
Line 248 
Line 445 
 } }
  
 //////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
 //Set the trace file  //Validate the trace file
 //////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
 Uint32 Tracer::setTraceFile(const char* traceFile)  Boolean Tracer::isValid(const char* filePath)
 { {
     ofstream outFile;      return (_getInstance()->_traceHandler->isValidFilePath(filePath));
     Uint32 retCode = 0;  }
   
   ////////////////////////////////////////////////////////////////////////////////
   //Validate the trace components
   ////////////////////////////////////////////////////////////////////////////////
   Boolean Tracer::isValid(
                        const String traceComponents, String& invalidComponents)
   {
       // Validate the trace components and modify the traceComponents argument
       // to reflect the invalid components
   
       Uint32    position=0;
       Uint32    index=0;
       String    componentName = String::EMPTY;
       String    componentStr = String::EMPTY;
       Boolean   validComponent=false;
       Boolean   retCode=true;
   
       componentStr = traceComponents;
       invalidComponents = String::EMPTY;
   
       if (componentStr != String::EMPTY)
       {
           // Check if ALL is specified
           if (String::equalNoCase(componentStr,"ALL"))
           {
               return _SUCCESS;
           }
   
           // Append _COMPONENT_SEPARATOR to the end of the traceComponents
           componentStr += _COMPONENT_SEPARATOR;
  
     // Check if the file can be opened in append mode          while (componentStr != String::EMPTY)
     if (traceFile)  
     {     {
         outFile.open(traceFile,ofstream::app);              //
               // Get the Component name from traceComponents.
               // Components are separated by _COMPONENT_SEPARATOR
               //
               position = componentStr.find(_COMPONENT_SEPARATOR);
               componentName = componentStr.subString(0,(position));
   
               // Lookup the index for Component name in TRACE_COMPONENT_LIST
               index = 0;
               validComponent = false;
  
         if (outFile.good())              while (index < _NUM_COMPONENTS)
         {         {
             _getInstance()->_traceHandler->setFileName (traceFile);                  if (String::equalNoCase(
             outFile.close();                         componentName, TRACE_COMPONENT_LIST[index]))
                   {
                       // Found component, break from the loop
                       validComponent = true;
                       break;
         }         }
         else         else
         {         {
             outFile.close();                     index++;
             retCode = 1;                  }
               }
   
               // Remove the searched componentname from the traceComponents
               componentStr.remove(0,position+1);
   
               if ( !validComponent )
               {
                   invalidComponents += componentName;
                   invalidComponents += _COMPONENT_SEPARATOR;
               }
         }         }
     }     }
     else     else
     {     {
         retCode=1;          // trace components is empty, it is a valid value so return true
           return _SUCCESS;
       }
       if ( invalidComponents != String::EMPTY )
       {
           retCode = false;
           //
           // Remove the extra ',' at the end
           //
           invalidComponents.remove(
               invalidComponents.reverseFind(_COMPONENT_SEPARATOR));
     }     }
     return retCode;     return retCode;
 } }
  
 //////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
   //Returns the Singleton instance of the Tracer
   ////////////////////////////////////////////////////////////////////////////////
   Tracer* Tracer::_getInstance()
   {
       if (_tracerInstance == 0)
       {
           _tracerInstance = new Tracer();
       }
       return _tracerInstance;
   }
   
   // PEGASUS_REMOVE_TRACE defines the compile time inclusion of the Trace
   // interfaces. If defined the interfaces map to empty functions
   
   #ifndef PEGASUS_REMOVE_TRACE
   
   ////////////////////////////////////////////////////////////////////////////////
   //Set the trace file
   ////////////////////////////////////////////////////////////////////////////////
   Uint32 Tracer::setTraceFile(const char* traceFile)
   {
       return (_getInstance()->_traceHandler->setFileName (traceFile));
   }
   
   ////////////////////////////////////////////////////////////////////////////////
 //Set the trace level //Set the trace level
 //////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
 Uint32 Tracer::setTraceLevel(const Uint32 traceLevel) Uint32 Tracer::setTraceLevel(const Uint32 traceLevel)
Line 312 
Line 596 
  
 //////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
 //Set components to be traced. //Set components to be traced.
 //The String traceComponents will be updated within the function.  
 //////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
 void Tracer::setTraceComponents(String traceComponents)  void Tracer::setTraceComponents( const String traceComponents )
 { {
     Uint32 position=0;     Uint32 position=0;
     Uint32 index=0;     Uint32 index=0;
     String componentName;      String componentName     = String::EMPTY;
       String componentStr      = traceComponents;
       String invalidComponents = String::EMPTY;
  
     if (traceComponents != String::EMPTY)      if (componentStr != String::EMPTY)
     {     {
         // Check if ALL is specified         // Check if ALL is specified
         if (traceComponents == "ALL")          if (String::equalNoCase(componentStr,"ALL"))
         {         {
             for (int index=0; index < _NUM_COMPONENTS;              for (index=0; index < _NUM_COMPONENTS;
                 _getInstance()->_traceComponentMask[index++] = true);                 _getInstance()->_traceComponentMask[index++] = true);
             return;             return;
         }         }
  
         // initialise ComponentMask array to False         // initialise ComponentMask array to False
         for (int index = 0;index < _NUM_COMPONENTS;          for (index = 0;index < _NUM_COMPONENTS;
             _getInstance()->_traceComponentMask[index++] = false);             _getInstance()->_traceComponentMask[index++] = false);
  
         // Append _COMPONENT_SEPARATOR to the end of the traceComponents         // Append _COMPONENT_SEPARATOR to the end of the traceComponents
         traceComponents += _COMPONENT_SEPARATOR;          componentStr += _COMPONENT_SEPARATOR;
  
         while (traceComponents != String::EMPTY)          while (componentStr != String::EMPTY)
         {         {
             // Get the Component name from traceComponents.             // Get the Component name from traceComponents.
             // Components are separated by _COMPONENT_SEPARATOR             // Components are separated by _COMPONENT_SEPARATOR
             position = traceComponents.find(_COMPONENT_SEPARATOR);              position = componentStr.find(_COMPONENT_SEPARATOR);
             componentName = traceComponents.subString(0,(position));              componentName = componentStr.subString(0,(position));
  
             // Lookup the index for Component name in TRACE_COMPONENT_LIST             // Lookup the index for Component name in TRACE_COMPONENT_LIST
             index = 0;             index = 0;
             while (index < _NUM_COMPONENTS)             while (index < _NUM_COMPONENTS)
             {             {
                 if (componentName == TRACE_COMPONENT_LIST[index])                  if (String::equalNoCase(
                          componentName,TRACE_COMPONENT_LIST[index]))
                 {                 {
                     _getInstance()->_traceComponentMask[index]=true;                     _getInstance()->_traceComponentMask[index]=true;
  
Line 362 
Line 648 
             }             }
  
             // Remove the searched componentname from the traceComponents             // Remove the searched componentname from the traceComponents
             traceComponents.remove(0,position+1);              componentStr.remove(0,position+1);
         }         }
     }     }
     else     else
     {     {
         // initialise ComponentMask array to False         // initialise ComponentMask array to False
         for (int index = 0;index < _NUM_COMPONENTS;          for (Uint32 index = 0;index < _NUM_COMPONENTS;
             _getInstance()->_traceComponentMask[index++] = false);             _getInstance()->_traceComponentMask[index++] = false);
     }     }
       return ;
 } }
  
 ////////////////////////////////////////////////////////////////////////////////  #endif
 //Returns the Singleton instance of the Tracer  
 ////////////////////////////////////////////////////////////////////////////////  
 Tracer* Tracer::_getInstance()  
 {  
     if (_tracerInstance == 0)  
     {  
         _tracerInstance = new Tracer();  
     }  
     return _tracerInstance;  
 }  
  
 PEGASUS_NAMESPACE_END PEGASUS_NAMESPACE_END


Legend:
Removed from v.1.1.2.1  
changed lines
  Added in v.1.6

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2