Only print command of event handler to log when debug flag is present

(cherry picked from commit 49d28c5)
This commit is contained in:
Gina Häußge 2015-06-29 20:54:13 +02:00
parent bb0bfaee56
commit 2a8eea086c
2 changed files with 35 additions and 13 deletions

View file

@ -366,6 +366,27 @@ Use the following settings to add shell/gcode commands to be executed on certain
type: gcode type: gcode
enabled: False 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: .. _sec-configuration-config_yaml-feature:
Feature Feature

View file

@ -254,10 +254,11 @@ class CommandTrigger(GenericEventListener):
event = subscription["event"] event = subscription["event"]
command = subscription["command"] command = subscription["command"]
commandType = subscription["type"] commandType = subscription["type"]
debug = subscription["debug"] if "debug" in subscription else False
if not event in self._subscriptions.keys(): if not event in self._subscriptions.keys():
self._subscriptions[event] = [] self._subscriptions[event] = []
self._subscriptions[event].append((command, commandType)) self._subscriptions[event].append((command, commandType, debug))
if not event in eventsToSubscribe: if not event in eventsToSubscribe:
eventsToSubscribe.append(event) eventsToSubscribe.append(event)
@ -275,7 +276,7 @@ class CommandTrigger(GenericEventListener):
if not event in self._subscriptions: if not event in self._subscriptions:
return return
for command, commandType in self._subscriptions[event]: for command, commandType, debug in self._subscriptions[event]:
try: try:
if isinstance(command, (tuple, list, set)): if isinstance(command, (tuple, list, set)):
processedCommand = [] processedCommand = []
@ -283,19 +284,20 @@ class CommandTrigger(GenericEventListener):
processedCommand.append(self._processCommand(c, payload)) processedCommand.append(self._processCommand(c, payload))
else: else:
processedCommand = self._processCommand(command, payload) processedCommand = self._processCommand(command, payload)
self.executeCommand(processedCommand, commandType) self.executeCommand(processedCommand, commandType, debug=debug)
except KeyError, e: except KeyError, e:
self._logger.warn("There was an error processing one or more placeholders in the following command: %s" % command) 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": if commandType == "system":
self._executeSystemCommand(command) self._executeSystemCommand(command, debug=debug)
elif commandType == "gcode": elif commandType == "gcode":
self._executeGcodeCommand(command) self._executeGcodeCommand(command, debug=debug)
def _executeSystemCommand(self, command): def _executeSystemCommand(self, command, debug=False):
def commandExecutioner(command): 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) subprocess.Popen(command, shell=True)
try: try:
@ -306,16 +308,15 @@ class CommandTrigger(GenericEventListener):
commandExecutioner(command) commandExecutioner(command)
except subprocess.CalledProcessError, e: except subprocess.CalledProcessError, e:
self._logger.warn("Command failed with return code %i: %s" % (e.returncode, str(e))) self._logger.warn("Command failed with return code %i: %s" % (e.returncode, str(e)))
except Exception, ex: except:
self._logger.exception("Command failed") self._logger.exception("Command failed")
def _executeGcodeCommand(self, command): def _executeGcodeCommand(self, command, debug=False):
commands = [command] commands = [command]
if isinstance(command, (list, tuple, set)): if isinstance(command, (list, tuple, set)):
self._logger.debug("Executing GCode commands: %r" % command)
commands = list(command) commands = list(command)
else: if debug:
self._logger.debug("Executing GCode command: %s" % command) self._logger.info("Executing GCode commands: %r" % command)
self._printer.commands(commands) self._printer.commands(commands)
def _processCommand(self, command, payload): def _processCommand(self, command, payload):