torture: Fix a buffer overrun
[Samba.git] / buildtools / wafadmin / Logs.py
blobc160b3773664aa8b06db9a5a351d852a41b50ada
1 #!/usr/bin/env python
2 # encoding: utf-8
3 # Thomas Nagy, 2005 (ita)
5 import ansiterm
6 import os, re, logging, traceback, sys
7 from Constants import *
9 zones = ''
10 verbose = 0
12 colors_lst = {
13 'USE' : True,
14 'BOLD' :'\x1b[01;1m',
15 'RED' :'\x1b[01;31m',
16 'GREEN' :'\x1b[32m',
17 'YELLOW':'\x1b[33m',
18 'PINK' :'\x1b[35m',
19 'BLUE' :'\x1b[01;34m',
20 'CYAN' :'\x1b[36m',
21 'NORMAL':'\x1b[0m',
22 'cursor_on' :'\x1b[?25h',
23 'cursor_off' :'\x1b[?25l',
26 got_tty = False
27 term = os.environ.get('TERM', 'dumb')
28 if not term in ['dumb', 'emacs']:
29 try:
30 got_tty = sys.stderr.isatty() or (sys.platform == 'win32' and term in ['xterm', 'msys'])
31 except AttributeError:
32 pass
34 import Utils
36 if not got_tty or 'NOCOLOR' in os.environ:
37 colors_lst['USE'] = False
39 # test
40 #if sys.platform == 'win32':
41 # colors_lst['USE'] = True
43 def get_color(cl):
44 if not colors_lst['USE']: return ''
45 return colors_lst.get(cl, '')
47 class foo(object):
48 def __getattr__(self, a):
49 return get_color(a)
50 def __call__(self, a):
51 return get_color(a)
53 colors = foo()
55 re_log = re.compile(r'(\w+): (.*)', re.M)
56 class log_filter(logging.Filter):
57 def __init__(self, name=None):
58 pass
60 def filter(self, rec):
61 rec.c1 = colors.PINK
62 rec.c2 = colors.NORMAL
63 rec.zone = rec.module
64 if rec.levelno >= logging.INFO:
65 if rec.levelno >= logging.ERROR:
66 rec.c1 = colors.RED
67 elif rec.levelno >= logging.WARNING:
68 rec.c1 = colors.YELLOW
69 else:
70 rec.c1 = colors.GREEN
71 return True
73 zone = ''
74 m = re_log.match(rec.msg)
75 if m:
76 zone = rec.zone = m.group(1)
77 rec.msg = m.group(2)
79 if zones:
80 return getattr(rec, 'zone', '') in zones or '*' in zones
81 elif not verbose > 2:
82 return False
83 return True
85 class formatter(logging.Formatter):
86 def __init__(self):
87 logging.Formatter.__init__(self, LOG_FORMAT, HOUR_FORMAT)
89 def format(self, rec):
90 if rec.levelno >= logging.WARNING or rec.levelno == logging.INFO:
91 try:
92 return '%s%s%s' % (rec.c1, rec.msg.decode('utf-8'), rec.c2)
93 except:
94 return rec.c1+rec.msg+rec.c2
95 return logging.Formatter.format(self, rec)
97 def debug(*k, **kw):
98 if verbose:
99 k = list(k)
100 k[0] = k[0].replace('\n', ' ')
101 logging.debug(*k, **kw)
103 def error(*k, **kw):
104 logging.error(*k, **kw)
105 if verbose > 1:
106 if isinstance(k[0], Utils.WafError):
107 st = k[0].stack
108 else:
109 st = traceback.extract_stack()
110 if st:
111 st = st[:-1]
112 buf = []
113 for filename, lineno, name, line in st:
114 buf.append(' File "%s", line %d, in %s' % (filename, lineno, name))
115 if line:
116 buf.append(' %s' % line.strip())
117 if buf: logging.error("\n".join(buf))
119 warn = logging.warn
120 info = logging.info
122 def init_log():
123 log = logging.getLogger()
124 log.handlers = []
125 log.filters = []
126 hdlr = logging.StreamHandler()
127 hdlr.setFormatter(formatter())
128 log.addHandler(hdlr)
129 log.addFilter(log_filter())
130 log.setLevel(logging.DEBUG)
132 # may be initialized more than once
133 init_log()