diff --git a/bin/devdeck b/bin/devdeck index f0c90d7..03bff01 100644 --- a/bin/devdeck +++ b/bin/devdeck @@ -3,23 +3,39 @@ import logging import os import sys import threading +from logging.handlers import RotatingFileHandler from pathlib import Path from StreamDeck.DeviceManager import DeviceManager from devdeck.devdeck import DevDeck +from devdeck.logging.filters import InfoFilter from devdeck.settings.devdeck_settings import DevDeckSettings from devdeck.settings.validation_error import ValidationError if __name__ == "__main__": + os.makedirs(os.path.join(str(Path.home()), '.devdeck'), exist_ok=True) + root = logging.getLogger('devdeck') root.setLevel(logging.DEBUG) - handler = logging.StreamHandler(sys.stdout) - handler.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') - handler.setFormatter(formatter) - root.addHandler(handler) + + info_handler = logging.StreamHandler(sys.stdout) + info_handler.setLevel(logging.INFO) + info_handler.setFormatter(formatter) + info_handler.addFilter(InfoFilter()) + root.addHandler(info_handler) + + error_handler = logging.StreamHandler(sys.stderr) + error_handler.setLevel(logging.WARNING) + error_handler.setFormatter(formatter) + root.addHandler(error_handler) + + fileHandler = RotatingFileHandler(os.path.join(str(Path.home()), '.devdeck', 'devdeck.log'), maxBytes=100000, + backupCount=5) + fileHandler.setFormatter(formatter) + root.addHandler(fileHandler) streamdecks = DeviceManager().enumerate() @@ -34,7 +50,6 @@ if __name__ == "__main__": deck.close() if len(serial_numbers) > 0: root.info("Generating a setting file as none exist: %s", settings_filename) - os.makedirs(os.path.join(str(Path.home()), '.devdeck'), exist_ok=True) DevDeckSettings.generate_default(settings_filename, serial_numbers) else: root.info("""No stream deck connected. Please connect a stream deck to generate an initial config file. \n diff --git a/devdeck/controls/command_control.py b/devdeck/controls/command_control.py index 6b959a4..fdbabe4 100644 --- a/devdeck/controls/command_control.py +++ b/devdeck/controls/command_control.py @@ -1,3 +1,4 @@ +import logging import os from subprocess import Popen, DEVNULL @@ -5,10 +6,17 @@ class CommandControl(DeckControl): + def __init__(self, key_no, **kwargs): + self.__logger = logging.getLogger('devdeck') + super().__init__(key_no, **kwargs) + def initialize(self): with self.deck_context() as context: with context.renderer() as r: r.image(os.path.expanduser(self.settings['icon'])).end() def pressed(self): - Popen(self.settings['command'], stdout=DEVNULL, stderr=DEVNULL) + try: + Popen(self.settings['command'], stdout=DEVNULL, stderr=DEVNULL) + except Exception as ex: + self.__logger.error("Error executing command %s: %s", self.settings['command'], str(ex)) \ No newline at end of file diff --git a/devdeck/logging/__init__.py b/devdeck/logging/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/devdeck/logging/filters.py b/devdeck/logging/filters.py new file mode 100644 index 0000000..590906f --- /dev/null +++ b/devdeck/logging/filters.py @@ -0,0 +1,6 @@ +import logging + + +class InfoFilter(logging.Filter): + def filter(self, rec): + return rec.levelno in (logging.DEBUG, logging.INFO)