1 #----------------------------------------------------------------------------
3 # Purpose: Utilities to help with logging
9 # Copyright: (c) 2005 ActiveGrid, Inc.
10 # License: wxWindows License
11 #----------------------------------------------------------------------------
18 from activegrid
.util
.lang
import *
20 LEVEL_FATAL
= logging
.FATAL
21 LEVEL_ERROR
= logging
.ERROR
22 LEVEL_WARN
= logging
.WARN
23 LEVEL_INFO
= logging
.INFO
24 LEVEL_DEBUG
= logging
.DEBUG
27 TEST_MODE_DETERMINISTIC
= 1
28 TEST_MODE_NON_DETERMINISTIC
= 2
31 agTestMode
= TEST_MODE_NONE
33 def setTestMode(mode
):
41 def testMode(normalObj
, testObj
=None):
42 if getTestMode() > TEST_MODE_NONE
:
46 pythonFileRefPattern
= asString(r
'(?<=File ")[^"]*(#[^#]*")(, line )[0-9]*')
47 phpFileRefPattern
= asString(r
'( in ).*#([^#]*#[^ ]*)(?= on line )')
48 pathSepPattern
= os
.sep
49 if (pathSepPattern
== "\\"):
50 pathSepPattern
= "\\\\"
51 pythonFileRefPattern
= pythonFileRefPattern
.replace("#", pathSepPattern
)
52 pythonFileRefPattern
= re
.compile(pythonFileRefPattern
)
53 phpFileRefPattern
= phpFileRefPattern
.replace("#", pathSepPattern
)
54 phpFileRefPattern
= re
.compile(phpFileRefPattern
)
56 def removeFileRefs(str):
57 str = pythonFileRefPattern
.sub(_fileNameReplacement
, str)
58 str = phpFileRefPattern
.sub(_fileNameReplacementPHP
, str)
61 def removePHPFileRefs(str):
62 str = phpFileRefPattern
.sub(_fileNameReplacementPHP
, str)
65 def _fileNameReplacement(match
):
66 return "...%s" % match
.group(1).replace(os
.sep
, "/")
68 def _fileNameReplacementPHP(match
):
69 return "%s...%s" % (match
.group(1), match
.group(2).replace(os
.sep
, "/"))
72 extype
, val
, tb
= sys
.exc_info()
74 for s
in traceback
.format_tb(tb
):
78 def reportException(out
=None, stacktrace
=False, diffable
=False, exception
=None):
79 if (True): # exception == None):
80 extype
, val
, t
= sys
.exc_info()
82 extype
= type(exception
)
85 e
,v
,t
= sys
.exc_info()
87 exstr
= removeFileRefs(str(val
))
91 print "Got Exception = %s: %s" % (extype
, exstr
)
93 print >> out
, "Got Exception = %s: %s" % (extype
, exstr
)
95 fmt
= traceback
.format_exception(extype
, val
, t
)