handle_error.py
Go to the documentation of this file.
00001 
00002 import traceback, sys, string, time, socket, os
00003 import who_calls
00004 
00005 import cgitb
00006 
00007 #DUMP_DIR = "/neo/data/bugs"
00008 DUMP_DIR = "/tmp/bugs"
00009 if os.environ.has_key('PYCLEARSILVER_BUGS_PATH'):
00010   DUMP_DIR = os.environ['PYCLEARSILVER_BUGS_PATH']
00011 
00012 Warning = "handle_error.Warning"
00013 
00014 # levels
00015 LV_MESSAGE = "LV_MESSAGE"
00016 LV_WARNING = "LV_WARNING"
00017 LV_ERROR   = "LV_ERROR"
00018 
00019 Count = 0
00020 
00021 gErrorCount = 0
00022 DISABLE_DUMP = 0
00023 
00024 def exceptionReason():
00025   return "%s.%s" % (str(sys.exc_type), str(sys.exc_value))
00026 
00027 def exceptionString():
00028   import cStringIO
00029   sfp = cStringIO.StringIO()
00030   handler = cgitb.Hook(file=sfp, display=True)
00031   handler.handle()
00032   exception = sfp.getvalue()
00033   return exception
00034 
00035   #tb_list = traceback.format_exception(sys.exc_type,sys.exc_value,sys.exc_traceback)
00036   #return string.join(tb_list,"")
00037 
00038   return exception
00039 
00040 
00041 def handleException (msg=None, lvl=LV_ERROR, dump = 1):
00042   global gErrorCount
00043   gErrorCount = gErrorCount + 1
00044 
00045   tb_list = traceback.format_exception(sys.exc_type,sys.exc_value,sys.exc_traceback)
00046   if msg:
00047     sys.stderr.write ("%s\n" % msg)
00048   else:
00049     msg = "Unhandled Exception"
00050         
00051   sys.stderr.write (string.join(tb_list,""))
00052 
00053   try:
00054     if dump: dump_bug(lvl, "handleException", msg, string.join(tb_list, ""))
00055   except:
00056     handleException("Unable to dump_bug", dump = 0)
00057 
00058 def handleWarning (msg=""):
00059   header = "*** handleWarning: %s\n" % msg
00060   sys.stderr.write(header)
00061   tb = who_calls.pretty_who_calls(strip=1) + "\n"
00062   sys.stderr.write(tb)
00063 
00064   try:
00065     dump_bug(LV_WARNING, "handleException", msg, tb)
00066   except:
00067     handleException("Unable to dump_bug", dump = 0)
00068 
00069 def checkPaths():
00070   paths = (DUMP_DIR, 
00071            os.path.join (DUMP_DIR, "tmp"),
00072            os.path.join (DUMP_DIR, "new"))
00073   for path in paths:
00074     if not os.path.isdir(path):
00075       os.mkdir(path, 0755)
00076 
00077 
00078 def dump_bug (level, etype, msg, location=None, nhdf=None):
00079     global DISABLE_DUMP
00080     if DISABLE_DUMP: return
00081 
00082     now = int(time.time())
00083     pid = os.getpid()
00084 
00085     import neo_cgi, neo_util
00086     hdf = neo_util.HDF()
00087     hdf.setValue("Required.Level", level)
00088     hdf.setValue("Required.When", str(int(time.time())))
00089     hdf.setValue("Required.Type", etype)
00090     hdf.setValue("Required.Title", msg)
00091     hdf.setValue("Optional.Hostname", socket.gethostname())
00092     if location:
00093         hdf.setValue("Optional.Location", location)
00094 
00095     for (key, value) in os.environ.items():
00096         hdf.setValue ("Environ.%s" % key, value)
00097 
00098     global Count
00099     Count = Count + 1
00100     fname = "%d.%d_%d.%s" % (now, pid, Count, socket.gethostname())
00101     checkPaths()
00102 
00103     tpath = os.path.join (DUMP_DIR, "tmp", fname)
00104     npath = os.path.join (DUMP_DIR, "new", fname)
00105     try:
00106       hdf.writeFile(tpath)
00107       os.rename(tpath, npath)
00108     except os.error, reason:
00109       warn("unable to write bug file", tpath)
00110       warn("reason:", reason)
00111 


pyclearsilver
Author(s): Scott Hassan/hassan@willowgarage.com
autogenerated on Wed Apr 23 2014 10:35:42