logHandler.py 741 B

1234567891011121314151617181920212223242526272829303132333435
  1. import datetime
  2. class LogList():
  3. def __init__(self):
  4. self.log_list = list()
  5. self.read_index = 0
  6. def add_item(self, item):
  7. self.log_list.append(item)
  8. if len(self.log_list) > 100:
  9. self.log_list.pop(0)
  10. def log_and_print(self, *args):
  11. args = [str(i) for i in args]
  12. item = " ".join(args)
  13. item = str(datetime.datetime.now())+" "+str(item)
  14. self.add_item(item)
  15. print(item)
  16. def get_log_list(self):
  17. return self.log_list
  18. def get_new_items(self):
  19. items = self.log_list[self.read_index:]
  20. self.read_index = len(self.log_list)
  21. return items
  22. _log_handler = None
  23. def get_log_handler():
  24. global _log_handler
  25. if not _log_handler:
  26. _log_handler = LogList()
  27. return _log_handler