Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions BlocksScreen/BlocksScreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import sys
import typing

import logger
from lib.panels.mainWindow import MainWindow
from logger import setup_logging
from PyQt6 import QtCore, QtGui, QtWidgets

_logger = logging.getLogger(name="logs/BlocksScreen.log")
QtGui.QGuiApplication.setAttribute(
QtCore.Qt.ApplicationAttribute.AA_SynthesizeMouseForUnhandledTouchEvents,
True,
Expand All @@ -22,13 +21,6 @@
RESET = "\033[0m"


def setup_app_loggers():
"""Setup logger"""
_ = logger.create_logger(name="logs/BlocksScreen.log", level=logging.DEBUG)
_logger = logging.getLogger(name="logs/BlocksScreen.log")
_logger.info("============ BlocksScreen Initializing ============")


def show_splash(window: typing.Optional[QtWidgets.QWidget] = None):
"""Show splash screen on app initialization"""
logo = QtGui.QPixmap("BlocksScreen/BlocksScreen/lib/ui/resources/logoblocks.png")
Expand All @@ -39,7 +31,16 @@ def show_splash(window: typing.Optional[QtWidgets.QWidget] = None):


if __name__ == "__main__":
setup_app_loggers()
setup_logging(
filename="logs/BlocksScreen.log",
level=logging.DEBUG, # File gets DEBUG+
console_output=True, # Print to terminal
console_level=logging.DEBUG, # Console gets DEBUG+
capture_stderr=True, # Capture X11 errors
capture_stdout=False, # Don't capture print()
)
_logger = logging.getLogger(__name__)
_logger.info("============ BlocksScreen Initializing ============")
BlocksScreen = QtWidgets.QApplication([])
BlocksScreen.setApplicationName("BlocksScreen")
BlocksScreen.setApplicationDisplayName("BlocksScreen")
Expand Down
16 changes: 9 additions & 7 deletions BlocksScreen/configfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

from helper_methods import check_file_on_path

logger = logging.getLogger(__name__)

HOME_DIR = os.path.expanduser("~/")
WORKING_DIR = os.getcwd()
DEFAULT_CONFIGFILE_PATH = pathlib.Path(HOME_DIR, "printer_data", "config")
Expand Down Expand Up @@ -267,9 +269,9 @@ def add_section(self, section: str) -> None:
self.config.add_section(section)
self.update_pending = True
except configparser.DuplicateSectionError as e:
logging.error(f'Section "{section}" already exists. {e}')
logger.error(f'Section "{section}" already exists. {e}')
except configparser.Error as e:
logging.error(f'Unable to add "{section}" section to configuration: {e}')
logger.error(f'Unable to add "{section}" section to configuration: {e}')

def add_option(
self,
Expand Down Expand Up @@ -297,9 +299,9 @@ def add_option(
self.config.set(section, option, value)
self.update_pending = True
except configparser.DuplicateOptionError as e:
logging.error(f"Option {option} already present on {section}: {e}")
logger.error(f"Option {option} already present on {section}: {e}")
except configparser.Error as e:
logging.error(
logger.error(
f'Unable to add "{option}" option to section "{section}": {e} '
)

Expand All @@ -324,7 +326,7 @@ def update_option(
self.config.set(section, option, str(value))
self.update_pending = True
except Exception as e:
logging.error(
logger.error(
f'Unable to update option "{option}" in section "{section}": {e}'
)

Expand All @@ -349,7 +351,7 @@ def save_configuration(self) -> None:
self.config.write(sio)
sio.close()
except Exception as e:
logging.error(
logger.error(
f"ERROR: Unable to save new configuration, something went wrong while saving updated configuration. {e}"
)
finally:
Expand Down Expand Up @@ -443,6 +445,6 @@ def get_configparser() -> BlocksScreenConfig:
config_object = BlocksScreenConfig(configfile=configfile, section="server")
config_object.load_config()
if not config_object.has_section("server"):
logging.error("Error loading configuration file for the application.")
logger.error("Error loading configuration file for the application.")
raise ConfigError("Section [server] is missing from configuration")
return config_object
6 changes: 4 additions & 2 deletions BlocksScreen/helper_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import struct
import typing

logger = logging.getLogger(__name__)

try:
ctypes.cdll.LoadLibrary("libXext.so.6")
libxext = ctypes.CDLL("libXext.so.6")
Expand Down Expand Up @@ -220,9 +222,9 @@ def disable_dpms() -> None:
set_dpms_mode(DPMSState.OFF)

except OSError as e:
logging.exception(f"OSError couldn't load DPMS library: {e}")
logger.exception(f"OSError couldn't load DPMS library: {e}")
except Exception as e:
logging.exception(f"Unexpected exception occurred {e}")
logger.exception(f"Unexpected exception occurred {e}")


def convert_bytes_to_mb(self, bytes: int | float) -> float:
Expand Down
8 changes: 5 additions & 3 deletions BlocksScreen/lib/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from PyQt6 import QtCore

logger = logging.getLogger(__name__)


class MachineControl(QtCore.QObject):
service_restart = QtCore.pyqtSignal(str, name="service-restart")
Expand Down Expand Up @@ -67,10 +69,10 @@ def _run_command(self, command: str):
)
return p.stdout.strip() + "\n" + p.stderr.strip()
except ValueError as e:
logging.error("Failed to parse command string '%s': '%s'", command, e)
logger.error("Failed to parse command string '%s': '%s'", command, e)
raise RuntimeError(f"Invalid command format: {e}") from e
except subprocess.CalledProcessError as e:
logging.error(
logger.error(
"Caught exception (exit code %d) failed to run command: %s \nStderr: %s",
e.returncode,
command,
Expand All @@ -82,4 +84,4 @@ def _run_command(self, command: str):
subprocess.TimeoutExpired,
FileNotFoundError,
):
logging.error("Caught exception failed to run command %s", command)
logger.error("Caught exception failed to run command %s", command)
36 changes: 17 additions & 19 deletions BlocksScreen/lib/moonrakerComm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from lib.utils.RepeatedTimer import RepeatedTimer
from PyQt6 import QtCore, QtWidgets

_logger = logging.getLogger(name="logs/BlocksScreen.log")
logger = logging.getLogger(__name__)


class OneShotTokenError(Exception):
Expand Down Expand Up @@ -67,7 +67,7 @@ def __init__(self, parent: QtCore.QObject) -> None:
)

self.klippy_state_signal.connect(self.api.request_printer_info)
_logger.info("Websocket object initialized")
logger.info("Websocket object initialized")

@QtCore.pyqtSlot(name="retry_wb_conn")
def retry_wb_conn(self):
Expand Down Expand Up @@ -102,10 +102,10 @@ def reconnect(self):
else:
raise TypeError("QApplication.instance expected ad non-None value")
except Exception as e:
_logger.error(
logger.error(
f"Error on sending Event {unable_to_connect_event.__class__.__name__} | Error message: {e}"
)
_logger.info(
logger.info(
"Maximum number of connection retries reached, Unable to establish connection with Moonraker"
)
return False
Expand All @@ -114,11 +114,11 @@ def reconnect(self):
def connect(self) -> bool:
"""Connect to websocket"""
if self.connected:
_logger.info("Connection established")
logger.info("Connection established")
return True
self._reconnect_count += 1
self.connecting_signal[int].emit(int(self._reconnect_count))
_logger.debug(
logger.debug(
f"Establishing connection to Moonraker...\n Try number {self._reconnect_count}"
)
# TODO Handle if i cannot connect to moonraker, request server.info and see if i get a result
Expand All @@ -127,7 +127,7 @@ def connect(self) -> bool:
if _oneshot_token is None:
raise OneShotTokenError("Unable to retrieve oneshot token")
except Exception as e:
_logger.info(
logger.info(
f"Unexpected error occurred when trying to acquire oneshot token: {e}"
)
return False
Expand All @@ -148,11 +148,11 @@ def connect(self) -> bool:
daemon=True,
)
try:
_logger.info("Websocket Start...")
_logger.debug(self.ws.url)
logger.info("Websocket Start...")
logger.debug(self.ws.url)
self._wst.start()
except Exception as e:
_logger.info(f"Unexpected while starting websocket {self._wst.name}: {e}")
logger.info(f"Unexpected while starting websocket {self._wst.name}: {e}")
return False
return True

Expand All @@ -162,14 +162,14 @@ def wb_disconnect(self) -> None:
self.ws.close()
if self._wst.is_alive():
self._wst.join()
_logger.info("Websocket closed")
logger.info("Websocket closed")

def on_error(self, *args) -> None:
"""Websocket error callback"""
# First argument is ws second is error message
# TODO: Handle error messages
_error = args[1] if len(args) == 2 else args[0]
_logger.info(f"Websocket error, disconnected: {_error}")
logger.info(f"Websocket error, disconnected: {_error}")
self.connected = False
self.disconnected = True

Expand Down Expand Up @@ -199,11 +199,11 @@ def on_close(self, *args) -> None:
else:
raise TypeError("QApplication.instance expected non None value")
except Exception as e:
_logger.info(
logger.info(
f"Unexpected error when sending websocket close_event on disconnection: {e}"
)

_logger.info(
logger.info(
f"Websocket closed, code: {_close_status_code}, message: {_close_message}"
)

Expand Down Expand Up @@ -231,11 +231,11 @@ def on_open(self, *args) -> None:
else:
raise TypeError("QApplication.instance expected non None value")
except Exception as e:
_logger.info(f"Unexpected error opening websocket: {e}")
logger.info(f"Unexpected error opening websocket: {e}")

self.connected_signal.emit()
self._retry_timer.stopTimer()
_logger.info(f"Connection to websocket achieved on {_ws}")
logger.info(f"Connection to websocket achieved on {_ws}")

def on_message(self, *args) -> None:
"""Websocket on message callback
Expand Down Expand Up @@ -300,9 +300,7 @@ def on_message(self, *args) -> None:
else:
raise TypeError("QApplication.instance expected non None value")
except Exception as e:
_logger.info(
f"Unexpected error while creating websocket message event: {e}"
)
logger.info(f"Unexpected error while creating websocket message event: {e}")

def send_request(self, method: str, params: dict = {}) -> bool:
"""Send a request over the websocket
Expand Down
4 changes: 3 additions & 1 deletion BlocksScreen/lib/moonrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import requests
from requests import Request, Response

logger = logging.getLogger(__name__)


class UncallableError(Exception):
"""Raised when a method is not callable"""
Expand Down Expand Up @@ -145,4 +147,4 @@ def _request(
return response.json() if json_response else response.content

except Exception as e:
logging.info(f"Unexpected error while sending HTTP request: {e}")
logger.info(f"Unexpected error while sending HTTP request: {e}")
13 changes: 3 additions & 10 deletions BlocksScreen/lib/panels/mainWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
from lib.ui.resources.main_menu_resources_rc import *
from lib.ui.resources.system_resources_rc import *
from lib.ui.resources.top_bar_resources_rc import *
from logger import LogManager
from PyQt6 import QtCore, QtGui, QtWidgets
from screensaver import ScreenSaver

_logger = logging.getLogger(name="logs/BlocksScreen.log")
_logger = logging.getLogger(__name__)


def api_handler(func):
Expand Down Expand Up @@ -782,16 +783,8 @@ def closeEvent(self, a0: typing.Optional[QtGui.QCloseEvent]) -> None:
except Exception as e:
_logger.warning("Network panel shutdown error: %s", e)

_loggers = [
logging.getLogger(name) for name in logging.root.manager.loggerDict
] # Get available logger handlers
for logger in _loggers: # noqa: F402
if hasattr(logger, "cancel"):
_callback = getattr(logger, "cancel")
if callable(_callback):
_callback()
self.ws.wb_disconnect()
self.close()
LogManager.shutdown()
if a0 is None:
return
QtWidgets.QMainWindow.closeEvent(self, a0)
Expand Down
2 changes: 1 addition & 1 deletion BlocksScreen/lib/panels/printTab.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from lib.utils.display_button import DisplayButton
from PyQt6 import QtCore, QtGui, QtWidgets

logger = logging.getLogger(name="logs/BlocksScreen.log")
logger = logging.getLogger(__name__)


class PrintTab(QtWidgets.QStackedWidget):
Expand Down
4 changes: 3 additions & 1 deletion BlocksScreen/lib/panels/widgets/connectionPage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from lib.ui.connectionWindow_ui import Ui_ConnectivityForm
from PyQt6 import QtCore, QtWidgets

logger = logging.getLogger(__name__)


class ConnectionPage(QtWidgets.QFrame):
text_updated = QtCore.pyqtSignal(int, name="connection_text_updated")
Expand Down Expand Up @@ -141,7 +143,7 @@ def text_update(self, text: int | str | None = None):
if self.state == "shutdown" and self.message is not None:
return False
self.dot_timer.stop()
logging.debug(f"[ConnectionWindowPanel] text_update: {text}")
logger.debug(f"[ConnectionWindowPanel] text_update: {text}")
if text == "wb lost":
self.panel.connectionTextBox.setText("Moonraker connection lost")
if text is None:
Expand Down
2 changes: 1 addition & 1 deletion BlocksScreen/lib/panels/widgets/jobStatusPage.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from lib.utils.display_button import DisplayButton
from PyQt6 import QtCore, QtGui, QtWidgets

logger = logging.getLogger("logs/BlocksScreen.log")
logger = logging.getLogger(__name__)


class JobStatusWidget(QtWidgets.QWidget):
Expand Down
4 changes: 2 additions & 2 deletions BlocksScreen/lib/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from lib.moonrakerComm import MoonWebSocket
from PyQt6 import QtCore, QtWidgets

logger = logging.getLogger(name="logs/BlocksScreen.logs")
logger = logging.getLogger(__name__)


class Printer(QtCore.QObject):
Expand Down Expand Up @@ -511,7 +511,7 @@ def send_print_event(self, event: str):
_print_state_upper = event[0].upper()
_print_state_call = f"{_print_state_upper}{event[1:]}"
if hasattr(events, f"Print{_print_state_call}"):
logging.debug(
logger.debug(
"Print Event Caught, print is %s, calling event %s",
_print_state_call,
f"Print{_print_state_call}",
Expand Down
Loading