Spaces:
Runtime error
Runtime error
Commit
•
a5c5f76
1
Parent(s):
730387c
Upload logging_colors.py
Browse files- modules/logging_colors.py +117 -0
modules/logging_colors.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copied from https://stackoverflow.com/a/1336640
|
2 |
+
|
3 |
+
import logging
|
4 |
+
import platform
|
5 |
+
|
6 |
+
logging.basicConfig(
|
7 |
+
format='%(asctime)s %(levelname)s:%(message)s',
|
8 |
+
datefmt='%Y-%m-%d %H:%M:%S',
|
9 |
+
)
|
10 |
+
|
11 |
+
|
12 |
+
def add_coloring_to_emit_windows(fn):
|
13 |
+
# add methods we need to the class
|
14 |
+
def _out_handle(self):
|
15 |
+
import ctypes
|
16 |
+
return ctypes.windll.kernel32.GetStdHandle(self.STD_OUTPUT_HANDLE)
|
17 |
+
out_handle = property(_out_handle)
|
18 |
+
|
19 |
+
def _set_color(self, code):
|
20 |
+
import ctypes
|
21 |
+
|
22 |
+
# Constants from the Windows API
|
23 |
+
self.STD_OUTPUT_HANDLE = -11
|
24 |
+
hdl = ctypes.windll.kernel32.GetStdHandle(self.STD_OUTPUT_HANDLE)
|
25 |
+
ctypes.windll.kernel32.SetConsoleTextAttribute(hdl, code)
|
26 |
+
|
27 |
+
setattr(logging.StreamHandler, '_set_color', _set_color)
|
28 |
+
|
29 |
+
def new(*args):
|
30 |
+
FOREGROUND_BLUE = 0x0001 # text color contains blue.
|
31 |
+
FOREGROUND_GREEN = 0x0002 # text color contains green.
|
32 |
+
FOREGROUND_RED = 0x0004 # text color contains red.
|
33 |
+
FOREGROUND_INTENSITY = 0x0008 # text color is intensified.
|
34 |
+
FOREGROUND_WHITE = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
|
35 |
+
# winbase.h
|
36 |
+
# STD_INPUT_HANDLE = -10
|
37 |
+
# STD_OUTPUT_HANDLE = -11
|
38 |
+
# STD_ERROR_HANDLE = -12
|
39 |
+
|
40 |
+
# wincon.h
|
41 |
+
# FOREGROUND_BLACK = 0x0000
|
42 |
+
FOREGROUND_BLUE = 0x0001
|
43 |
+
FOREGROUND_GREEN = 0x0002
|
44 |
+
# FOREGROUND_CYAN = 0x0003
|
45 |
+
FOREGROUND_RED = 0x0004
|
46 |
+
FOREGROUND_MAGENTA = 0x0005
|
47 |
+
FOREGROUND_YELLOW = 0x0006
|
48 |
+
# FOREGROUND_GREY = 0x0007
|
49 |
+
FOREGROUND_INTENSITY = 0x0008 # foreground color is intensified.
|
50 |
+
|
51 |
+
# BACKGROUND_BLACK = 0x0000
|
52 |
+
# BACKGROUND_BLUE = 0x0010
|
53 |
+
# BACKGROUND_GREEN = 0x0020
|
54 |
+
# BACKGROUND_CYAN = 0x0030
|
55 |
+
# BACKGROUND_RED = 0x0040
|
56 |
+
# BACKGROUND_MAGENTA = 0x0050
|
57 |
+
BACKGROUND_YELLOW = 0x0060
|
58 |
+
# BACKGROUND_GREY = 0x0070
|
59 |
+
BACKGROUND_INTENSITY = 0x0080 # background color is intensified.
|
60 |
+
|
61 |
+
levelno = args[1].levelno
|
62 |
+
if (levelno >= 50):
|
63 |
+
color = BACKGROUND_YELLOW | FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_INTENSITY
|
64 |
+
elif (levelno >= 40):
|
65 |
+
color = FOREGROUND_RED | FOREGROUND_INTENSITY
|
66 |
+
elif (levelno >= 30):
|
67 |
+
color = FOREGROUND_YELLOW | FOREGROUND_INTENSITY
|
68 |
+
elif (levelno >= 20):
|
69 |
+
color = FOREGROUND_GREEN
|
70 |
+
elif (levelno >= 10):
|
71 |
+
color = FOREGROUND_MAGENTA
|
72 |
+
else:
|
73 |
+
color = FOREGROUND_WHITE
|
74 |
+
args[0]._set_color(color)
|
75 |
+
|
76 |
+
ret = fn(*args)
|
77 |
+
args[0]._set_color(FOREGROUND_WHITE)
|
78 |
+
# print "after"
|
79 |
+
return ret
|
80 |
+
return new
|
81 |
+
|
82 |
+
|
83 |
+
def add_coloring_to_emit_ansi(fn):
|
84 |
+
# add methods we need to the class
|
85 |
+
def new(*args):
|
86 |
+
levelno = args[1].levelno
|
87 |
+
if (levelno >= 50):
|
88 |
+
color = '\x1b[31m' # red
|
89 |
+
elif (levelno >= 40):
|
90 |
+
color = '\x1b[31m' # red
|
91 |
+
elif (levelno >= 30):
|
92 |
+
color = '\x1b[33m' # yellow
|
93 |
+
elif (levelno >= 20):
|
94 |
+
color = '\x1b[32m' # green
|
95 |
+
elif (levelno >= 10):
|
96 |
+
color = '\x1b[35m' # pink
|
97 |
+
else:
|
98 |
+
color = '\x1b[0m' # normal
|
99 |
+
args[1].msg = color + args[1].msg + '\x1b[0m' # normal
|
100 |
+
# print "after"
|
101 |
+
return fn(*args)
|
102 |
+
return new
|
103 |
+
|
104 |
+
|
105 |
+
if platform.system() == 'Windows':
|
106 |
+
# Windows does not support ANSI escapes and we are using API calls to set the console color
|
107 |
+
logging.StreamHandler.emit = add_coloring_to_emit_windows(logging.StreamHandler.emit)
|
108 |
+
else:
|
109 |
+
# all non-Windows platforms are supporting ANSI escapes so we use them
|
110 |
+
logging.StreamHandler.emit = add_coloring_to_emit_ansi(logging.StreamHandler.emit)
|
111 |
+
# log = logging.getLogger()
|
112 |
+
# log.addFilter(log_filter())
|
113 |
+
# //hdlr = logging.StreamHandler()
|
114 |
+
# //hdlr.setFormatter(formatter())
|
115 |
+
|
116 |
+
logger = logging.getLogger('text-generation-webui')
|
117 |
+
logger.setLevel(logging.DEBUG)
|