File: log.py

package info (click to toggle)
clearsilver 0.10.5-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,304 kB
  • sloc: ansic: 24,586; python: 4,233; sh: 2,502; cs: 1,429; ruby: 819; java: 735; makefile: 589; perl: 120; lisp: 34; sql: 21
file content (130 lines) | stat: -rwxr-xr-x 2,416 bytes parent folder | download | duplicates (10)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/neo/opt/bin/python

# log.py

import sys, time, string

DEV = "development"
DEV_UPDATE = "update queries"
DEV_SELECT = "select queries"
DEV_REPORT = "report log"

LOGGING_STATUS = {
   DEV : 1,
   DEV_UPDATE : 0,
   DEV_SELECT : 0,
   DEV_REPORT : 0}

tstart = 0

def dlog(when,astr):
    global LOGGING_STATUS
    try:
        if LOGGING_STATUS[when]:
            log(astr)
    except KeyError:
        pass

def tlog(astr):
    global tstart
    t = time.time()
    if tstart == 0:
        tstart = t
    time_stamp = "%5.5f" % (t-tstart)
    if len(astr):
        if astr[-1] == "\n":
            sys.stderr.write("[%s] %s" % (time_stamp, astr))
        else:
            sys.stderr.write("[%s] %s\n" % (time_stamp, astr))

def orig_log(astr):
    if len(astr) > 1024:
        astr = astr[:1024]

    t = time.time()
    time_stamp = time.strftime("%m/%d %T", time.localtime(t))
    if len(astr):
        if astr[-1] == "\n":
            sys.stderr.write("[%s] %s" % (time_stamp, astr))
        else:
            sys.stderr.write("[%s] %s\n" % (time_stamp, astr))
    # sys.stderr.flush()


#----------------------------------------------------------------------
# static functions

_gDebug = 0
_gFileDebug = 0

kBLACK = 0
kRED = 1
kGREEN = 2
kYELLOW = 3
kBLUE = 4
kMAGENTA = 5
kCYAN = 6
kWHITE = 7
kBRIGHT = 8

def ansicolor (str, fgcolor = None, bgcolor = None):
  o = ""
  if fgcolor:
    if fgcolor & kBRIGHT:
      bright = ';1'
    else:
      bright = ''
    o = o + '%c[3%d%sm' % (chr(27), fgcolor & 0x7, bright)
  if bgcolor:
    o = o + '%c[4%dm' % (chr(27), bgcolor)
  o = o + str
  if fgcolor or bgcolor:
    o = o + '%c[0m' % (chr(27))
  return o


def _log(*args):
  t = time.time()

  log_line = ""

  log_line = log_line + "[" + time.strftime("%m/%d %T", time.localtime(t)) + "] "

  l = []
  for arg in args:
    l.append(str(arg))
  log_line = log_line + string.join(l, " ") + "\n"

  sys.stderr.write(log_line)

def warn(*args):
  apply(_log, args)

def warnred(*args):
  args = tuple ([""] + list(args) + [""])
  apply(_log, args)

def log(*args):
  apply(_log, args)

def logred(*args):
  if _gDebug>=1: 
    args = tuple ([""] + list(args) + [""])
    apply(_log, args)

def debug(*args):
  if _gDebug>=2: apply(_log, args)


def debugfull():
  global _gDebug
  _gDebug = 2

def debugon():
  global _gDebug
  _gDebug = 1

def debugoff():
  global _gDebug
  _gDebug = 0