📄 tracer.cpp
字号:
// // Allocate messageHeader. // Needs to be updated if additional info is added // tmpBuffer = new char[2 * _STRLEN_MAX_PID_TID + 6]; sprintf(tmpBuffer, "[%u:%s]: ", System::getPID(), Threads::id().buffer); msgHeader = new char[strlen(timeStamp) + strlen(TRACE_COMPONENT_LIST[traceComponent]) + strlen(tmpBuffer) + 1 + 5]; sprintf(msgHeader, "%s: %s %s ", (const char*)timeStamp, TRACE_COMPONENT_LIST[traceComponent], tmpBuffer); delete [] tmpBuffer; } // Call trace file handler to write message _getInstance()->_traceHandler->handleMessage(msgHeader,fmt,argList); delete [] msgHeader;}//////////////////////////////////////////////////////////////////////////////////Validate the trace file////////////////////////////////////////////////////////////////////////////////Boolean Tracer::isValidFileName(const char* filePath){ String moduleName = _getInstance()->_moduleName; if (moduleName == String::EMPTY) { return _getInstance()->_traceHandler->isValidFilePath(filePath); } else { String extendedFilePath = String(filePath) + "." + moduleName; return _getInstance()->_traceHandler->isValidFilePath( extendedFilePath.getCString()); }}//////////////////////////////////////////////////////////////////////////////////Validate the trace components////////////////////////////////////////////////////////////////////////////////Boolean Tracer::isValidComponents(const String& traceComponents){ String invalidComponents; return isValidComponents(traceComponents, invalidComponents);}Boolean Tracer::isValidComponents( 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.append(_COMPONENT_SEPARATOR); while (componentStr != String::EMPTY) { // // 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; while (index < _NUM_COMPONENTS) { if (String::equalNoCase( componentName, TRACE_COMPONENT_LIST[index])) { // Found component, break from the loop validComponent = true; break; } else { index++; } } // Remove the searched componentname from the traceComponents componentStr.remove(0,position+1); if (!validComponent) { invalidComponents.append(componentName); invalidComponents.append(_COMPONENT_SEPARATOR); } } } else { // 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;}//////////////////////////////////////////////////////////////////////////////////Set the name of the module being traced////////////////////////////////////////////////////////////////////////////////void Tracer::setModuleName(const String& moduleName){ _getInstance()->_moduleName = moduleName;}//////////////////////////////////////////////////////////////////////////////////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. This section defines the trace functions IF the remove// trace flag is NOT set. If it is set, they are defined as empty functions// in the header file.#ifndef PEGASUS_REMOVE_TRACE//////////////////////////////////////////////////////////////////////////////////Set the trace file////////////////////////////////////////////////////////////////////////////////Uint32 Tracer::setTraceFile(const char* traceFile){ if (*traceFile == 0) { return 1; } String moduleName = _getInstance()->_moduleName; if (moduleName == String::EMPTY) { return _getInstance()->_traceHandler->setFileName(traceFile); } else { String extendedTraceFile = String(traceFile) + "." + moduleName; return _getInstance()->_traceHandler->setFileName( extendedTraceFile.getCString()); }}//////////////////////////////////////////////////////////////////////////////////Set the trace level////////////////////////////////////////////////////////////////////////////////Uint32 Tracer::setTraceLevel(const Uint32 traceLevel){ Uint32 retCode = 0; switch (traceLevel) { case LEVEL1: _getInstance()->_traceLevelMask = 0x01; break; case LEVEL2: _getInstance()->_traceLevelMask = 0x03; break; case LEVEL3: _getInstance()->_traceLevelMask = 0x07; break; case LEVEL4: _getInstance()->_traceLevelMask = 0x0F; break; default: _getInstance()->_traceLevelMask = 0; retCode = 1; } return retCode;}////////////////////////////////////////////////////////////////////////////////// Set components to be traced.////////////////////////////////////////////////////////////////////////////////void Tracer::setTraceComponents(const String& traceComponents){ Uint32 position = 0; Uint32 index = 0; String componentName = String::EMPTY; String componentStr = traceComponents; String invalidComponents = String::EMPTY; if (componentStr != String::EMPTY) { // Check if ALL is specified if (String::equalNoCase(componentStr,"ALL")) { for (index = 0; index < _NUM_COMPONENTS; index++) { (_getInstance()->_traceComponentMask.get())[index] = true; } _traceOn = true; return; } // initialize ComponentMask array to False for (index = 0; index < _NUM_COMPONENTS; index++) { (_getInstance()->_traceComponentMask.get())[index] = false; } _traceOn = false; // Append _COMPONENT_SEPARATOR to the end of the traceComponents componentStr.append(_COMPONENT_SEPARATOR); while (componentStr != String::EMPTY) { // 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; while (index < _NUM_COMPONENTS) { if (String::equalNoCase( componentName,TRACE_COMPONENT_LIST[index])) { (_getInstance()->_traceComponentMask.get())[index] = true; _traceOn = true; // Found component, break from the loop break; } else { index++; } } // Remove the searched componentname from the traceComponents componentStr.remove(0,position+1); } } else { // initialise ComponentMask array to False for (Uint32 index = 0;index < _NUM_COMPONENTS; index++) { (_getInstance()->_traceComponentMask.get())[index] = false; } _traceOn = 0; } return ;}void Tracer::traceEnter( TracerToken& token, Uint32 traceComponent, const char* method){ if (_traceOn) { token.component = traceComponent; token.method = method; _traceEnter( "unknown", 0, traceComponent, "%s %s", _METHOD_ENTER_MSG, method); }}void Tracer::traceExit(TracerToken& token){ if (_traceOn) _traceExit( "unknown",0, token.component, "%s %s", _METHOD_EXIT_MSG, token.method);}void Tracer::traceEnter( TracerToken& token, const char* file, size_t line, Uint32 traceComponent, const char* method){ if (_traceOn) { token.component = traceComponent; token.method = method; _traceEnter( file, line, traceComponent, "%s %s", _METHOD_ENTER_MSG, method); }}void Tracer::traceExit( TracerToken& token, const char* file, size_t line){ if (_traceOn) _traceExit( file, line, token.component, "%s %s", _METHOD_EXIT_MSG, token.method);}void Tracer::traceBuffer( const Uint32 traceComponent, const Uint32 traceLevel, const char* data, const Uint32 size){ if (_traceOn) _traceBuffer(traceComponent, traceLevel, data, size);}void Tracer::traceBuffer( const char* fileName, const Uint32 lineNum, const Uint32 traceComponent, const Uint32 traceLevel, const char* data, const Uint32 size){ if (_traceOn) { _traceBuffer( fileName, lineNum, traceComponent, traceLevel, data, size); }}void Tracer::trace( const Uint32 traceComponent, const Uint32 traceLevel, const char *fmt, ...){ if (_traceOn) { va_list argList; va_start(argList,fmt); _trace(traceComponent,traceLevel,fmt,argList); va_end(argList); }}void Tracer::trace( const char* fileName, const Uint32 lineNum, const Uint32 traceComponent, const Uint32 traceLevel, const char* fmt, ...){ if (_traceOn) { va_list argList; va_start(argList,fmt); _trace(fileName,lineNum,traceComponent,traceLevel,fmt,argList); va_end(argList); }}void Tracer::trace( const char* fileName, const Uint32 lineNum, const Uint32 traceComponent, const Uint32 traceLevel, const String& traceString){ if (_traceOn) { _traceString( fileName, lineNum, traceComponent, traceLevel, traceString); }}void Tracer::traceCIMException( const Uint32 traceComponent, const Uint32 traceLevel, const CIMException& cimException){ if (_traceOn) { _traceCIMException(traceComponent, traceLevel, cimException); }}void Tracer::trace( const Uint32 traceComponent, const Uint32 level, const String& string){ trace("unknown", 0, traceComponent, level, string);}#endif /* !PEGASUS_REMOVE_TRACE */PEGASUS_NAMESPACE_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -