1#!/neo/opt/bin/python
2
3# log.py
4
5import sys, time, string
6
7DEV = "development"
8DEV_UPDATE = "update queries"
9DEV_SELECT = "select queries"
10DEV_REPORT = "report log"
11
12LOGGING_STATUS = {
13   DEV : 1,
14   DEV_UPDATE : 0,
15   DEV_SELECT : 0,
16   DEV_REPORT : 0}
17
18tstart = 0
19
20def dlog(when,astr):
21    global LOGGING_STATUS
22    try:
23        if LOGGING_STATUS[when]:
24            log(astr)
25    except KeyError:
26        pass
27
28def tlog(astr):
29    global tstart
30    t = time.time()
31    if tstart == 0:
32        tstart = t
33    time_stamp = "%5.5f" % (t-tstart)
34    if len(astr):
35        if astr[-1] == "\n":
36            sys.stderr.write("[%s] %s" % (time_stamp, astr))
37        else:
38            sys.stderr.write("[%s] %s\n" % (time_stamp, astr))
39
40def orig_log(astr):
41    if len(astr) > 1024:
42        astr = astr[:1024]
43
44    t = time.time()
45    time_stamp = time.strftime("%m/%d %T", time.localtime(t))
46    if len(astr):
47        if astr[-1] == "\n":
48            sys.stderr.write("[%s] %s" % (time_stamp, astr))
49        else:
50            sys.stderr.write("[%s] %s\n" % (time_stamp, astr))
51    # sys.stderr.flush()
52
53
54#----------------------------------------------------------------------
55# static functions
56
57_gDebug = 0
58_gFileDebug = 0
59
60kBLACK = 0
61kRED = 1
62kGREEN = 2
63kYELLOW = 3
64kBLUE = 4
65kMAGENTA = 5
66kCYAN = 6
67kWHITE = 7
68kBRIGHT = 8
69
70def ansicolor (str, fgcolor = None, bgcolor = None):
71  o = ""
72  if fgcolor:
73    if fgcolor & kBRIGHT:
74      bright = ';1'
75    else:
76      bright = ''
77    o = o + '%c[3%d%sm' % (chr(27), fgcolor & 0x7, bright)
78  if bgcolor:
79    o = o + '%c[4%dm' % (chr(27), bgcolor)
80  o = o + str
81  if fgcolor or bgcolor:
82    o = o + '%c[0m' % (chr(27))
83  return o
84
85
86def _log(*args):
87  t = time.time()
88
89  log_line = ""
90
91  log_line = log_line + "[" + time.strftime("%m/%d %T", time.localtime(t)) + "] "
92
93  l = []
94  for arg in args:
95    l.append(str(arg))
96  log_line = log_line + string.join(l, " ") + "\n"
97
98  sys.stderr.write(log_line)
99
100def warn(*args):
101  apply(_log, args)
102
103def warnred(*args):
104  args = tuple ([""] + list(args) + [""])
105  apply(_log, args)
106
107def log(*args):
108  apply(_log, args)
109
110def logred(*args):
111  if _gDebug>=1:
112    args = tuple ([""] + list(args) + [""])
113    apply(_log, args)
114
115def debug(*args):
116  if _gDebug>=2: apply(_log, args)
117
118
119def debugfull():
120  global _gDebug
121  _gDebug = 2
122
123def debugon():
124  global _gDebug
125  _gDebug = 1
126
127def debugoff():
128  global _gDebug
129  _gDebug = 0
130
131