2022-01-30 14:28:30 +00:00
|
|
|
import logging
|
|
|
|
from logging import StreamHandler, Formatter
|
2020-05-14 02:03:13 +00:00
|
|
|
|
2022-02-02 22:39:39 +00:00
|
|
|
from fishy.helper.config import config
|
|
|
|
|
2020-05-14 02:03:13 +00:00
|
|
|
|
2022-02-02 23:45:00 +00:00
|
|
|
class GuiLogger(StreamHandler):
|
|
|
|
def __init__(self):
|
2020-05-14 02:03:13 +00:00
|
|
|
StreamHandler.__init__(self)
|
|
|
|
|
2022-02-02 23:45:00 +00:00
|
|
|
self.renderer = None
|
|
|
|
self._temp_buffer = []
|
|
|
|
|
2022-02-02 22:39:39 +00:00
|
|
|
formatter = Formatter('%(levelname)s - %(message)s')
|
|
|
|
self.setFormatter(formatter)
|
2022-02-02 23:45:00 +00:00
|
|
|
logging_config = {"comtypes": logging.INFO,
|
|
|
|
"PIL": logging.INFO,
|
|
|
|
"urllib3": logging.WARNING,
|
|
|
|
"": logging.DEBUG}
|
|
|
|
for name, level in logging_config.items():
|
|
|
|
_logger = logging.getLogger(name)
|
|
|
|
_logger.setLevel(level)
|
2022-02-02 22:39:39 +00:00
|
|
|
self.setLevel(logging.DEBUG if config.get("debug", False) else logging.INFO)
|
2022-02-02 23:45:00 +00:00
|
|
|
logging.getLogger("").addHandler(self)
|
2020-05-14 02:03:13 +00:00
|
|
|
|
2022-02-02 23:45:00 +00:00
|
|
|
def emit(self, record):
|
|
|
|
msg = self.format(record)
|
|
|
|
if self.renderer:
|
|
|
|
self.renderer(msg)
|
|
|
|
else:
|
|
|
|
self._temp_buffer.append(msg)
|
2020-05-14 02:03:13 +00:00
|
|
|
|
2022-02-02 23:45:00 +00:00
|
|
|
def connect(self, gui):
|
|
|
|
self.renderer = lambda m: gui.call_in_thread(lambda: gui.write_to_console(m))
|
|
|
|
while self._temp_buffer:
|
|
|
|
self.renderer(self._temp_buffer.pop(0))
|