From 2a8eea086c7cb25fc2d09a22799e1f7dd0ad690e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 29 Jun 2015 20:54:13 +0200 Subject: [PATCH] Only print command of event handler to log when debug flag is present (cherry picked from commit 49d28c5) --- docs/configuration/config_yaml.rst | 21 +++++++++++++++++++++ src/octoprint/events.py | 27 ++++++++++++++------------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/docs/configuration/config_yaml.rst b/docs/configuration/config_yaml.rst index f698a3ba..15cfbbbf 100644 --- a/docs/configuration/config_yaml.rst +++ b/docs/configuration/config_yaml.rst @@ -366,6 +366,27 @@ Use the following settings to add shell/gcode commands to be executed on certain type: gcode enabled: False +.. note:: + + For debugging purposes, you can also add an additional property ``debug`` to your event subscription definitions + that if set to true will make the event handler print a log line with your subscription's command after performing + all placeholder replacements. Example: + + .. code-block:: yaml + + events: + subscriptions: + - event: Startup + command: "logger 'OctoPrint started up'" + type: system + debug: true + + This will be logged in OctoPrint's logfile as + + .. code-block:: none + + Executing System Command: logger 'OctoPrint started up' + .. _sec-configuration-config_yaml-feature: Feature diff --git a/src/octoprint/events.py b/src/octoprint/events.py index cbcbf3b8..a26a48e8 100644 --- a/src/octoprint/events.py +++ b/src/octoprint/events.py @@ -254,10 +254,11 @@ class CommandTrigger(GenericEventListener): event = subscription["event"] command = subscription["command"] commandType = subscription["type"] + debug = subscription["debug"] if "debug" in subscription else False if not event in self._subscriptions.keys(): self._subscriptions[event] = [] - self._subscriptions[event].append((command, commandType)) + self._subscriptions[event].append((command, commandType, debug)) if not event in eventsToSubscribe: eventsToSubscribe.append(event) @@ -275,7 +276,7 @@ class CommandTrigger(GenericEventListener): if not event in self._subscriptions: return - for command, commandType in self._subscriptions[event]: + for command, commandType, debug in self._subscriptions[event]: try: if isinstance(command, (tuple, list, set)): processedCommand = [] @@ -283,19 +284,20 @@ class CommandTrigger(GenericEventListener): processedCommand.append(self._processCommand(c, payload)) else: processedCommand = self._processCommand(command, payload) - self.executeCommand(processedCommand, commandType) + self.executeCommand(processedCommand, commandType, debug=debug) except KeyError, e: self._logger.warn("There was an error processing one or more placeholders in the following command: %s" % command) - def executeCommand(self, command, commandType): + def executeCommand(self, command, commandType, debug=False): if commandType == "system": - self._executeSystemCommand(command) + self._executeSystemCommand(command, debug=debug) elif commandType == "gcode": - self._executeGcodeCommand(command) + self._executeGcodeCommand(command, debug=debug) - def _executeSystemCommand(self, command): + def _executeSystemCommand(self, command, debug=False): def commandExecutioner(command): - self._logger.info("Executing system command: %s" % command) + if debug: + self._logger.info("Executing system command: %s" % command) subprocess.Popen(command, shell=True) try: @@ -306,16 +308,15 @@ class CommandTrigger(GenericEventListener): commandExecutioner(command) except subprocess.CalledProcessError, e: self._logger.warn("Command failed with return code %i: %s" % (e.returncode, str(e))) - except Exception, ex: + except: self._logger.exception("Command failed") - def _executeGcodeCommand(self, command): + def _executeGcodeCommand(self, command, debug=False): commands = [command] if isinstance(command, (list, tuple, set)): - self._logger.debug("Executing GCode commands: %r" % command) commands = list(command) - else: - self._logger.debug("Executing GCode command: %s" % command) + if debug: + self._logger.info("Executing GCode commands: %r" % command) self._printer.commands(commands) def _processCommand(self, command, payload):