logHandler.py 790 B

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