-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.py
66 lines (52 loc) · 1.57 KB
/
logging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import time, os, cPickle
from datetime import datetime
from debug import DefaultDict, Debug
Logging = DefaultDict(0)
Logging.set('USERLOG', True)
logdir = 'logs'
logbuffer = None
logfilename = ''
def getLogFilename():
now = datetime.now()
return '%s/log-%d-%02d-%02d.txt' % (logdir,now.year,now.month,now.day)
def openlogbuffer():
global logbuffer, logfilename
if logbuffer != None:
closelogbuffer()
logfilename = getLogFilename()
logbuffer = open(logfilename, 'a')
def getlogbuffer():
# If we've passed to the next day, switch to the next log file
if logbuffer == None or logfilename != getLogFilename():
openlogbuffer()
return logbuffer
def closelogbuffer():
global logbuffer
logbuffer.close()
logbuffer = None
def errordump(errorobj):
errornum = 0
while True:
filename = logdir + '/errordump_' + str(errornum)
if not os.path.exists(filename):
break
errornum += 1
f = open(filename, 'wb')
cPickle.dump(errorobj, f)
f.close()
return filename
def log(msg, category='', component='', closeafterwrite=False, buffer=None):
if not Logging(category) and not Debug("VERBOSE"): return
if buffer == None:
buffer = getlogbuffer()
fullmsg = component
if component and component != '':
fullmsg += ': '
fullmsg += msg
fullmsg = time.strftime('%c') + ": " + fullmsg
if Debug("PRINT_LOG"):
print fullmsg
buffer.write(fullmsg + '\n')
buffer.flush()
if closeafterwrite:
closelogbuffer(buffer)