__main__.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright 2024 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. import argparse
  16. import logging
  17. import sys
  18. from . import interactive
  19. def main(args=None):
  20. def reattach_handler(logger, formatter, handler):
  21. if handler is not None:
  22. logger.removeHandler(handler)
  23. handler = logging.StreamHandler(sys.stdout)
  24. handler.setFormatter(formatter)
  25. logger.addHandler(handler)
  26. return handler
  27. if args is None:
  28. parser = argparse.ArgumentParser(description='Pebble Commander.')
  29. parser.add_argument('-v', '--verbose', help='verbose logging', action='count',
  30. default=0)
  31. parser.add_argument('-t', '--tty', help='serial port (defaults to auto-detect)', metavar='TTY',
  32. default=None)
  33. parser.add_argument('-c', '--pcap', metavar='FILE', default=None,
  34. help='write packet capture to pcap file')
  35. parser.add_argument('dict', help='log-hashing dictionary file', metavar='loghash_dict.json',
  36. nargs='?', default=None)
  37. args = parser.parse_args()
  38. log_level = (logging.DEBUG if args.verbose >= 2
  39. else logging.INFO if args.verbose >= 1
  40. else logging.WARNING)
  41. use_colors = True
  42. formatter_string = '%(name)-12s: %(levelname)-8s %(message)s'
  43. if use_colors:
  44. formatter_string = '\x1b[33m%s\x1b[m' % formatter_string
  45. formatter = logging.Formatter(formatter_string)
  46. handler = reattach_handler(logging.getLogger(), formatter, None)
  47. logging.getLogger().setLevel(log_level)
  48. with interactive.InteractivePebbleCommander(
  49. loghash_path=args.dict, tty=args.tty, capfile=args.pcap) as cmdr:
  50. cmdr.attach_prompt_toolkit()
  51. # Re-create the logging handler to use the patched stdout
  52. handler = reattach_handler(logging.getLogger(), formatter, handler)
  53. cmdr.command_loop()
  54. if __name__ == '__main__':
  55. main()