$search
00001 #!/usr/bin/env python 00002 00003 # log.py 00004 00005 import sys, time, string 00006 00007 DEV = "development" 00008 DEV_UPDATE = "update queries" 00009 DEV_SELECT = "select queries" 00010 DEV_REPORT = "report log" 00011 00012 LOGGING_STATUS = { 00013 DEV : 1, 00014 DEV_UPDATE : 0, 00015 DEV_SELECT : 0, 00016 DEV_REPORT : 0} 00017 00018 tstart = 0 00019 00020 def dlog(when,astr): 00021 global LOGGING_STATUS 00022 try: 00023 if LOGGING_STATUS[when]: 00024 log(astr) 00025 except KeyError: 00026 pass 00027 00028 def tlog(astr): 00029 global tstart 00030 t = time.time() 00031 if tstart == 0: 00032 tstart = t 00033 time_stamp = "%5.5f" % (t-tstart) 00034 if len(astr): 00035 if astr[-1] == "\n": 00036 sys.stderr.write("[%s] %s" % (time_stamp, astr)) 00037 else: 00038 sys.stderr.write("[%s] %s\n" % (time_stamp, astr)) 00039 00040 def orig_log(astr): 00041 if len(astr) > 1024: 00042 astr = astr[:1024] 00043 00044 t = time.time() 00045 time_stamp = time.strftime("%m/%d %T", time.localtime(t)) 00046 if len(astr): 00047 if astr[-1] == "\n": 00048 sys.stderr.write("[%s] %s" % (time_stamp, astr)) 00049 else: 00050 sys.stderr.write("[%s] %s\n" % (time_stamp, astr)) 00051 # sys.stderr.flush() 00052 00053 00054 #---------------------------------------------------------------------- 00055 # static functions 00056 00057 _gDebug = 0 00058 _gFileDebug = 0 00059 00060 kBLACK = 0 00061 kRED = 1 00062 kGREEN = 2 00063 kYELLOW = 3 00064 kBLUE = 4 00065 kMAGENTA = 5 00066 kCYAN = 6 00067 kWHITE = 7 00068 kBRIGHT = 8 00069 00070 def ansicolor (str, fgcolor = None, bgcolor = None): 00071 o = "" 00072 if fgcolor: 00073 if fgcolor & kBRIGHT: 00074 bright = ';1' 00075 else: 00076 bright = '' 00077 o = o + '%c[3%d%sm' % (chr(27), fgcolor & 0x7, bright) 00078 if bgcolor: 00079 o = o + '%c[4%dm' % (chr(27), bgcolor) 00080 o = o + str 00081 if fgcolor or bgcolor: 00082 o = o + '%c[0m' % (chr(27)) 00083 return o 00084 00085 00086 def _log(*args): 00087 t = time.time() 00088 00089 log_line = "" 00090 00091 log_line = log_line + "[" + time.strftime("%m/%d %T", time.localtime(t)) + "] " 00092 00093 l = [] 00094 for arg in args: 00095 l.append(str(arg)) 00096 log_line = log_line + string.join(l, " ") + "\n" 00097 00098 sys.stderr.write(log_line) 00099 00100 def warn(*args): 00101 apply(_log, args) 00102 00103 def warnred(*args): 00104 args = tuple (["[31m"] + list(args) + ["[0m"]) 00105 apply(_log, args) 00106 00107 def log(*args): 00108 if _gDebug>=1: 00109 apply(_log, args) 00110 00111 def logred(*args): 00112 if _gDebug>=1: 00113 args = tuple (["[31m"] + list(args) + ["[0m"]) 00114 apply(_log, args) 00115 00116 def debug(*args): 00117 if _gDebug>=2: apply(_log, args) 00118 00119 00120 def debugfull(): 00121 global _gDebug 00122 _gDebug = 2 00123 00124 def debugon(): 00125 global _gDebug 00126 _gDebug = 1 00127 00128 def debugoff(): 00129 global _gDebug 00130 _gDebug = 0 00131