-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDebug.py
More file actions
57 lines (48 loc) · 1.71 KB
/
Debug.py
File metadata and controls
57 lines (48 loc) · 1.71 KB
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
import time
import inspect
import sys,os
import traceback
class Logger(object):
"""docstring for MyLogger"""
def __init__(self, filename, *altfiles):
super(Logger, self).__init__()
altfiles = list(altfiles)
altfiles.insert(0,filename)
for name in altfiles:
try:
name = os.path.expandvars(os.path.expanduser(name))
dir = os.path.dirname(name)
if not os.path.exists(dir):
os.makedirs(dir)
self.logfile = open(name,"a")
print "Writing log to",name
break
except:
pass
else:
print >>sys.stderr,"Unable to open any of these files for logging:", self.altfiles
def __call__(self, *args, **kwargs):
caller = traceback.extract_stack()[-2]
if not args or kwargs:
trace = traceback.format_exc()
if not trace:
return
else:
args=[trace]
print >>self.logfile, time.asctime()
print >>self.logfile, "Log message from file "+caller[0]+" at line "+str(caller[1])+":"
for item in args:
print >>self.logfile, item
for key, value in kwargs.iteritems():
print >>self.logfile, key+":", value
print >>self.logfile, ""
self.logfile.flush()
os.fsync(self.logfile)
def DummyLogger(*args,**kwargs):
pass
import os,sys
log_file_name="Log_"+time.strftime("%Y_%m_%d")+".txt"
if "-d" in sys.argv or "--debug" in sys.argv:
log = Logger("$ENV_WATCHER_DIR/"+log_file_name, "$HOME/.envwatcher/"+log_file_name, "$HOME/EnvWatcher_"+log_file_name)
else:
log = DummyLogger