|
37 | 37 | """
|
38 | 38 |
|
39 | 39 | import argparse
|
| 40 | +from collections import namedtuple |
40 | 41 | import logging
|
41 | 42 | import sys
|
42 | 43 | from typing import Any, BinaryIO
|
43 | 44 | import socket
|
44 | 45 | from inspect import cleandoc
|
45 | 46 | import serial # type: ignore
|
46 |
| - |
| 47 | +import re |
47 | 48 | import pw_cli.log
|
48 | 49 | from pw_console.console_app import embed
|
49 | 50 | from pw_console.__main__ import create_temp_log_file
|
@@ -149,9 +150,27 @@ def read(self, num_bytes: int = PW_RPC_MAX_PACKET_SIZE):
|
149 | 150 | def write_to_output(data: bytes,
|
150 | 151 | unused_output: BinaryIO = sys.stdout.buffer,):
|
151 | 152 | log_line = data
|
152 |
| - |
| 153 | + RegexStruct = namedtuple('RegexStruct', 'platform type regex match_num') |
| 154 | + LEVEL_MAPPING = {"I": logging.INFO, "W": logging.WARNING, |
| 155 | + "E": logging.ERROR, "F": logging.FATAL, "V": logging.DEBUG, "D": logging.DEBUG} |
| 156 | + ESP_CHIP_REGEX = r"(?P<level>[IWEFV]) \((?P<time>\d+)\) (?P<mod>chip\[[a-zA-Z]+\]):\s(?P<msg>.*)" |
| 157 | + ESP_APP_REGEX = r"(?P<level>[IWEFVD]) \((?P<time>\d+)\) (?P<mod>[a-z\-_A-Z]+):\s(?P<msg>.*)" |
| 158 | + LogRegexes = [RegexStruct("ESP", "CHIP", re.compile(ESP_CHIP_REGEX), 4), |
| 159 | + RegexStruct("ESP", "APP", re.compile(ESP_APP_REGEX), 4) |
| 160 | + ] |
153 | 161 | for line in log_line.decode(errors="surrogateescape").splitlines():
|
154 |
| - _DEVICE_LOG.info(line) |
| 162 | + fields = {'level': logging.INFO, "time": "", |
| 163 | + "mod": "", "type": "", "msg": line} |
| 164 | + for log_regex in LogRegexes: |
| 165 | + match = log_regex.regex.search(line) |
| 166 | + if match and len(match.groups()) == log_regex.match_num: |
| 167 | + fields['type'] = log_regex.type |
| 168 | + fields.update(match.groupdict()) |
| 169 | + if "level" in match.groupdict(): |
| 170 | + fields["level"] = LEVEL_MAPPING[fields["level"]] |
| 171 | + break |
| 172 | + _DEVICE_LOG.log(fields["level"], fields["msg"], extra={'extra_metadata_fields': { |
| 173 | + "time": fields["time"], "type": fields["type"], "mod": fields["mod"]}}) |
155 | 174 |
|
156 | 175 |
|
157 | 176 | def console(device: str, baudrate: int,
|
|
0 commit comments