]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/ide/activegrid/util/aglogging.py
No longer building the C++ version of the OGL lib
[wxWidgets.git] / wxPython / samples / ide / activegrid / util / aglogging.py
1 #----------------------------------------------------------------------------
2 # Name: aglogging.py
3 # Purpose: Utilities to help with logging
4 #
5 # Author: Jeff Norton
6 #
7 # Created: 01/04/05
8 # CVS-ID: $Id$
9 # Copyright: (c) 2005 ActiveGrid, Inc.
10 # License: wxWindows License
11 #----------------------------------------------------------------------------
12
13 import sys
14 import os
15 import re
16 import traceback
17
18 global agTestMode
19 agTestMode = False
20
21 def setTestMode(mode):
22 global agTestMode
23 if (mode):
24 agTestMode = True
25 else:
26 agTestMode = False
27
28 def getTestMode():
29 global agTestMode
30 return agTestMode
31
32 def testMode(normalObj, testObj=None):
33 if getTestMode():
34 return testObj
35 return normalObj
36
37 def toDiffableString(value):
38 s = repr(value)
39 ds = ""
40 i = s.find(" at 0x")
41 start = 0
42 while (i >= 0):
43 j = s.find(">", i)
44 if (j < i):
45 break
46 ds += s[start:i]
47 start = j
48 i = s.find(" at 0x", start)
49 return ds + s[start:]
50
51 def removeFileRefs(str):
52 str = re.sub(r'(?<=File ")[^"]*(\\[^\\]*")(, line )[0-9]*', _fileNameReplacement, str)
53 return str
54
55 def _fileNameReplacement(match):
56 return "...%s" % match.group(1)
57
58 def getTraceback():
59 extype, val, tb = sys.exc_info()
60 tbs = "\n"
61 for s in traceback.format_tb(tb):
62 tbs += s
63 return tbs
64
65 def reportException(out=None, stacktrace=False, diffable=False):
66 extype, val, t = sys.exc_info()
67 if (diffable):
68 exstr = removeFileRefs(str(val))
69 else:
70 exstr = str(val)
71 if (out == None):
72 print "Got Exception = %s: %s" % (extype, exstr)
73 else:
74 print >> out, "Got Exception = %s: %s" % (extype, exstr)
75 if (stacktrace):
76 fmt = traceback.format_exception(extype, val, t)
77 for s in fmt:
78 if (diffable):
79 s = removeFileRefs(s)
80 if (out == None):
81 print s
82 else:
83 print >> out, s
84