tail -f with adaptable filter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import argparse
  2. from console_bottombar import BottomBar
  3. import os
  4. import re
  5. import sys
  6. import tail
  7. import task
  8. VERSION = "0.1.0"
  9. HELPTEXT = """
  10. F1: Get this help message
  11. F2: Set a filter (regular expression) for the topic of a message
  12. Examples:
  13. * "/gfw/.*/main_light/" to get everything with "/gfw/" before "/main_light/"
  14. * "^zigbee.*(?>!logging)$" to get everything starting with "zigbee" and not ending with "logging"
  15. * "^(?!shellies).*/dirk/.*temperature$" to get everything not starting with "shellies" followed by "/dirk/" and ends with "temperature"
  16. F12: Quit ftail
  17. 'c': Clear screen
  18. 'q': Quit
  19. """
  20. def tail_parser(line):
  21. global my_bb
  22. try:
  23. match = len(re.findall(my_bb.get_entry('msg_re'), line)) > 0
  24. except re.error:
  25. print('No valid regular expression (%s). No filter active.' % my_bb.get_entry('msg_re'))
  26. match = True
  27. if match:
  28. sys.stdout.write(line)
  29. if __name__ == "__main__":
  30. parser = argparse.ArgumentParser(description='This is a mqtt sniffer.')
  31. parser.add_argument('logfile') # , nargs='+' for >= 1
  32. args = parser.parse_args()
  33. if not os.access(args.logfile, os.R_OK):
  34. print("Error: File %s is not readable!" % logfile)
  35. sys.exit(1)
  36. else:
  37. #
  38. # Background tail task
  39. #
  40. tp = tail.Tail(args.logfile)
  41. tp.register_callback(tail_parser)
  42. my_tail = task.delayed(0.1, tp.follow, 0.1)
  43. #
  44. # Bottombar
  45. #
  46. my_bb = BottomBar(VERSION, label='ftail')
  47. my_bb.add_entry('help', 1, my_bb.FUNC_INFO, '[F1] Help', infotext=HELPTEXT)
  48. my_bb.add_entry('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter', default="")
  49. my_bb.add_entry('quit', 12, my_bb.FUNC_QUIT, "Quit", label='[F12]', right=True)
  50. my_tail.run()
  51. my_bb.run()
  52. my_tail.stop()