diff options
author | Eric Callahan <arksine.code@gmail.com> | 2021-08-21 11:30:01 -0400 |
---|---|---|
committer | KevinOConnor <kevin@koconnor.net> | 2021-08-21 15:00:29 -0400 |
commit | c64ea474d70b235b8e75a20840b8dc93ffdda004 (patch) | |
tree | 97a10645a7d63caacd4ef4cb9d08aa915bc8d69f /klippy/webhooks.py | |
parent | 23bb6fa1f357ab6cd8c4c3a7092512f7ca1739ba (diff) | |
download | kutter-c64ea474d70b235b8e75a20840b8dc93ffdda004.tar.gz kutter-c64ea474d70b235b8e75a20840b8dc93ffdda004.tar.xz kutter-c64ea474d70b235b8e75a20840b8dc93ffdda004.zip |
webhooks: log client requests on shutdown
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Diffstat (limited to 'klippy/webhooks.py')
-rw-r--r-- | klippy/webhooks.py | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/klippy/webhooks.py b/klippy/webhooks.py index 3865d0d6..da951279 100644 --- a/klippy/webhooks.py +++ b/klippy/webhooks.py @@ -3,9 +3,11 @@ # Copyright (C) 2020 Eric Callahan <arksine.code@gmail.com> # # This file may be distributed under the terms of the GNU GPLv3 license -import logging, socket, os, sys, errno, json +import logging, socket, os, sys, errno, json, collections import gcode +REQUEST_LOG_SIZE = 20 + # Json decodes strings as unicode types in Python 2.x. This doesn't # play well with some parts of Klipper (particuarly displays), so we # need to create an object hook. This solution borrowed from: @@ -119,6 +121,8 @@ class ServerSocket: self.sock.fileno(), self._handle_accept) printer.register_event_handler( 'klippy:disconnect', self._handle_disconnect) + printer.register_event_handler( + "klippy:shutdown", self._handle_shutdown) def _handle_accept(self, eventtime): try: @@ -139,6 +143,10 @@ class ServerSocket: except socket.error: pass + def _handle_shutdown(self): + for client in self.clients.values(): + client.dump_request_log() + def _remove_socket_file(self, file_path): try: os.remove(file_path) @@ -165,6 +173,15 @@ class ClientConnection: self.partial_data = self.send_buffer = "" self.is_sending_data = False self.set_client_info("?", "New connection") + self.request_log = collections.deque([], REQUEST_LOG_SIZE) + + def dump_request_log(self): + out = [] + out.append("Dumping %d requests for client %d" + % (len(self.request_log), self.uid,)) + for eventtime, request in self.request_log: + out.append("Received %f: %s" % (eventtime, request)) + logging.info("\n".join(out)) def set_client_info(self, client_info, state_msg=None): if state_msg is None: @@ -210,6 +227,7 @@ class ClientConnection: requests[0] = self.partial_data + requests[0] self.partial_data = requests.pop() for req in requests: + self.request_log.append((eventtime, req)) try: web_request = WebRequest(self, req) except Exception: |