def Traceback(method=None, req=None, mail=1, ostream=sys.stderr, extra=None, severity="notification", with_locals=0): """ Reports an traceback error and optionally sends mail about it. NOTE: extra = extra text information. """ # pylint: disable=C0103 global QUIET_MAIL if mail: # safeguard if QUIET_MAIL is None: QUIET_MAIL = CFG.QUIET_MAIL if QUIET_MAIL < 0: QUIET_MAIL = 0 if QUIET_MAIL == 0: # make sure we don't mail mail = 0 e_type = sys.exc_info()[:2][0] t = time.ctime(time.time()) exc = StringIO() unicode_hostname = idn_puny_to_unicode(hostname) exc.write("Exception reported from %s\nTime: %s\n" % (to_string(unicode_hostname), t)) exc.write("Exception type %s\n" % to_string(e_type)) if method: exc.write("Exception while handling function %s\n" % to_string(method)) # print information about the request being served if req: print_req(req, exc) if extra: exc.write("Extra information about this error:\n%s\n" % to_string(extra)) # Print the traceback exc.write("\nException Handler Information\n") traceback.print_exc(None, exc) if with_locals and not mail: # The mail case will call print_locals by itself print_locals(exc) # we always log it somewhere if ostream: ostream.write(to_string(exc.getvalue())) ostream.write("\n") if mail: # print the stack frames for the mail we send out print_locals(exc) # dump the environment print_env(exc) # and send the mail # build the headers to = CFG.TRACEBACK_MAIL fr = to if isinstance(to, type([])): fr = to[0].strip() to = ', '.join([x.strip() for x in to]) headers = { "Subject": "%s TRACEBACK from %s" % (PRODUCT_NAME, unicode_hostname), "From": "%s <%s>" % (hostname, fr), "To": to, "X-RHN-Traceback-Severity": severity, "Content-Type": 'text/plain; charset="utf-8"', } QUIET_MAIL = QUIET_MAIL - 1 # count it no matter what outstring = to_string(exc.getvalue()) # 5/18/05 wregglej - 151158 Go through every string in the security list # and censor it out of the debug information. outstring = censor_string(outstring) rhnMail.send(headers, outstring) exc.close() return
def Traceback(method=None, req=None, mail=1, ostream=sys.stderr, extra=None, severity="notification", with_locals=0): """ Reports an traceback error and optionally sends mail about it. NOTE: extra = extra text information. """ global QUIET_MAIL if mail: # safeguard if QUIET_MAIL is None: QUIET_MAIL = CFG.QUIET_MAIL if QUIET_MAIL < 0: QUIET_MAIL = 0 if QUIET_MAIL == 0: # make sure we don't mail mail = 0 e_type, e_value = sys.exc_info()[:2] t = time.ctime(time.time()) exc = StringIO() exc.write("Exception reported from %s\nTime: %s\n" % (hostname, t)) exc.write("Exception type %s\n" % (e_type,)) if method: exc.write("Exception while handling function %s\n" % method) # print information about the request being served if req: print_req(req, exc) if extra: exc.write("Extra information about this error:\n%s\n" % extra) # Print the traceback exc.write("\nException Handler Information\n") traceback.print_exc(None, exc) if with_locals and not mail: # The mail case will call print_locals by itself print_locals(exc) # we always log it somewhere if ostream: ostream.write("%s\n" % exc.getvalue()) ret = 0 # default return value if mail: # print the stack frames for the mail we send out print_locals(exc) # dump the environment print_env(exc) # and send the mail # build the headers to = CFG.TRACEBACK_MAIL fr = to if isinstance(to, type([])): fr = string.strip(to[0]) to = string.join(map(string.strip, to), ", ") headers = { "Subject": "RHN TRACEBACK from %s" % hostname, "From": "%s <%s>" % (hostname, fr), "To": to, "X-RHN-Traceback-Severity": severity, } QUIET_MAIL = QUIET_MAIL - 1 # count it no matter what outstring = exc.getvalue() # 5/18/05 wregglej - 151158 Go through every string in the security list and censor it out of the debug information. outstring = censor_string(outstring) ret = rhnMail.send(headers, outstring) exc.close() return ret