From d09f75a815311ed0904868a28a35fa9599df23eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 5 Jun 2018 16:54:01 +0200 Subject: [PATCH] Add commdebug logger To help with debugging ping pong communication --- src/octoprint/__init__.py | 16 +++++++++++++++ .../comm/protocol/reprap/__init__.py | 20 +++++++++++++++++-- src/octoprint/printer/standard.py | 1 + 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/octoprint/__init__.py b/src/octoprint/__init__.py index 6eb99dbd9..63a74684b 100644 --- a/src/octoprint/__init__.py +++ b/src/octoprint/__init__.py @@ -197,6 +197,9 @@ def init_logging(settings, use_logging_file=True, logging_file=None, default_con }, "communication": { "format": "%(asctime)s - %(name)s - %(message)s" + }, + "commdebug": { + "format": "%(asctime)s - %(threadName)s - %(message)s" } }, "handlers": { @@ -229,6 +232,14 @@ def init_logging(settings, use_logging_file=True, logging_file=None, default_con "backupCount": 3, "filename": os.path.join(settings.getBaseFolder("logs"), "transport.log"), "delay": True + }, + "commDebugFile": { + "class": "octoprint.logging.handlers.CommunicationLogHandler", + "level": "DEBUG", + "formatter": "commdebug", + "backupCount": 3, + "filename": os.path.join(settings.getBaseFolder("logs"), "commdebug.log"), + "delay": True } }, "loggers": { @@ -242,6 +253,11 @@ def init_logging(settings, use_logging_file=True, logging_file=None, default_con "handlers": ["transportFile"], "propagate": False }, + "COMMDEBUG": { + "level": "DEBUG", + "handlers": ["commDebugFile"], + "propagate": False + }, "octoprint": { "level": "INFO" }, diff --git a/src/octoprint/comm/protocol/reprap/__init__.py b/src/octoprint/comm/protocol/reprap/__init__.py index 32f7090ea..b59d52462 100644 --- a/src/octoprint/comm/protocol/reprap/__init__.py +++ b/src/octoprint/comm/protocol/reprap/__init__.py @@ -99,6 +99,8 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt sd_status_interval_autoreport=1.0): super(ReprapGcodeProtocol, self).__init__() + self._logger = logging.getLogger(__name__) + self._commdebug_logger = logging.getLogger("COMMDEBUG") self._phase_logger = logging.getLogger(__name__ + ".command_phases") self.flavor = flavor if flavor is not None else GenericFlavor @@ -553,6 +555,7 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt self._logger.info(message) self.notify_listeners("on_protocol_log", self, message + " " + general_message) if self._send_same_from_resend(): + self._commdebug_logger.debug("on_comm_timeout => clear_to_send.set: timeout during active resend (counter: {})".format(self._clear_to_send.counter)) self._clear_to_send.set() elif self._internal_flags["heating"]: @@ -571,6 +574,7 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt self._logger.info(message) self.notify_listeners("on_protocol_log_message", self, message + " " + general_message) if self._send_command(GcodeCommand("M105", type="temperature")): + self._commdebug_logger.debug("on_comm_timeout => clear_to_send.set: timeout while printing (counter: {})".format(self._clear_to_send.counter)) self._clear_to_send.set() elif self._clear_to_send.blocked(): @@ -578,13 +582,14 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt message = "Communication timeout while idle, trying to trigger response from printer." self._logger.info(message) self.notify_listeners("on_protocol_log_message", self, message + " " + general_message) + self._commdebug_logger.debug("on_comm_timeout => clear_to_send.set: timeout while idle (counter: {})".format(self._clear_to_send.counter)) self._clear_to_send.set() self._internal_flags["ok_timeout"] = self._get_timeout("communication_busy" if self._internal_flags["busy_detected"] else "communication") return self.state != ProtocolState.PROCESSING and line == "" - def _on_comm_ok(self): + def _on_comm_ok(self, wait=False): if self._internal_flags["ignore_ok"] > 0: self._internal_flags["ignore_ok"] -= 1 if self._internal_flags["ignore_ok"] < 0: @@ -597,6 +602,7 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt return self._internal_flags["ok_timeout"] = self._get_timeout("communication_busy" if self._internal_flags["busy_detected"] else "communication") + self._commdebug_logger.debug("on_comm_ok => clear_to_send.set: {} (counter: {})".format("wait" if wait else "ok", self._clear_to_send.counter)) self._clear_to_send.set() # reset long running commands, persisted current tools and heatup counters on ok @@ -625,7 +631,7 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt def _on_comm_wait(self): if self.state == ProtocolState.PROCESSING: - self._on_comm_ok() + self._on_comm_ok(wait=True) def _on_comm_resend(self, linenumber): if linenumber is None: @@ -819,9 +825,11 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt def _tickle(self, command): if self._send_command(command): if self._clear_to_send.blocked(): + self._commdebug_logger.debug("tickle => clear_to_send.set (counter: {})".format(self._clear_to_send.counter)) self._clear_to_send.set() def _continue_sending(self): + self._commdebug_logger.debug("continue_sending (counter: {}, line number: {})".format(self._clear_to_send.counter, self._current_linenumber)) while self._active: if self.state in (ProtocolState.PROCESSING,) and not (self._job is None or not self._job.active or isinstance(self._job, SDFilePrintjob)): # we are printing, we really want to send either something from the command @@ -848,6 +856,7 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt return self._send_next_from_resend(again=True) def _send_next_from_resend(self, again=False): + self._commdebug_logger.debug("send_next_from_resend (counter: {}, line number: {})".format(self._clear_to_send.counter, self._internal_flags.get("resend_linenumber"))) self._last_communication_error = None # Make sure we are only handling one sending job at a time @@ -1082,6 +1091,7 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt # loop won't be triggered with the reply from this unsent command # now, so we try to tickle the processing of any active # command queues manually + self._commdebug_logger.debug("send_loop => no results") self._continue_sending() # and now let's fetch the next item from the queue @@ -1100,6 +1110,7 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt self._logger.info("Refusing to send an empty line to the printer") # same here, tickle the queues manually + self._commdebug_logger.debug("send_loop => empty line") self._continue_sending() # and fetch the next item @@ -1110,6 +1121,7 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt self._process_command_phase("sending", command) # tickle... + self._commdebug_logger.debug("send_loop => at command") self._continue_sending() # ... and fetch the next item @@ -1147,14 +1159,18 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt use_up_clear = self.flavor.unknown_requires_ack if isinstance(command, GcodeCommand): use_up_clear = True + else: + self._commdebug_logger.debug("send_loop => command != instanceof GcodeCommand: {!r}".format(command)) if use_up_clear: # if we need to use up a clear, do that now + self._commdebug_logger.debug("send_loop => clear_to_send.clear: line sent (counter: {}, line number: {})".format(self._clear_to_send.counter, self._current_linenumber)) self._clear_to_send.clear() else: # Otherwise we need to tickle the read queue - there might not be a reply # to this command, so our _monitor loop will stay waiting until timeout. We # definitely do not want that, so we tickle the queue manually here + self._commdebug_logger.debug("send_loop => use_up_clear == False") self._continue_sending() finally: diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index 5735287de..e207a5825 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -288,6 +288,7 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback, ProtocolListener, from octoprint.logging.handlers import CommunicationLogHandler CommunicationLogHandler.on_open_connection(u"TRANSPORT") CommunicationLogHandler.on_open_connection(u"PROTOCOL") + CommunicationLogHandler.on_open_connection(u"COMMDEBUG") from octoprint.comm.transport import lookup_transport transport_class = lookup_transport(selected_transport)