3 # Thomas Nagy, 2005 (ita)
6 import os
, re
, logging
, traceback
, sys
7 from Constants
import *
19 'BLUE' :'\x1b[01;34m',
22 'cursor_on' :'\x1b[?25h',
23 'cursor_off' :'\x1b[?25l',
27 term
= os
.environ
.get('TERM', 'dumb')
28 if not term
in ['dumb', 'emacs']:
30 got_tty
= sys
.stderr
.isatty() or (sys
.platform
== 'win32' and term
in ['xterm', 'msys'])
31 except AttributeError:
36 if not got_tty
or 'NOCOLOR' in os
.environ
:
37 colors_lst
['USE'] = False
40 #if sys.platform == 'win32':
41 # colors_lst['USE'] = True
44 if not colors_lst
['USE']: return ''
45 return colors_lst
.get(cl
, '')
48 def __getattr__(self
, a
):
50 def __call__(self
, a
):
55 re_log
= re
.compile(r
'(\w+): (.*)', re
.M
)
56 class log_filter(logging
.Filter
):
57 def __init__(self
, name
=None):
60 def filter(self
, rec
):
62 rec
.c2
= colors
.NORMAL
64 if rec
.levelno
>= logging
.INFO
:
65 if rec
.levelno
>= logging
.ERROR
:
67 elif rec
.levelno
>= logging
.WARNING
:
68 rec
.c1
= colors
.YELLOW
74 m
= re_log
.match(rec
.msg
)
76 zone
= rec
.zone
= m
.group(1)
80 return getattr(rec
, 'zone', '') in zones
or '*' in zones
85 class formatter(logging
.Formatter
):
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
:
92 return '%s%s%s' % (rec
.c1
, rec
.msg
.decode('utf-8'), rec
.c2
)
94 return rec
.c1
+rec
.msg
+rec
.c2
95 return logging
.Formatter
.format(self
, rec
)
100 k
[0] = k
[0].replace('\n', ' ')
101 logging
.debug(*k
, **kw
)
104 logging
.error(*k
, **kw
)
106 if isinstance(k
[0], Utils
.WafError
):
109 st
= traceback
.extract_stack()
113 for filename
, lineno
, name
, line
in st
:
114 buf
.append(' File "%s", line %d, in %s' % (filename
, lineno
, name
))
116 buf
.append(' %s' % line
.strip())
117 if buf
: logging
.error("\n".join(buf
))
123 log
= logging
.getLogger()
126 hdlr
= logging
.StreamHandler()
127 hdlr
.setFormatter(formatter())
129 log
.addFilter(log_filter())
130 log
.setLevel(logging
.DEBUG
)
132 # may be initialized more than once