queuehandler.py 757 B

12345678910111213141516171819202122
  1. import logging
  2. class QueueHandler(logging.Handler):
  3. """Class to send logging records to a queue
  4. It can be used from different threads
  5. The ConsoleUi class polls this queue to display records in a ScrolledText widget
  6. """
  7. # Example from Moshe Kaplan: https://gist.github.com/moshekaplan/c425f861de7bbf28ef06
  8. # (https://stackoverflow.com/questions/13318742/python-logging-to-tkinter-text-widget) is not thread safe!
  9. # See https://stackoverflow.com/questions/43909849/tkinter-python-crashes-on-new-thread-trying-to-log-on-main-thread
  10. def __init__(self, log_queue):
  11. super().__init__()
  12. self.log_queue = log_queue
  13. def emit(self, record):
  14. self.log_queue.put(record)
  15. def flush(self):
  16. pass