]>
Commit | Line | Data |
---|---|---|
2eeaec19 RD |
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 | import logging | |
18 | from activegrid.util.lang import * | |
19 | ||
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 | |
25 | ||
26 | TEST_MODE_NONE = 0 | |
27 | TEST_MODE_DETERMINISTIC = 1 | |
28 | TEST_MODE_NON_DETERMINISTIC = 2 | |
29 | ||
30 | global agTestMode | |
31 | agTestMode = TEST_MODE_NONE | |
32 | ||
33 | def setTestMode(mode): | |
34 | global agTestMode | |
35 | agTestMode = mode | |
36 | ||
37 | def getTestMode(): | |
38 | global agTestMode | |
39 | return agTestMode | |
40 | ||
41 | def testMode(normalObj, testObj=None): | |
42 | if getTestMode() > TEST_MODE_NONE: | |
43 | return testObj | |
44 | return normalObj | |
45 | ||
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) | |
55 | ||
56 | def removeFileRefs(str): | |
57 | str = pythonFileRefPattern.sub(_fileNameReplacement, str) | |
58 | str = phpFileRefPattern.sub(_fileNameReplacementPHP, str) | |
59 | return str | |
60 | ||
61 | def removePHPFileRefs(str): | |
62 | str = phpFileRefPattern.sub(_fileNameReplacementPHP, str) | |
63 | return str | |
64 | ||
65 | def _fileNameReplacement(match): | |
66 | return "...%s" % match.group(1).replace(os.sep, "/") | |
67 | ||
68 | def _fileNameReplacementPHP(match): | |
69 | return "%s...%s" % (match.group(1), match.group(2).replace(os.sep, "/")) | |
70 | ||
71 | def 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 | ||
78 | def 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 |