5
5
try :
6
6
import colorful
7
7
except ImportError :
8
+
8
9
class colorful :
10
+
9
11
def __getattr__ (self , attr ):
10
12
return lambda st : st
13
+
11
14
colorful = colorful ()
12
15
from .utils import make_unicode
13
16
14
17
__print = print
18
+
19
+
15
20
def _print (* args , ** kwargs ):
16
21
flush = False
17
22
if 'flush' in kwargs :
@@ -21,22 +26,28 @@ def _print(*args, **kwargs):
21
26
if flush :
22
27
kwargs .get ('file' , sys .stdout ).flush ()
23
28
29
+
24
30
def _join_dict (a , b ):
25
31
"""join two dict"""
26
32
c = a .copy ()
27
33
for k , v in b .items ():
28
34
c [k ] = v
29
35
return c
30
36
37
+
31
38
_log_funcs = {}
32
39
_log_lock = Lock ()
40
+
41
+
33
42
def log (funcname , * args , ** kwargs ):
34
43
"""log with log function specified by ``funcname``"""
35
44
_log_lock .acquire ()
36
- rv = _log_funcs .get (funcname , lambda * args , ** kwargs : None )(* args , ** kwargs )
45
+ rv = _log_funcs .get (funcname , lambda * args , ** kwargs : None )(* args ,
46
+ ** kwargs )
37
47
_log_lock .release ()
38
48
return rv
39
49
50
+
40
51
"""5 log levels
41
52
1. debug: debug info
42
53
2. info: common info
@@ -51,6 +62,7 @@ def log(funcname, *args, **kwargs):
51
62
warn = partial (log , 'warn' )
52
63
error = partial (log , 'error' )
53
64
65
+
54
66
def register_logfunc (funcname , func ):
55
67
"""register logfunc
56
68
str funcname -> name of logfunc
@@ -64,17 +76,28 @@ def register_logfunc(funcname, func):
64
76
except KeyError :
65
77
pass
66
78
67
- _nb_print = lambda * args , ** kwargs : _print (* args , ** _join_dict (kwargs , {'flush' : True }))
68
- _nb_print_e = lambda * args , ** kwargs : _print (* args , ** _join_dict (kwargs , {'file' : sys .stderr , 'flush' : True }))
69
- _cl_print = lambda color , * args , ** kwargs : _nb_print (* [color (make_unicode (item )) for item in args ], ** kwargs ) if sys .stdout .isatty () else _nb_print (* args , ** kwargs )
70
- _cl_print_e = lambda color , * args , ** kwargs : _nb_print_e (* [color (make_unicode (item )) for item in args ], ** kwargs ) if sys .stderr .isatty () else _nb_print_e (* args , ** kwargs )
79
+
80
+ _nb_print = lambda * args , ** kwargs : _print (
81
+ * args , ** _join_dict (kwargs , {'flush' : True }))
82
+ _nb_print_e = lambda * args , ** kwargs : _print (
83
+ * args , ** _join_dict (kwargs , {
84
+ 'file' : sys .stderr ,
85
+ 'flush' : True
86
+ }))
87
+ _cl_print = lambda color , * args , ** kwargs : _nb_print (
88
+ * [color (make_unicode (item )) for item in args ], ** kwargs
89
+ ) if sys .stdout .isatty () else _nb_print (* args , ** kwargs )
90
+ _cl_print_e = lambda color , * args , ** kwargs : _nb_print_e (
91
+ * [color (make_unicode (item )) for item in args ], ** kwargs
92
+ ) if sys .stderr .isatty () else _nb_print_e (* args , ** kwargs )
71
93
72
94
_default_debug = partial (_cl_print , colorful .cyan )
73
95
_default_info = partial (_cl_print , colorful .blue )
74
96
_default_print = _nb_print
75
97
_default_warn = partial (_cl_print_e , colorful .yellow )
76
98
_default_error = partial (_cl_print_e , colorful .red )
77
99
100
+
78
101
def set_quiet ():
79
102
"""set log mode to "quiet" """
80
103
register_logfunc ('debug' , None )
@@ -83,6 +106,7 @@ def set_quiet():
83
106
register_logfunc ('warn' , None )
84
107
register_logfunc ('error' , _default_error )
85
108
109
+
86
110
def set_normal ():
87
111
"""set log mode to "normal" """
88
112
register_logfunc ('debug' , None )
@@ -91,6 +115,7 @@ def set_normal():
91
115
register_logfunc ('warn' , _default_warn )
92
116
register_logfunc ('error' , _default_error )
93
117
118
+
94
119
def set_verbose ():
95
120
"""set log mode to "verbose" """
96
121
register_logfunc ('debug' , _default_debug )
@@ -99,4 +124,5 @@ def set_verbose():
99
124
register_logfunc ('warn' , _default_warn )
100
125
register_logfunc ('error' , _default_error )
101
126
127
+
102
128
set_normal ()
0 commit comments