Chris@87
|
1 # Colored log, requires Python 2.3 or up.
|
Chris@87
|
2 from __future__ import division, absolute_import, print_function
|
Chris@87
|
3
|
Chris@87
|
4 import sys
|
Chris@87
|
5 from distutils.log import *
|
Chris@87
|
6 from distutils.log import Log as old_Log
|
Chris@87
|
7 from distutils.log import _global_log
|
Chris@87
|
8
|
Chris@87
|
9 if sys.version_info[0] < 3:
|
Chris@87
|
10 from .misc_util import (red_text, default_text, cyan_text, green_text,
|
Chris@87
|
11 is_sequence, is_string)
|
Chris@87
|
12 else:
|
Chris@87
|
13 from numpy.distutils.misc_util import (red_text, default_text, cyan_text,
|
Chris@87
|
14 green_text, is_sequence, is_string)
|
Chris@87
|
15
|
Chris@87
|
16
|
Chris@87
|
17 def _fix_args(args,flag=1):
|
Chris@87
|
18 if is_string(args):
|
Chris@87
|
19 return args.replace('%', '%%')
|
Chris@87
|
20 if flag and is_sequence(args):
|
Chris@87
|
21 return tuple([_fix_args(a, flag=0) for a in args])
|
Chris@87
|
22 return args
|
Chris@87
|
23
|
Chris@87
|
24
|
Chris@87
|
25 class Log(old_Log):
|
Chris@87
|
26 def _log(self, level, msg, args):
|
Chris@87
|
27 if level >= self.threshold:
|
Chris@87
|
28 if args:
|
Chris@87
|
29 msg = msg % _fix_args(args)
|
Chris@87
|
30 if 0:
|
Chris@87
|
31 if msg.startswith('copying ') and msg.find(' -> ') != -1:
|
Chris@87
|
32 return
|
Chris@87
|
33 if msg.startswith('byte-compiling '):
|
Chris@87
|
34 return
|
Chris@87
|
35 print(_global_color_map[level](msg))
|
Chris@87
|
36 sys.stdout.flush()
|
Chris@87
|
37
|
Chris@87
|
38 def good(self, msg, *args):
|
Chris@87
|
39 """
|
Chris@87
|
40 If we log WARN messages, log this message as a 'nice' anti-warn
|
Chris@87
|
41 message.
|
Chris@87
|
42
|
Chris@87
|
43 """
|
Chris@87
|
44 if WARN >= self.threshold:
|
Chris@87
|
45 if args:
|
Chris@87
|
46 print(green_text(msg % _fix_args(args)))
|
Chris@87
|
47 else:
|
Chris@87
|
48 print(green_text(msg))
|
Chris@87
|
49 sys.stdout.flush()
|
Chris@87
|
50
|
Chris@87
|
51
|
Chris@87
|
52 _global_log.__class__ = Log
|
Chris@87
|
53
|
Chris@87
|
54 good = _global_log.good
|
Chris@87
|
55
|
Chris@87
|
56 def set_threshold(level, force=False):
|
Chris@87
|
57 prev_level = _global_log.threshold
|
Chris@87
|
58 if prev_level > DEBUG or force:
|
Chris@87
|
59 # If we're running at DEBUG, don't change the threshold, as there's
|
Chris@87
|
60 # likely a good reason why we're running at this level.
|
Chris@87
|
61 _global_log.threshold = level
|
Chris@87
|
62 if level <= DEBUG:
|
Chris@87
|
63 info('set_threshold: setting threshold to DEBUG level,'
|
Chris@87
|
64 ' it can be changed only with force argument')
|
Chris@87
|
65 else:
|
Chris@87
|
66 info('set_threshold: not changing threshold from DEBUG level'
|
Chris@87
|
67 ' %s to %s' % (prev_level, level))
|
Chris@87
|
68 return prev_level
|
Chris@87
|
69
|
Chris@87
|
70
|
Chris@87
|
71 def set_verbosity(v, force=False):
|
Chris@87
|
72 prev_level = _global_log.threshold
|
Chris@87
|
73 if v < 0:
|
Chris@87
|
74 set_threshold(ERROR, force)
|
Chris@87
|
75 elif v == 0:
|
Chris@87
|
76 set_threshold(WARN, force)
|
Chris@87
|
77 elif v == 1:
|
Chris@87
|
78 set_threshold(INFO, force)
|
Chris@87
|
79 elif v >= 2:
|
Chris@87
|
80 set_threshold(DEBUG, force)
|
Chris@87
|
81 return {FATAL:-2,ERROR:-1,WARN:0,INFO:1,DEBUG:2}.get(prev_level, 1)
|
Chris@87
|
82
|
Chris@87
|
83
|
Chris@87
|
84 _global_color_map = {
|
Chris@87
|
85 DEBUG:cyan_text,
|
Chris@87
|
86 INFO:default_text,
|
Chris@87
|
87 WARN:red_text,
|
Chris@87
|
88 ERROR:red_text,
|
Chris@87
|
89 FATAL:red_text
|
Chris@87
|
90 }
|
Chris@87
|
91
|
Chris@87
|
92 # don't use INFO,.. flags in set_verbosity, these flags are for set_threshold.
|
Chris@87
|
93 set_verbosity(0, force=True)
|