[0fa325b] | 1 | #! /usr/bin/env python |
---|
| 2 | # encoding: utf-8 |
---|
| 3 | # WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file |
---|
| 4 | |
---|
[904702d] | 5 | import os,re,traceback,sys,types |
---|
| 6 | from waflib import Utils,ansiterm |
---|
| 7 | if not os.environ.get('NOSYNC',False): |
---|
| 8 | if sys.stdout.isatty()and id(sys.stdout)==id(sys.__stdout__): |
---|
| 9 | sys.stdout=ansiterm.AnsiTerm(sys.stdout) |
---|
| 10 | if sys.stderr.isatty()and id(sys.stderr)==id(sys.__stderr__): |
---|
| 11 | sys.stderr=ansiterm.AnsiTerm(sys.stderr) |
---|
[0fa325b] | 12 | import logging |
---|
| 13 | LOG_FORMAT="%(asctime)s %(c1)s%(zone)s%(c2)s %(message)s" |
---|
| 14 | HOUR_FORMAT="%H:%M:%S" |
---|
| 15 | zones='' |
---|
| 16 | verbose=0 |
---|
| 17 | colors_lst={'USE':True,'BOLD':'\x1b[01;1m','RED':'\x1b[01;31m','GREEN':'\x1b[32m','YELLOW':'\x1b[33m','PINK':'\x1b[35m','BLUE':'\x1b[01;34m','CYAN':'\x1b[36m','NORMAL':'\x1b[0m','cursor_on':'\x1b[?25h','cursor_off':'\x1b[?25l',} |
---|
[904702d] | 18 | indicator='\r\x1b[K%s%s%s' |
---|
| 19 | def enable_colors(use): |
---|
| 20 | if use==1: |
---|
| 21 | if not(sys.stderr.isatty()or sys.stdout.isatty()): |
---|
| 22 | use=0 |
---|
| 23 | if Utils.is_win32: |
---|
| 24 | term=os.environ.get('TERM','') |
---|
[0fa325b] | 25 | else: |
---|
[904702d] | 26 | term=os.environ.get('TERM','dumb') |
---|
| 27 | if term in('dumb','emacs'): |
---|
| 28 | use=0 |
---|
| 29 | if use>=1: |
---|
| 30 | os.environ['TERM']='vt100' |
---|
| 31 | colors_lst['USE']=use |
---|
| 32 | try: |
---|
| 33 | get_term_cols=ansiterm.get_term_cols |
---|
| 34 | except AttributeError: |
---|
| 35 | def get_term_cols(): |
---|
| 36 | return 80 |
---|
[0fa325b] | 37 | get_term_cols.__doc__=""" |
---|
| 38 | Get the console width in characters. |
---|
| 39 | |
---|
| 40 | :return: the number of characters per line |
---|
| 41 | :rtype: int |
---|
| 42 | """ |
---|
| 43 | def get_color(cl): |
---|
| 44 | if not colors_lst['USE']:return'' |
---|
| 45 | return colors_lst.get(cl,'') |
---|
| 46 | class color_dict(object): |
---|
| 47 | def __getattr__(self,a): |
---|
| 48 | return get_color(a) |
---|
| 49 | def __call__(self,a): |
---|
| 50 | return get_color(a) |
---|
| 51 | colors=color_dict() |
---|
| 52 | re_log=re.compile(r'(\w+): (.*)',re.M) |
---|
| 53 | class log_filter(logging.Filter): |
---|
| 54 | def __init__(self,name=None): |
---|
| 55 | pass |
---|
| 56 | def filter(self,rec): |
---|
| 57 | rec.zone=rec.module |
---|
| 58 | if rec.levelno>=logging.INFO: |
---|
| 59 | return True |
---|
| 60 | m=re_log.match(rec.msg) |
---|
| 61 | if m: |
---|
| 62 | rec.zone=m.group(1) |
---|
| 63 | rec.msg=m.group(2) |
---|
| 64 | if zones: |
---|
| 65 | return getattr(rec,'zone','')in zones or'*'in zones |
---|
| 66 | elif not verbose>2: |
---|
| 67 | return False |
---|
| 68 | return True |
---|
[904702d] | 69 | class log_handler(logging.StreamHandler): |
---|
| 70 | def emit(self,record): |
---|
| 71 | try: |
---|
| 72 | try: |
---|
| 73 | self.stream=record.stream |
---|
| 74 | except AttributeError: |
---|
| 75 | if record.levelno>=logging.WARNING: |
---|
| 76 | record.stream=self.stream=sys.stderr |
---|
| 77 | else: |
---|
| 78 | record.stream=self.stream=sys.stdout |
---|
| 79 | self.emit_override(record) |
---|
| 80 | self.flush() |
---|
| 81 | except(KeyboardInterrupt,SystemExit): |
---|
| 82 | raise |
---|
| 83 | except: |
---|
| 84 | self.handleError(record) |
---|
| 85 | def emit_override(self,record,**kw): |
---|
| 86 | self.terminator=getattr(record,'terminator','\n') |
---|
| 87 | stream=self.stream |
---|
| 88 | if hasattr(types,"UnicodeType"): |
---|
| 89 | msg=self.formatter.format(record) |
---|
| 90 | fs='%s'+self.terminator |
---|
| 91 | try: |
---|
| 92 | if(isinstance(msg,unicode)and getattr(stream,'encoding',None)): |
---|
| 93 | fs=fs.decode(stream.encoding) |
---|
| 94 | try: |
---|
| 95 | stream.write(fs%msg) |
---|
| 96 | except UnicodeEncodeError: |
---|
| 97 | stream.write((fs%msg).encode(stream.encoding)) |
---|
| 98 | else: |
---|
| 99 | stream.write(fs%msg) |
---|
| 100 | except UnicodeError: |
---|
| 101 | stream.write((fs%msg).encode("UTF-8")) |
---|
| 102 | else: |
---|
| 103 | logging.StreamHandler.emit(self,record) |
---|
[0fa325b] | 104 | class formatter(logging.Formatter): |
---|
| 105 | def __init__(self): |
---|
| 106 | logging.Formatter.__init__(self,LOG_FORMAT,HOUR_FORMAT) |
---|
| 107 | def format(self,rec): |
---|
[904702d] | 108 | try: |
---|
| 109 | msg=rec.msg.decode('utf-8') |
---|
| 110 | except Exception: |
---|
| 111 | msg=rec.msg |
---|
| 112 | use=colors_lst['USE'] |
---|
| 113 | if(use==1 and rec.stream.isatty())or use==2: |
---|
| 114 | c1=getattr(rec,'c1',None) |
---|
| 115 | if c1 is None: |
---|
| 116 | c1='' |
---|
| 117 | if rec.levelno>=logging.ERROR: |
---|
| 118 | c1=colors.RED |
---|
| 119 | elif rec.levelno>=logging.WARNING: |
---|
| 120 | c1=colors.YELLOW |
---|
| 121 | elif rec.levelno>=logging.INFO: |
---|
| 122 | c1=colors.GREEN |
---|
| 123 | c2=getattr(rec,'c2',colors.NORMAL) |
---|
| 124 | msg='%s%s%s'%(c1,msg,c2) |
---|
| 125 | else: |
---|
| 126 | msg=msg.replace('\r','\n') |
---|
| 127 | msg=re.sub(r'\x1B\[(K|.*?(m|h|l))','',msg) |
---|
| 128 | if rec.levelno>=logging.INFO: |
---|
| 129 | return msg |
---|
| 130 | rec.msg=msg |
---|
| 131 | rec.c1=colors.PINK |
---|
| 132 | rec.c2=colors.NORMAL |
---|
[0fa325b] | 133 | return logging.Formatter.format(self,rec) |
---|
| 134 | log=None |
---|
| 135 | def debug(*k,**kw): |
---|
| 136 | if verbose: |
---|
| 137 | k=list(k) |
---|
| 138 | k[0]=k[0].replace('\n',' ') |
---|
| 139 | global log |
---|
| 140 | log.debug(*k,**kw) |
---|
| 141 | def error(*k,**kw): |
---|
| 142 | global log |
---|
| 143 | log.error(*k,**kw) |
---|
| 144 | if verbose>2: |
---|
| 145 | st=traceback.extract_stack() |
---|
| 146 | if st: |
---|
| 147 | st=st[:-1] |
---|
| 148 | buf=[] |
---|
| 149 | for filename,lineno,name,line in st: |
---|
| 150 | buf.append(' File "%s", line %d, in %s'%(filename,lineno,name)) |
---|
| 151 | if line: |
---|
| 152 | buf.append(' %s'%line.strip()) |
---|
| 153 | if buf:log.error("\n".join(buf)) |
---|
| 154 | def warn(*k,**kw): |
---|
| 155 | global log |
---|
| 156 | log.warn(*k,**kw) |
---|
| 157 | def info(*k,**kw): |
---|
| 158 | global log |
---|
| 159 | log.info(*k,**kw) |
---|
| 160 | def init_log(): |
---|
| 161 | global log |
---|
| 162 | log=logging.getLogger('waflib') |
---|
| 163 | log.handlers=[] |
---|
| 164 | log.filters=[] |
---|
[904702d] | 165 | hdlr=log_handler() |
---|
[0fa325b] | 166 | hdlr.setFormatter(formatter()) |
---|
| 167 | log.addHandler(hdlr) |
---|
| 168 | log.addFilter(log_filter()) |
---|
| 169 | log.setLevel(logging.DEBUG) |
---|
| 170 | def make_logger(path,name): |
---|
| 171 | logger=logging.getLogger(name) |
---|
| 172 | hdlr=logging.FileHandler(path,'w') |
---|
| 173 | formatter=logging.Formatter('%(message)s') |
---|
| 174 | hdlr.setFormatter(formatter) |
---|
| 175 | logger.addHandler(hdlr) |
---|
| 176 | logger.setLevel(logging.DEBUG) |
---|
| 177 | return logger |
---|
[904702d] | 178 | def make_mem_logger(name,to_log,size=8192): |
---|
[0fa325b] | 179 | from logging.handlers import MemoryHandler |
---|
| 180 | logger=logging.getLogger(name) |
---|
| 181 | hdlr=MemoryHandler(size,target=to_log) |
---|
| 182 | formatter=logging.Formatter('%(message)s') |
---|
| 183 | hdlr.setFormatter(formatter) |
---|
| 184 | logger.addHandler(hdlr) |
---|
| 185 | logger.memhandler=hdlr |
---|
| 186 | logger.setLevel(logging.DEBUG) |
---|
| 187 | return logger |
---|
[904702d] | 188 | def free_logger(logger): |
---|
| 189 | try: |
---|
| 190 | for x in logger.handlers: |
---|
| 191 | x.close() |
---|
| 192 | logger.removeHandler(x) |
---|
| 193 | except Exception ,e: |
---|
| 194 | pass |
---|
| 195 | def pprint(col,msg,label='',sep='\n'): |
---|
| 196 | info("%s%s%s %s"%(colors(col),msg,colors.NORMAL,label),extra={'terminator':sep}) |
---|