Home | History | Annotate | Download | only in base
      1 #!/neo/opt/bin/python
      2 
      3 # log.py
      4 
      5 import sys, time, string
      6 
      7 DEV = "development"
      8 DEV_UPDATE = "update queries"
      9 DEV_SELECT = "select queries"
     10 DEV_REPORT = "report log"
     11 
     12 LOGGING_STATUS = {
     13    DEV : 1,
     14    DEV_UPDATE : 0,
     15    DEV_SELECT : 0,
     16    DEV_REPORT : 0}
     17 
     18 tstart = 0
     19 
     20 def dlog(when,astr):
     21     global LOGGING_STATUS
     22     try:
     23         if LOGGING_STATUS[when]:
     24             log(astr)
     25     except KeyError:
     26         pass
     27 
     28 def 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 
     40 def 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 
     60 kBLACK = 0
     61 kRED = 1
     62 kGREEN = 2
     63 kYELLOW = 3
     64 kBLUE = 4
     65 kMAGENTA = 5
     66 kCYAN = 6
     67 kWHITE = 7
     68 kBRIGHT = 8
     69 
     70 def 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 
     86 def _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 
    100 def warn(*args):
    101   apply(_log, args)
    102 
    103 def warnred(*args):
    104   args = tuple (["[31m"] + list(args) + ["[0m"])
    105   apply(_log, args)
    106 
    107 def log(*args):
    108   apply(_log, args)
    109 
    110 def logred(*args):
    111   if _gDebug>=1: 
    112     args = tuple (["[31m"] + list(args) + ["[0m"])
    113     apply(_log, args)
    114 
    115 def debug(*args):
    116   if _gDebug>=2: apply(_log, args)
    117 
    118 
    119 def debugfull():
    120   global _gDebug
    121   _gDebug = 2
    122 
    123 def debugon():
    124   global _gDebug
    125   _gDebug = 1
    126 
    127 def debugoff():
    128   global _gDebug
    129   _gDebug = 0
    130 
    131