|
6 | 6 | """ |
7 | 7 |
|
8 | 8 | import logging |
| 9 | +import re |
9 | 10 | import sys |
10 | 11 |
|
11 | 12 | from lib.core.enums import CUSTOM_LOGGING |
|
20 | 21 | try: |
21 | 22 | from thirdparty.ansistrm.ansistrm import ColorizingStreamHandler |
22 | 23 |
|
| 24 | + class _ColorizingStreamHandler(ColorizingStreamHandler): |
| 25 | + def colorize(self, message, levelno): |
| 26 | + if levelno in self.level_map and self.is_tty: |
| 27 | + bg, fg, bold = self.level_map[levelno] |
| 28 | + params = [] |
| 29 | + |
| 30 | + if bg in self.color_map: |
| 31 | + params.append(str(self.color_map[bg] + 40)) |
| 32 | + |
| 33 | + if fg in self.color_map: |
| 34 | + params.append(str(self.color_map[fg] + 30)) |
| 35 | + |
| 36 | + if bold: |
| 37 | + params.append('1') |
| 38 | + |
| 39 | + if params and message: |
| 40 | + match = re.search(r"\A(\s+)", message) |
| 41 | + prefix = match.group(1) if match else "" |
| 42 | + message = message[len(prefix):] |
| 43 | + |
| 44 | + match = re.search(r"\[([A-Z ]+)\]", message) # log level |
| 45 | + if match: |
| 46 | + level = match.group(1) |
| 47 | + if message.startswith(self.bold): |
| 48 | + message = message.replace(self.bold, "") |
| 49 | + reset = self.reset + self.bold |
| 50 | + params.append('1') |
| 51 | + else: |
| 52 | + reset = self.reset |
| 53 | + message = message.replace(level, ''.join((self.csi, ';'.join(params), 'm', level, reset)), 1) |
| 54 | + |
| 55 | + match = re.search(r"\A\s*\[([\d:]+)\]", message) # time |
| 56 | + if match: |
| 57 | + time = match.group(1) |
| 58 | + message = message.replace(time, ''.join((self.csi, str(self.color_map["cyan"] + 30), 'm', time, self._reset(message))), 1) |
| 59 | + |
| 60 | + match = re.search(r"\[(#\d+)\]", message) # counter |
| 61 | + if match: |
| 62 | + counter = match.group(1) |
| 63 | + message = message.replace(counter, ''.join((self.csi, str(self.color_map["yellow"] + 30), 'm', counter, self._reset(message))), 1) |
| 64 | + |
| 65 | + if level != "PAYLOAD": |
| 66 | + if any(_ in message for _ in ("parsed DBMS error message",)): |
| 67 | + match = re.search(r": '(.+)'", message) |
| 68 | + if match: |
| 69 | + string = match.group(1) |
| 70 | + message = message.replace("'%s'" % string, "'%s'" % ''.join((self.csi, str(self.color_map["white"] + 30), 'm', string, self._reset(message))), 1) |
| 71 | + else: |
| 72 | + match = re.search(r"\bresumed: '(.+\.\.\.)", message) |
| 73 | + if match: |
| 74 | + string = match.group(1) |
| 75 | + message = message.replace("'%s" % string, "'%s" % ''.join((self.csi, str(self.color_map["white"] + 30), 'm', string, self._reset(message))), 1) |
| 76 | + else: |
| 77 | + match = re.search(r" \('(.+)'\)\Z", message) |
| 78 | + if match: |
| 79 | + string = match.group(1) |
| 80 | + message = message.replace("'%s'" % string, "'%s'" % ''.join((self.csi, str(self.color_map["white"] + 30), 'm', string, self._reset(message))), 1) |
| 81 | + else: |
| 82 | + for match in re.finditer(r"[^\w]'([^']+)'", message): # single-quoted |
| 83 | + string = match.group(1) |
| 84 | + message = message.replace("'%s'" % string, "'%s'" % ''.join((self.csi, str(self.color_map["white"] + 30), 'm', string, self._reset(message))), 1) |
| 85 | + else: |
| 86 | + message = ''.join((self.csi, ';'.join(params), 'm', message, self.reset)) |
| 87 | + |
| 88 | + if prefix: |
| 89 | + message = "%s%s" % (prefix, message) |
| 90 | + |
| 91 | + message = message.replace("%s]" % self.bold, "]%s" % self.bold) # dirty patch |
| 92 | + |
| 93 | + return message |
| 94 | + |
23 | 95 | disableColor = False |
24 | 96 |
|
25 | 97 | for argument in sys.argv: |
|
30 | 102 | if disableColor: |
31 | 103 | LOGGER_HANDLER = logging.StreamHandler(sys.stdout) |
32 | 104 | else: |
33 | | - LOGGER_HANDLER = ColorizingStreamHandler(sys.stdout) |
| 105 | + LOGGER_HANDLER = _ColorizingStreamHandler(sys.stdout) |
34 | 106 | LOGGER_HANDLER.level_map[logging.getLevelName("PAYLOAD")] = (None, "cyan", False) |
35 | 107 | LOGGER_HANDLER.level_map[logging.getLevelName("TRAFFIC OUT")] = (None, "magenta", False) |
36 | 108 | LOGGER_HANDLER.level_map[logging.getLevelName("TRAFFIC IN")] = ("magenta", None, False) |
|
0 commit comments