]> git.saurik.com Git - wxWidgets.git/blame_incremental - wxPython/samples/ide/activegrid/util/aglogging.py
Fix "warning: operation on 'y' may be undefined".
[wxWidgets.git] / wxPython / samples / ide / activegrid / util / aglogging.py
... / ...
CommitLineData
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
13import sys
14import os
15import re
16import traceback
17import logging
18from activegrid.util.lang import *
19
20LEVEL_FATAL = logging.FATAL
21LEVEL_ERROR = logging.ERROR
22LEVEL_WARN = logging.WARN
23LEVEL_INFO = logging.INFO
24LEVEL_DEBUG = logging.DEBUG
25
26TEST_MODE_NONE = 0
27TEST_MODE_DETERMINISTIC = 1
28TEST_MODE_NON_DETERMINISTIC = 2
29
30global agTestMode
31agTestMode = TEST_MODE_NONE
32
33def setTestMode(mode):
34 global agTestMode
35 agTestMode = mode
36
37def getTestMode():
38 global agTestMode
39 return agTestMode
40
41def testMode(normalObj, testObj=None):
42 if getTestMode() > TEST_MODE_NONE:
43 return testObj
44 return normalObj
45
46pythonFileRefPattern = asString(r'(?<=File ")[^"]*(#[^#]*")(, line )[0-9]*')
47phpFileRefPattern = asString(r'( in ).*#([^#]*#[^ ]*)(?= on line )')
48pathSepPattern = os.sep
49if (pathSepPattern == "\\"):
50 pathSepPattern = "\\\\"
51pythonFileRefPattern = pythonFileRefPattern.replace("#", pathSepPattern)
52pythonFileRefPattern = re.compile(pythonFileRefPattern)
53phpFileRefPattern = phpFileRefPattern.replace("#", pathSepPattern)
54phpFileRefPattern = re.compile(phpFileRefPattern)
55
56def removeFileRefs(str):
57 str = pythonFileRefPattern.sub(_fileNameReplacement, str)
58 str = phpFileRefPattern.sub(_fileNameReplacementPHP, str)
59 return str
60
61def removePHPFileRefs(str):
62 str = phpFileRefPattern.sub(_fileNameReplacementPHP, str)
63 return str
64
65def _fileNameReplacement(match):
66 return "...%s" % match.group(1).replace(os.sep, "/")
67
68def _fileNameReplacementPHP(match):
69 return "%s...%s" % (match.group(1), match.group(2).replace(os.sep, "/"))
70
71def getTraceback():
72 extype, val, tb = sys.exc_info()
73 tbs = "\n"
74 for s in traceback.format_tb(tb):
75 tbs += s
76 return tbs
77
78def reportException(out=None, stacktrace=False, diffable=False, exception=None):
79 if (True): # exception == None):
80 extype, val, t = sys.exc_info()
81 else:
82 extype = type(exception)
83 val = exception
84 if (stacktrace):
85 e,v,t = sys.exc_info()
86 if (diffable):
87 exstr = removeFileRefs(str(val))
88 else:
89 exstr = str(val)
90 if (out == None):
91 print "Got Exception = %s: %s" % (extype, exstr)
92 else:
93 print >> out, "Got Exception = %s: %s" % (extype, exstr)
94 if (stacktrace):
95 fmt = traceback.format_exception(extype, val, t)
96 for s in fmt:
97 if (diffable):
98 s = removeFileRefs(s)
99 if (out == None):
100 print s
101 else:
102 print >> out, s
103