esp-idf/tools/idf_monitor_base/output_helpers.py

41 lines
1.3 KiB
Python
Raw Normal View History

2022-05-23 09:30:13 -04:00
# SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
2021-02-01 05:40:03 -05:00
2021-05-14 05:20:38 -04:00
import subprocess
2021-02-01 05:40:03 -05:00
import sys
from typing import BinaryIO, Callable, Optional, Union # noqa: F401
2021-02-01 05:40:03 -05:00
2021-05-14 05:20:38 -04:00
# ANSI terminal codes (if changed, regular expressions in LineMatcher need to be updated)
2021-02-01 05:40:03 -05:00
ANSI_RED = '\033[1;31m'
ANSI_YELLOW = '\033[0;33m'
ANSI_NORMAL = '\033[0m'
2021-05-14 05:20:38 -04:00
def color_print(message, color, newline='\n'): # type: (str, str, Optional[str]) -> None
2021-02-01 05:40:03 -05:00
""" Print a message to stderr with colored highlighting """
sys.stderr.write('%s%s%s%s' % (color, message, ANSI_NORMAL, newline))
2021-05-14 05:20:38 -04:00
def normal_print(message): # type: (str) -> None
2021-02-01 05:40:03 -05:00
sys.stderr.write(ANSI_NORMAL + message)
2021-05-14 05:20:38 -04:00
def yellow_print(message, newline='\n'): # type: (str, Optional[str]) -> None
2021-02-01 05:40:03 -05:00
color_print(message, ANSI_YELLOW, newline)
2021-05-14 05:20:38 -04:00
def red_print(message, newline='\n'): # type: (str, Optional[str]) -> None
2021-02-01 05:40:03 -05:00
color_print(message, ANSI_RED, newline)
2021-05-14 05:20:38 -04:00
def lookup_pc_address(pc_addr, toolchain_prefix, elf_file): # type: (str, str, str) -> Optional[str]
cmd = ['%saddr2line' % toolchain_prefix, '-pfiaC', '-e', elf_file, pc_addr]
try:
translation = subprocess.check_output(cmd, cwd='.')
if b'?? ??:0' not in translation:
return translation.decode()
except OSError as e:
red_print('%s: %s' % (' '.join(cmd), e))
return None