def __init__(self, error_type, error_code, exception=None, description="None", nvSeq=None, create=1, severity=None): ''' The constructor basically allows the developer to specify any number of parameters for an error trace, but requires none. Python is flexible enough to let us do this. Parameters: - error_type is the error type (a long) - error_code is the error code (a long) - exception is a previous exception from the ACS Error System, or a Python native exception, in which case, an ErrorTrace will be constructed. The traceback should be ok in most cases, but if you find that it isn't, a possible workaround is converting the python exception to an ACS exception using pyExceptionToCORBA() before passing it to an ACSError constructor. Remember, that if you don't use pyExceptionToCORBA(), if you are dealing with a native exception, you must pass create=1 to the ACSError constructor. - description is a stringified description of the errror - nvSeq is a name-value sequence describing the error condition. Each value should be of the type ACSErr.NameValue - create with a value of 1 implies error information will be added to the stack - severity is the ACSErr severity of the error Returns: Nothing Raises: Nothing ''' if nvSeq == None: nvSeq = [] # Get a Logger instance self.timehelper = TimeUtil() #If create has not been changed by the developer we create a new error trace #appending the old one if it exists if create == 1: self.errorTrace = ErrorTrace(error_type, error_code, exception, description, nvSeq, 3, severity) #Someone has caught a CORBA exception and is trying to convert it into #this helper class. elif exception != None: #If the previous exception is an ACS Error System Exception if hasattr(exception, "errorTrace"): #We can use an error stack... self.errorTrace = exception.errorTrace #if the previous exception was actually an ACSErr.Completion with a #non-empty error trace elif hasattr(exception, "previousError") and (len( exception.previousError) == 1): self.errorTrace = exception.previousError[0]
def test_errorTraceToString(self): """ErrorTraceHelper errorTraceToString creates formatted message""" rv = 'ErrorTrace (TimeStamp=Tue Feb 3 21:42:26 2009,\n File=myfile,\n Line=myline,\n Routine=myroutine,\n Host=myhost,\n Process=myprocess,\n Thread=mythread,\n Type=0,\n Code=0,\n ShortDescrip=Description,\n Severity=Routine,\n Data: )\n' tu = TimeUtil() et = ACSErr.ErrorTrace('myfile', 'myline', 'myroutine', 'myhost', 'myprocess', 'mythread', tu.py2epoch(1233697346).value, 'mySourceObj', 0, 0, 'Routine', 'Description', [], None) self.assertEqual(rv, self.eth.errorTraceToString(et, ' '))
def test_Print(self): """ErrorTraceHelper Print produces output for a single trace""" tu = TimeUtil() et = ACSErr.ErrorTrace('myfile', 'myline', 'myroutine', 'myhost', 'myprocess', 'mythread', tu.py2epoch(1233697346).value, 'mySourceObj', 0, 0, 'Routine', 'Description', [], []) self.eth = ET.ErrorTraceHelper(et) self.eth.Print() self.assertEqual('write', sys.stdout.method_calls[0][0])
def errorTraceToString(self, error_trace, ws): ''' Converts an error trace to a human-readable string. Parameters: error_trace is an errortrace ws is whitespace Returns: Nothing Raises: Nothing ''' #figure out a nice format for time first epoch = acstime.Duration( error_trace.timeStamp) #convert to an ACS epoch timehelper = TimeUtil() epoch = timehelper.epoch2py(epoch) #convert to Python time epoch = gmtime(epoch) #convert to gm time epoch = asctime(epoch) #convert to nice string format nice_space = " " for i in range(0, len(ws) / 4): nice_space = nice_space + " " message = "ErrorTrace (" message = message + "TimeStamp=" + epoch + "," + linesep message = message + nice_space + "File=" + str( error_trace.file) + "," + linesep message = message + nice_space + "Line=" + str( error_trace.lineNum) + "," + linesep message = message + nice_space + "Routine=" + str( error_trace.routine) + "," + linesep message = message + nice_space + "Host=" + str( error_trace.host) + "," + linesep message = message + nice_space + "Process=" + str( error_trace.process) + "," + linesep message = message + nice_space + "Thread=" + str( error_trace.thread) + "," + linesep message = message + nice_space + "Type=" + str( error_trace.errorType) + "," + linesep message = message + nice_space + "Code=" + str( error_trace.errorCode) + "," + linesep message = message + nice_space + "ShortDescrip=" + str( error_trace.shortDescription) + "," + linesep message = message + nice_space + "Severity=" + str( error_trace.severity) + "," + linesep message = message + nice_space + "Data: " for i in error_trace.data: message = message + "Name=" + str(i.name) + ", Value=" + str( i.value) + "; " message = message + ")" + linesep return message
def to_posixtime(tm): """ Convert an ACS epoch to a POSIX timestamp... ...such as is returned by time.time(). @param tm: A time in 100 nanoseconds that have passed since October 15, 1582. @type tm: acstime.Epoch @return: The POSIX timestamp. @rtype: float """ return TimeUtil().epoch2py(tm)
def test_log(self): """ErrorTraceHelper log records messages on stdout and ACS logger""" rv = 'ErrorTrace (TimeStamp=Tue Feb 3 21:42:26 2009,\n File=myfile,\n Line=myline,\n Routine=myroutine,\n Host=myhost,\n Process=myprocess,\n Thread=mythread,\n Type=0,\n Code=0,\n ShortDescrip=Description,\n Severity=Routine,\n Data: )\n' logger = mock.Mock(spec=Logger) tu = TimeUtil() et = ACSErr.ErrorTrace('myfile', 'myline', 'myroutine', 'myhost', 'myprocess', 'mythread', tu.py2epoch(1233697346).value, 'mySourceObj', 0, 0, 'Routine', 'Description', [], []) self.eth = ET.ErrorTraceHelper(et) self.eth.log(logger, ACSLog.ACS_LOG_TRACE) self.assertEqual('logErrorTrace', logger.method_calls[0][0]) self.assertEqual(2, len(sys.stdout.method_calls))
def test_Print_nested(self): """ErrorTraceHelper Print produces output for a nested trace""" tu = TimeUtil() etg = ACSErr.ErrorTrace('myfile', 'myline', 'myroutine', 'myhost', 'grandson', 'mythread', tu.py2epoch(1233697346).value, 'mySourceObj', 0, 0, 'Routine', 'Description', [], []) ets = ACSErr.ErrorTrace('myfile', 'myline', 'myroutine', 'myhost', 'son', 'mythread', tu.py2epoch(1233697346).value, 'mySourceObj', 0, 0, 'Routine', 'Description', [], [etg]) etf = ACSErr.ErrorTrace('myfile', 'myline', 'myroutine', 'myhost', 'father', 'mythread', tu.py2epoch(1233697346).value, 'mySourceObj', 0, 0, 'Routine', 'Description', [], [ets]) self.eth = ET.ErrorTraceHelper(etf) self.eth.Print() self.assertEqual(6, len(sys.stdout.method_calls))
def sendLog(self, record): ''' Method which sends logs to the real ACS logging service. ''' if (not self.logThrottle.checkPublishLogRecord()): if self.logThrottleAlarmSender != None and not self.logThrottleAlarmActive: self.logThrottleAlarmActive = True self.logThrottleAlarmSender.sendThrottleAlarm(True) return else: if self.logThrottleAlarmSender != None and self.logThrottleAlarmActive: self.logThrottleAlarmActive = False self.logThrottleAlarmSender.sendThrottleAlarm(False) # Create an RTContext object rt_context = ACSLog.RTContext( str(record.threadName).replace("<", "").replace(">", ""), str(record.source).replace("<", "").replace(">", ""), str(gethostname()).replace("<", "").replace(">", ""), "", str(record.name).replace("<", "").replace(">", "")) src_info = ACSLog.SourceInfo( str(record.module).replace("<", "").replace(">", ""), "Unknown", long(record.lineno)) # Put remaining keyword arguments into NVPairSeq data = [] if TimeUtil().py2epoch(time()).value > TimeUtil().py2epoch( record.created + self.timestampThreshold).value: # Reformat the record originalMsg = NVPair("Original message", record.getMessage()) data.append(originalMsg) originalTimestamp = NVPair("Original timestamp", record.asctime) data.append(originalTimestamp) record = self.replaceOldRecord(record) #timestamp acs_timestamp = TimeUtil().py2epoch(record.created).value if 'priority' in record.__dict__: # The more exotic log functions have priority keyword arguments if 'errortrace' in record.__dict__: # The message is an ErrorTrace. self.logSvc.logErrorWithPriority(record.errortrace, record.priority) elif 'data' in record.__dict__: # The message is a type-safe log message self.logSvc.logWithPriority( record.priority, acs_timestamp, record.getMessage(), record.rtCont if 'rtCont' in record.__dict__ and record.rtCont is not None else rt_context, record.srcInfo if 'srcInfo' in record.__dict__ and record.srcInfo is not None else src_info, record.data, record.audience, record.array, record.antenna) else: # The message is a not-so-type-safe message self.logSvc.logWithAudience(record.priority, acs_timestamp, record.getMessage(), rt_context, src_info, record.audience, record.array, record.antenna) elif record.levelname == 'TRACE': self.logSvc.logTrace(acs_timestamp, record.getMessage(), rt_context, src_info, data) elif record.levelname == 'DELOUSE': self.logSvc.logDelouse(acs_timestamp, record.getMessage(), rt_context, src_info, data) elif record.levelname == 'DEBUG': self.logSvc.logDebug(acs_timestamp, record.getMessage(), rt_context, src_info, data) elif record.levelname == 'INFO': self.logSvc.logInfo(acs_timestamp, record.getMessage(), rt_context, src_info, data) elif record.levelname == 'NOTICE': self.logSvc.logNotice(acs_timestamp, record.getMessage(), rt_context, src_info, data) elif record.levelname == 'WARNING' or record.levelname == 'WARN': self.logSvc.logWarning(acs_timestamp, record.getMessage(), rt_context, src_info, data) #this is a special case because logError only takes #in error traces elif record.levelname == 'ERROR': self.logSvc.logWithAudience(ACSLog.ACS_LOG_ERROR, acs_timestamp, record.getMessage(), rt_context, src_info, NO_AUDIENCE, "", "") elif record.levelname == 'CRITICAL': self.logSvc.logCritical(acs_timestamp, record.getMessage(), rt_context, src_info, data) elif record.levelname == 'ALERT': self.logSvc.logAlert(acs_timestamp, record.getMessage(), rt_context, src_info, data) elif record.levelname == 'EMERGENCY': self.logSvc.logEmergency(acs_timestamp, record.getMessage(), rt_context, src_info, data) #failsafe else: self.logSvc.logCritical(acs_timestamp, record.getMessage(), rt_context, src_info, data)
def testLogTypeSafeInvalidPriority(self): """Logger class Type-safe logging with invalid priority""" msg = "LogTypeSafe Message" ts = TimeUtil().py2epoch(time.time()).value self.assertRaises(KeyError, self.mylogger.logTypeSafe, 25, ts, msg, None, None, None)