logHandler.py 873 B

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