handle_error.py
Go to the documentation of this file.
1 
2 import traceback, sys, string, time, socket, os
3 import who_calls
4 
5 import cgitb
6 
7 #DUMP_DIR = "/neo/data/bugs"
8 DUMP_DIR = "/tmp/bugs"
9 if os.environ.has_key('PYCLEARSILVER_BUGS_PATH'):
10  DUMP_DIR = os.environ['PYCLEARSILVER_BUGS_PATH']
11 
12 Warning = "handle_error.Warning"
13 
14 # levels
15 LV_MESSAGE = "LV_MESSAGE"
16 LV_WARNING = "LV_WARNING"
17 LV_ERROR = "LV_ERROR"
18 
19 Count = 0
20 
21 gErrorCount = 0
22 DISABLE_DUMP = 0
23 
25  return "%s.%s" % (str(sys.exc_type), str(sys.exc_value))
26 
28  import cStringIO
29  sfp = cStringIO.StringIO()
30  handler = cgitb.Hook(file=sfp, display=True)
31  handler.handle()
32  exception = sfp.getvalue()
33  return exception
34 
35  #tb_list = traceback.format_exception(sys.exc_type,sys.exc_value,sys.exc_traceback)
36  #return string.join(tb_list,"")
37 
38  return exception
39 
40 
41 def handleException (msg=None, lvl=LV_ERROR, dump = 1):
42  global gErrorCount
43  gErrorCount = gErrorCount + 1
44 
45  tb_list = traceback.format_exception(sys.exc_type,sys.exc_value,sys.exc_traceback)
46  if msg:
47  sys.stderr.write ("%s\n" % msg)
48  else:
49  msg = "Unhandled Exception"
50 
51  sys.stderr.write (string.join(tb_list,""))
52 
53  try:
54  if dump: dump_bug(lvl, "handleException", msg, string.join(tb_list, ""))
55  except:
56  handleException("Unable to dump_bug", dump = 0)
57 
58 def handleWarning (msg=""):
59  header = "*** handleWarning: %s\n" % msg
60  sys.stderr.write(header)
61  tb = who_calls.pretty_who_calls(strip=1) + "\n"
62  sys.stderr.write(tb)
63 
64  try:
65  dump_bug(LV_WARNING, "handleException", msg, tb)
66  except:
67  handleException("Unable to dump_bug", dump = 0)
68 
69 def checkPaths():
70  paths = (DUMP_DIR,
71  os.path.join (DUMP_DIR, "tmp"),
72  os.path.join (DUMP_DIR, "new"))
73  for path in paths:
74  if not os.path.isdir(path):
75  os.mkdir(path, 0755)
76 
77 
78 def dump_bug (level, etype, msg, location=None, nhdf=None):
79  global DISABLE_DUMP
80  if DISABLE_DUMP: return
81 
82  now = int(time.time())
83  pid = os.getpid()
84 
85  import neo_cgi, neo_util
86  hdf = neo_util.HDF()
87  hdf.setValue("Required.Level", level)
88  hdf.setValue("Required.When", str(int(time.time())))
89  hdf.setValue("Required.Type", etype)
90  hdf.setValue("Required.Title", msg)
91  hdf.setValue("Optional.Hostname", socket.gethostname())
92  if location:
93  hdf.setValue("Optional.Location", location)
94 
95  for (key, value) in os.environ.items():
96  hdf.setValue ("Environ.%s" % key, value)
97 
98  global Count
99  Count = Count + 1
100  fname = "%d.%d_%d.%s" % (now, pid, Count, socket.gethostname())
101  checkPaths()
102 
103  tpath = os.path.join (DUMP_DIR, "tmp", fname)
104  npath = os.path.join (DUMP_DIR, "new", fname)
105  try:
106  hdf.writeFile(tpath)
107  os.rename(tpath, npath)
108  except os.error, reason:
109  warn("unable to write bug file", tpath)
110  warn("reason:", reason)
111 
def dump_bug(level, etype, msg, location=None, nhdf=None)
Definition: handle_error.py:78
def handleException(msg=None, lvl=LV_ERROR, dump=1)
Definition: handle_error.py:41
def warn(args)
Definition: log.py:100


pyclearsilver
Author(s): Scott Noob Hassan
autogenerated on Mon Jun 10 2019 15:51:13