mirror of
https://github.com/fishyboteso/fishyboteso.git
synced 2024-08-30 18:32:13 +00:00
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:
parent
c624557a41
commit
76e17c4502
@ -7,13 +7,13 @@ import win32con
|
|||||||
import win32gui
|
import win32gui
|
||||||
|
|
||||||
import fishy
|
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.engine.common.event_handler import EngineEventHandler
|
||||||
from fishy.gui import GUI, splash, update_dialog
|
from fishy.gui.log_config import GuiLogger
|
||||||
from fishy.helper import hotkey, auto_update
|
from fishy.helper import hotkey
|
||||||
from fishy.helper.active_poll import active
|
from fishy.helper.active_poll import active
|
||||||
from fishy.helper.config import config
|
from fishy.helper.config import config
|
||||||
from fishy.helper.helper import print_exc
|
|
||||||
from fishy.helper.hotkey.hotkey_process import hotkey
|
from fishy.helper.hotkey.hotkey_process import hotkey
|
||||||
from fishy.helper.migration import Migration
|
from fishy.helper.migration import Migration
|
||||||
|
|
||||||
@ -57,34 +57,31 @@ def main():
|
|||||||
print("launching please wait...")
|
print("launching please wait...")
|
||||||
|
|
||||||
config.init()
|
config.init()
|
||||||
if not gui.check_eula():
|
if not check_eula():
|
||||||
return
|
return
|
||||||
|
|
||||||
finish_splash = splash.start()
|
finish_splash = splash.start()
|
||||||
|
logger = GuiLogger()
|
||||||
config.start_backup_scheduler()
|
config.start_backup_scheduler()
|
||||||
active.init()
|
active.init()
|
||||||
hotkey.init()
|
hotkey.init()
|
||||||
|
|
||||||
def on_gui_load():
|
def on_gui_load():
|
||||||
finish_splash()
|
finish_splash()
|
||||||
update_dialog.check_update(gui_window)
|
update_dialog.check_update(gui)
|
||||||
|
logger.connect(gui)
|
||||||
info_logger = ["comtypes", "PIL"]
|
|
||||||
for i in info_logger:
|
|
||||||
_logger = logging.getLogger(i)
|
|
||||||
_logger.setLevel(logging.INFO)
|
|
||||||
|
|
||||||
window_to_hide = win32gui.GetForegroundWindow()
|
window_to_hide = win32gui.GetForegroundWindow()
|
||||||
|
|
||||||
bot = EngineEventHandler(lambda: gui_window)
|
bot = EngineEventHandler(lambda: gui)
|
||||||
gui_window = GUI(lambda: bot, on_gui_load)
|
gui = GUI(lambda: bot, on_gui_load)
|
||||||
|
|
||||||
hotkey.start()
|
hotkey.start()
|
||||||
|
|
||||||
logging.info(f"Fishybot v{fishy.__version__}")
|
logging.info(f"Fishybot v{fishy.__version__}")
|
||||||
initialize(window_to_hide)
|
initialize(window_to_hide)
|
||||||
|
|
||||||
gui_window.start()
|
gui.start()
|
||||||
active.start()
|
active.start()
|
||||||
|
|
||||||
bot.start_event_handler() # main thread loop
|
bot.start_event_handler() # main thread loop
|
||||||
|
@ -15,7 +15,7 @@ from fishy.gui.funcs import GUIFuncs
|
|||||||
from ..helper.config import config
|
from ..helper.config import config
|
||||||
from ..helper.helper import wait_until
|
from ..helper.helper import wait_until
|
||||||
from . import main_gui
|
from . import main_gui
|
||||||
from .log_config import GUIStreamHandler
|
from .log_config import GuiLogger
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -52,12 +52,6 @@ class GUI:
|
|||||||
self._notify = None
|
self._notify = None
|
||||||
self.login = 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
|
@property
|
||||||
def engine(self):
|
def engine(self):
|
||||||
return self.get_engine()
|
return self.get_engine()
|
||||||
@ -99,3 +93,17 @@ class GUI:
|
|||||||
|
|
||||||
def _get_start_stop_text(self):
|
def _get_start_stop_text(self):
|
||||||
return "STOP (F9)" if self._bot_running else "START (F9)"
|
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'
|
||||||
|
@ -1,33 +1,36 @@
|
|||||||
import logging
|
import logging
|
||||||
import typing
|
|
||||||
from logging import StreamHandler, Formatter
|
from logging import StreamHandler, Formatter
|
||||||
|
|
||||||
from fishy.helper.config import config
|
from fishy.helper.config import config
|
||||||
|
|
||||||
if typing.TYPE_CHECKING:
|
|
||||||
from . import GUI
|
|
||||||
|
|
||||||
|
class GuiLogger(StreamHandler):
|
||||||
class GUIStreamHandler(StreamHandler):
|
def __init__(self):
|
||||||
def __init__(self, gui):
|
|
||||||
StreamHandler.__init__(self)
|
StreamHandler.__init__(self)
|
||||||
self.gui = gui
|
|
||||||
|
|
||||||
def emit(self, record):
|
self.renderer = None
|
||||||
|
self._temp_buffer = []
|
||||||
|
|
||||||
formatter = Formatter('%(levelname)s - %(message)s')
|
formatter = Formatter('%(levelname)s - %(message)s')
|
||||||
self.setFormatter(formatter)
|
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)
|
self.setLevel(logging.DEBUG if config.get("debug", False) else logging.INFO)
|
||||||
|
logging.getLogger("").addHandler(self)
|
||||||
|
|
||||||
|
def emit(self, record):
|
||||||
msg = self.format(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 connect(self, gui):
|
||||||
def _write_to_console(root: 'GUI', msg):
|
self.renderer = lambda m: gui.call_in_thread(lambda: gui.write_to_console(m))
|
||||||
numlines = root._console.index('end - 1 line').split('.')[0]
|
while self._temp_buffer:
|
||||||
root._console['state'] = 'normal'
|
self.renderer(self._temp_buffer.pop(0))
|
||||||
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'
|
|
||||||
|
Loading…
Reference in New Issue
Block a user