decoupled logger and gui

logger is connected to gui after gui is ready
moved all logging configuration to log_config file
This commit is contained in:
Adam Saudagar 2022-02-03 05:15:00 +05:30
parent c624557a41
commit 76e17c4502
3 changed files with 49 additions and 41 deletions

View File

@ -7,13 +7,13 @@ import win32con
import win32gui
import fishy
from fishy import gui, helper, web
from fishy.gui import GUI, splash, update_dialog, check_eula
from fishy import helper, web
from fishy.engine.common.event_handler import EngineEventHandler
from fishy.gui import GUI, splash, update_dialog
from fishy.helper import hotkey, auto_update
from fishy.gui.log_config import GuiLogger
from fishy.helper import hotkey
from fishy.helper.active_poll import active
from fishy.helper.config import config
from fishy.helper.helper import print_exc
from fishy.helper.hotkey.hotkey_process import hotkey
from fishy.helper.migration import Migration
@ -57,34 +57,31 @@ def main():
print("launching please wait...")
config.init()
if not gui.check_eula():
if not check_eula():
return
finish_splash = splash.start()
logger = GuiLogger()
config.start_backup_scheduler()
active.init()
hotkey.init()
def on_gui_load():
finish_splash()
update_dialog.check_update(gui_window)
info_logger = ["comtypes", "PIL"]
for i in info_logger:
_logger = logging.getLogger(i)
_logger.setLevel(logging.INFO)
update_dialog.check_update(gui)
logger.connect(gui)
window_to_hide = win32gui.GetForegroundWindow()
bot = EngineEventHandler(lambda: gui_window)
gui_window = GUI(lambda: bot, on_gui_load)
bot = EngineEventHandler(lambda: gui)
gui = GUI(lambda: bot, on_gui_load)
hotkey.start()
logging.info(f"Fishybot v{fishy.__version__}")
initialize(window_to_hide)
gui_window.start()
gui.start()
active.start()
bot.start_event_handler() # main thread loop

View File

@ -15,7 +15,7 @@ from fishy.gui.funcs import GUIFuncs
from ..helper.config import config
from ..helper.helper import wait_until
from . import main_gui
from .log_config import GUIStreamHandler
from .log_config import GuiLogger
@dataclass
@ -52,12 +52,6 @@ class GUI:
self._notify = None
self.login = None
root_logger = logging.getLogger('')
root_logger.setLevel(logging.DEBUG)
logging.getLogger('urllib3').setLevel(logging.WARNING)
new_console = GUIStreamHandler(self)
root_logger.addHandler(new_console)
@property
def engine(self):
return self.get_engine()
@ -99,3 +93,17 @@ class GUI:
def _get_start_stop_text(self):
return "STOP (F9)" if self._bot_running else "START (F9)"
def write_to_console(self, msg):
if not self._console:
return
numlines = self._console.index('end - 1 line').split('.')[0]
self._console['state'] = 'normal'
if int(numlines) >= 50: # delete old lines
self._console.delete(1.0, 2.0)
if self._console.index('end-1c') != '1.0': # new line for each log
self._console.insert('end', '\n')
self._console.insert('end', msg)
self._console.see("end") # scroll to bottom
self._console['state'] = 'disabled'

View File

@ -1,33 +1,36 @@
import logging
import typing
from logging import StreamHandler, Formatter
from fishy.helper.config import config
if typing.TYPE_CHECKING:
from . import GUI
class GUIStreamHandler(StreamHandler):
def __init__(self, gui):
class GuiLogger(StreamHandler):
def __init__(self):
StreamHandler.__init__(self)
self.gui = gui
def emit(self, record):
self.renderer = None
self._temp_buffer = []
formatter = Formatter('%(levelname)s - %(message)s')
self.setFormatter(formatter)
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)
self.setLevel(logging.DEBUG if config.get("debug", False) else logging.INFO)
logging.getLogger("").addHandler(self)
def emit(self, record):
msg = self.format(record)
self.gui.call_in_thread(lambda: _write_to_console(self.gui, msg))
if self.renderer:
self.renderer(msg)
else:
self._temp_buffer.append(msg)
def _write_to_console(root: 'GUI', msg):
numlines = root._console.index('end - 1 line').split('.')[0]
root._console['state'] = 'normal'
if int(numlines) >= 50: # delete old lines
root._console.delete(1.0, 2.0)
if root._console.index('end-1c') != '1.0': # new line for each log
root._console.insert('end', '\n')
root._console.insert('end', msg)
root._console.see("end") # scroll to bottom
root._console['state'] = 'disabled'
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))