output_helpers.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import subprocess
  4. import sys
  5. from typing import BinaryIO, Callable, Optional, Union # noqa: F401
  6. # ANSI terminal codes (if changed, regular expressions in LineMatcher need to be updated)
  7. ANSI_RED = '\033[1;31m'
  8. ANSI_YELLOW = '\033[0;33m'
  9. ANSI_NORMAL = '\033[0m'
  10. def color_print(message, color, newline='\n'): # type: (str, str, Optional[str]) -> None
  11. """ Print a message to stderr with colored highlighting """
  12. sys.stderr.write('%s%s%s%s' % (color, message, ANSI_NORMAL, newline))
  13. sys.stderr.flush()
  14. def normal_print(message): # type: (str) -> None
  15. sys.stderr.write(ANSI_NORMAL + message)
  16. def yellow_print(message, newline='\n'): # type: (str, Optional[str]) -> None
  17. color_print(message, ANSI_YELLOW, newline)
  18. def red_print(message, newline='\n'): # type: (str, Optional[str]) -> None
  19. color_print(message, ANSI_RED, newline)
  20. def lookup_pc_address(pc_addr, toolchain_prefix, elf_file): # type: (str, str, str) -> Optional[str]
  21. cmd = ['%saddr2line' % toolchain_prefix, '-pfiaC', '-e', elf_file, pc_addr]
  22. try:
  23. translation = subprocess.check_output(cmd, cwd='.')
  24. if b'?? ??:0' not in translation:
  25. return translation.decode()
  26. except OSError as e:
  27. red_print('%s: %s' % (' '.join(cmd), e))
  28. return None