version 1.51, 2008/06/26 18:32:39
|
version 1.52, 2008/09/02 17:33:37
|
|
|
#include <Pegasus/Common/Tracer.h> | #include <Pegasus/Common/Tracer.h> |
#include <Pegasus/Common/TraceFileHandler.h> | #include <Pegasus/Common/TraceFileHandler.h> |
#include <Pegasus/Common/TraceLogHandler.h> | #include <Pegasus/Common/TraceLogHandler.h> |
|
#include <Pegasus/Common/TraceMemoryHandler.h> |
#include <Pegasus/Common/Thread.h> | #include <Pegasus/Common/Thread.h> |
#include <Pegasus/Common/System.h> | #include <Pegasus/Common/System.h> |
#include <Pegasus/Common/HTTPMessage.h> | #include <Pegasus/Common/HTTPMessage.h> |
|
#include <Pegasus/Common/StringConversion.h> |
| |
PEGASUS_USING_STD; | PEGASUS_USING_STD; |
| |
|
|
{ | { |
"File", | "File", |
"Log", | "Log", |
|
"Memory", |
0 | 0 |
}; | }; |
| |
|
|
//////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////// |
Tracer::Tracer() | Tracer::Tracer() |
: _traceComponentMask(new Boolean[_NUM_COMPONENTS]), | : _traceComponentMask(new Boolean[_NUM_COMPONENTS]), |
|
_traceMemoryBufferSize(PEGASUS_TRC_DEFAULT_BUFFER_SIZE_KB), |
_traceFacility(TRACE_FACILITY_FILE), | _traceFacility(TRACE_FACILITY_FILE), |
_traceLevelMask(0), | _traceLevelMask(0), |
_traceHandler(0) | _traceHandler(0) |
{ | { |
// Instantiate trace handler according to configured facility | // Instantiate trace handler according to configured facility |
_traceHandler.reset( getTraceHandler(_traceFacility) ); |
_setTraceHandler(_traceFacility); |
| |
// Initialize ComponentMask array to false | // Initialize ComponentMask array to false |
for (Uint32 index=0;index < _NUM_COMPONENTS; | for (Uint32 index=0;index < _NUM_COMPONENTS; |
|
|
//////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////// |
Tracer::~Tracer() | Tracer::~Tracer() |
{ | { |
|
delete _traceHandler; |
delete _tracerInstance; | delete _tracerInstance; |
} | } |
| |
|
|
//////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////// |
//Factory function for the trace handler instances. | //Factory function for the trace handler instances. |
//////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////// |
TraceHandler* Tracer::getTraceHandler( Uint32 traceFacility ) |
void Tracer::_setTraceHandler( Uint32 traceFacility ) |
{ | { |
TraceHandler * trcHandler; |
TraceHandler * oldTrcHandler = _traceHandler; |
|
|
switch(traceFacility) | switch(traceFacility) |
{ | { |
case TRACE_FACILITY_LOG: | case TRACE_FACILITY_LOG: |
trcHandler = new TraceLogHandler(); |
_traceFacility = TRACE_FACILITY_LOG; |
|
_traceHandler = new TraceLogHandler(); |
|
break; |
|
|
|
case TRACE_FACILITY_MEMORY: |
|
_traceFacility = TRACE_FACILITY_MEMORY; |
|
_traceHandler = new TraceMemoryHandler(_traceMemoryBufferSize); |
break; | break; |
| |
case TRACE_FACILITY_FILE: | case TRACE_FACILITY_FILE: |
default: | default: |
trcHandler = new TraceFileHandler(); |
_traceFacility = TRACE_FACILITY_FILE; |
|
_traceHandler = new TraceFileHandler(); |
} | } |
| |
return trcHandler; |
delete oldTrcHandler; |
} | } |
| |
//////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////// |
|
|
va_list argList) | va_list argList) |
{ | { |
char* msgHeader; | char* msgHeader; |
|
Uint32 msgLen; |
|
Uint32 usec,sec; |
| |
// Get the current system time and prepend to message | // Get the current system time and prepend to message |
String currentTime = System::getCurrentASCIITime(); |
System::getCurrentTimeUsec(sec,usec); |
CString timeStamp = currentTime.getCString(); |
|
| |
// | // |
// Allocate messageHeader. | // Allocate messageHeader. |
|
|
// Construct the message header | // Construct the message header |
// The message header is in the following format | // The message header is in the following format |
// timestamp: <component name> [file name:line number] | // timestamp: <component name> [file name:line number] |
|
// |
|
// Format string length calculation: |
|
// 11(sec)+ 2('s-')+11(usec)+4('us: ')+1(' ')+1(\0) = 30 |
if (*message != '\0') | if (*message != '\0') |
{ | { |
// << Wed Jul 16 10:58:40 2003 mdd >> _STRLEN_MAX_PID_TID is not used |
|
// in this format string |
|
msgHeader = new char [strlen(message) + | msgHeader = new char [strlen(message) + |
strlen(TRACE_COMPONENT_LIST[traceComponent]) + |
strlen(TRACE_COMPONENT_LIST[traceComponent]) + 30]; |
strlen(timeStamp) + _STRLEN_MAX_PID_TID + 5]; |
|
| |
sprintf(msgHeader, "%s: %s %s", (const char*)timeStamp, |
msgLen = sprintf(msgHeader, "%us-%uus: %s %s", sec, usec, |
TRACE_COMPONENT_LIST[traceComponent], message); | TRACE_COMPONENT_LIST[traceComponent], message); |
} | } |
else | else |
{ | { |
// | // |
// Since the message is blank, form a string using the pid and tid |
|
// |
|
char* tmpBuffer; |
|
|
|
// |
|
// Allocate messageHeader. | // Allocate messageHeader. |
// Needs to be updated if additional info is added | // Needs to be updated if additional info is added |
// | // |
tmpBuffer = new char[2 * _STRLEN_MAX_PID_TID + 6]; |
// Format string length calculation: |
sprintf(tmpBuffer, "[%u:%s]: ", |
// 11(sec)+2('s-')+11(usec)+4('us: ')+ |
System::getPID(), Threads::id().buffer); |
// +2(' [')+1(':')+3(']: ')+1(\0) = 35 |
msgHeader = new char[strlen(timeStamp) + |
msgHeader = new char[2 * _STRLEN_MAX_PID_TID + |
strlen(TRACE_COMPONENT_LIST[traceComponent]) + |
strlen(TRACE_COMPONENT_LIST[traceComponent]) + 35]; |
strlen(tmpBuffer) + 1 + 5]; |
|
| |
sprintf(msgHeader, "%s: %s %s ", (const char*)timeStamp, |
msgLen = sprintf(msgHeader, "%us-%uus: %s [%u:%s]: ", sec, usec, |
TRACE_COMPONENT_LIST[traceComponent], tmpBuffer); |
TRACE_COMPONENT_LIST[traceComponent], |
delete [] tmpBuffer; |
System::getPID(), Threads::id().buffer); |
} | } |
| |
// Call trace file handler to write message | // Call trace file handler to write message |
_getInstance()->_traceHandler->handleMessage(msgHeader,fmt,argList); |
_getInstance()->_traceHandler->handleMessage(msgHeader,msgLen,fmt,argList); |
| |
delete [] msgHeader; | delete [] msgHeader; |
} | } |
|
|
const char* cstring) | const char* cstring) |
{ | { |
char* completeMessage; | char* completeMessage; |
|
Uint32 msgLen; |
|
Uint32 usec,sec; |
| |
// Get the current system time and prepend to message | // Get the current system time and prepend to message |
String currentTime = System::getCurrentASCIITime(); |
System::getCurrentTimeUsec(sec,usec); |
CString timeStamp = currentTime.getCString(); |
|
// | // |
// Allocate completeMessage. | // Allocate completeMessage. |
// Needs to be updated if additional info is added | // Needs to be updated if additional info is added |
|
|
// Construct the message header | // Construct the message header |
// The message header is in the following format | // The message header is in the following format |
// timestamp: <component name> [file name:line number] | // timestamp: <component name> [file name:line number] |
|
// |
|
// Format string length calculation: |
|
// 11(sec)+ 2('s-')+11(usec)+4('us: ')+1(' ')+1(\0) = 30 |
if (*message != '\0') | if (*message != '\0') |
{ | { |
// << Wed Jul 16 10:58:40 2003 mdd >> _STRLEN_MAX_PID_TID is not used |
|
// in this format string |
|
completeMessage = new char [strlen(message) + | completeMessage = new char [strlen(message) + |
strlen(TRACE_COMPONENT_LIST[traceComponent]) + | strlen(TRACE_COMPONENT_LIST[traceComponent]) + |
strlen(timeStamp) + _STRLEN_MAX_PID_TID + 5 + |
strlen(cstring) + 30]; |
strlen(cstring) ]; |
|
| |
sprintf(completeMessage, "%s: %s %s%s", (const char*)timeStamp, |
msgLen = sprintf(completeMessage, "%us-%uus: %s %s%s", sec, usec, |
TRACE_COMPONENT_LIST[traceComponent], message, cstring); | TRACE_COMPONENT_LIST[traceComponent], message, cstring); |
} | } |
else | else |
|
|
// Allocate messageHeader. | // Allocate messageHeader. |
// Needs to be updated if additional info is added | // Needs to be updated if additional info is added |
// | // |
tmpBuffer = new char[2 * _STRLEN_MAX_PID_TID + 6]; |
// Format string length calculation: |
sprintf(tmpBuffer, "[%u:%s]: ", |
// 11(sec)+2('s-')+11(usec)+4('us: ')+ |
System::getPID(), Threads::id().buffer); |
// +2(' [')+1(':')+3(']: ')+1(\0) = 35 |
|
completeMessage = new char[2 * _STRLEN_MAX_PID_TID + |
completeMessage = new char[strlen(timeStamp) + |
|
strlen(TRACE_COMPONENT_LIST[traceComponent]) + | strlen(TRACE_COMPONENT_LIST[traceComponent]) + |
strlen(tmpBuffer) + 1 + 5 + |
strlen(cstring) +35]; |
strlen(cstring)]; |
|
| |
sprintf(completeMessage, "%s: %s %s %s", (const char*)timeStamp, |
msgLen = sprintf(completeMessage, "%us-%uus: %s [%u:%s] %s", sec, usec, |
TRACE_COMPONENT_LIST[traceComponent], tmpBuffer, cstring); |
TRACE_COMPONENT_LIST[traceComponent], |
delete [] tmpBuffer; |
System::getPID(), Threads::id().buffer, |
|
cstring); |
} | } |
| |
// Call trace file handler to write message | // Call trace file handler to write message |
_getInstance()->_traceHandler->handleMessage(completeMessage); |
_getInstance()->_traceHandler->handleMessage(completeMessage,msgLen); |
| |
delete [] completeMessage; | delete [] completeMessage; |
} | } |
|
|
{ | { |
if (index != instance->_traceFacility) | if (index != instance->_traceFacility) |
{ | { |
instance->_traceFacility = index; |
instance->_setTraceHandler(index); |
instance->_traceHandler.reset( |
|
instance->getTraceHandler(instance->_traceFacility)); |
|
} | } |
retCode = 1; | retCode = 1; |
break; | break; |
|
|
return _getInstance()->_traceFacility; | return _getInstance()->_traceFacility; |
} | } |
| |
|
//////////////////////////////////////////////////////////////////////////////// |
|
// Set the size of the memory trace buffer |
|
//////////////////////////////////////////////////////////////////////////////// |
|
Boolean Tracer::setTraceMemoryBufferSize(Uint32 bufferSize) |
|
{ |
|
Tracer* instance = _getInstance(); |
|
instance->_traceMemoryBufferSize = bufferSize; |
|
|
|
// If we decide to dynamically change the trace buffer size, |
|
// this is where it needs to be implemented. |
|
return true; |
|
} |
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
|
// Flushes the trace buffer to traceFilePath. This method will only |
|
// have an effect when traceFacility=Memory. |
|
//////////////////////////////////////////////////////////////////////////////// |
|
void Tracer::flushTrace() |
|
{ |
|
_getInstance()->_traceHandler->flushTrace(); |
|
return; |
|
} |
|
|
|
|
void Tracer::traceEnter( | void Tracer::traceEnter( |
TracerToken& token, | TracerToken& token, |
const char* file, | const char* file, |
|
|
{ | { |
char* message; | char* message; |
| |
|
Uint32 msgLen; |
|
Uint32 usec,sec; |
|
|
|
// Get the current system time |
|
System::getCurrentTimeUsec(sec,usec); |
|
|
// | // |
// Allocate memory for the message string | // Allocate memory for the message string |
// Needs to be updated if additional info is added | // Needs to be updated if additional info is added |
// | // |
message = new char[strlen(fileName) + | message = new char[strlen(fileName) + |
_STRLEN_MAX_UNSIGNED_INT + (_STRLEN_MAX_PID_TID * 2) + 8]; |
_STRLEN_MAX_UNSIGNED_INT + (_STRLEN_MAX_PID_TID * 2) + 8 + |
sprintf( |
strlen(TRACE_COMPONENT_LIST[traceComponent]) + |
message, |
strlen(cstring) + 30]; |
"[%u:%s:%s:%u]: ", |
|
|
msgLen = sprintf(message, "%us-%uus: %s [%u:%s:%s:%u]: %s", |
|
sec, |
|
usec, |
|
TRACE_COMPONENT_LIST[traceComponent], |
System::getPID(), | System::getPID(), |
Threads::id().buffer, | Threads::id().buffer, |
fileName, | fileName, |
lineNum); |
lineNum, |
|
cstring); |
|
|
|
// Call trace file handler to write message |
|
_getInstance()->_traceHandler->handleMessage(message,msgLen); |
| |
_traceCString(traceComponent, message, cstring); |
|
delete [] message; | delete [] message; |
} | } |
| |