]> code.delx.au - pymsnt/blob - src/debug.py
Reimporting (0.9.5)
[pymsnt] / src / debug.py
1 # Copyright 2004 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
3
4 import os
5 import sys
6 import config
7 import utils
8 import time
9
10 """ A simple logging module. Use as follows.
11
12 > import debug
13 > debug.log("text string")
14
15 If debugging is enabled then the data will be dumped to a file
16 or the screen (whichever the user chose)
17 """
18
19
20 file = None
21 rollingStack = None
22 if(config.debugSmart):
23 rollingStack = utils.RollingStack(100)
24
25
26 def reopenFile(first=False):
27 global file
28 if(file or first):
29 if(file): file.close()
30
31 try:
32 file = open(utils.doPath(config.debugLog), 'a')
33 except:
34 print "Error opening debug log file. Exiting..."
35 os.abort()
36
37
38 def flushDebugSmart():
39 global rollingStack
40 if(config.debugSmart):
41 file.write(rollingStack.grabAll())
42 rollingStack.flush()
43 file.flush()
44
45
46 if(config.debugOn):
47 if(len(config.debugLog) > 0):
48 reopenFile(True)
49 def log(data, wtime=True):
50 text = ""
51 if(wtime):
52 text += time.strftime("%D - %H:%M:%S - ")
53 text += utils.latin1(data) + "\n"
54 if(config.debugSmart):
55 rollingStack.push(text)
56 else:
57 file.write(text)
58 file.flush()
59 else:
60 def log(data, wtime=True):
61 if(wtime):
62 print time.strftime("%D - %H:%M:%S - "),
63 print utils.latin1(data)
64 sys.stdout.flush()
65 log("Debug logging enabled.")
66 else:
67 def log(data):
68 pass
69
70
71 def write(data):
72 # So that I can pass this module to twisted.python.failure.Failure.printDetailedTraceback() as a file
73 data = data.rstrip()
74 log(data)
75
76