Improve DASP trace: use S/R labels, add timestamp and full hex datagram
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+1
-1
@@ -8,7 +8,7 @@ so later rebinding would only ever be seen by the module that did it.
|
|||||||
import configparser
|
import configparser
|
||||||
import os
|
import os
|
||||||
|
|
||||||
VERSION = "0.0.0.093"
|
VERSION = "0.0.0.094"
|
||||||
PROPERTIES_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "editor.properties")
|
PROPERTIES_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "editor.properties")
|
||||||
DEFAULT_GRID_SIZE = 10
|
DEFAULT_GRID_SIZE = 10
|
||||||
DEFAULT_ADD_TIMESTAMP = True
|
DEFAULT_ADD_TIMESTAMP = True
|
||||||
|
|||||||
@@ -85,16 +85,20 @@ _MSG_TYPE_NAMES = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _trace(direction, msg, addr=None):
|
def _trace(direction, msg, datagram=None):
|
||||||
import app_state
|
import app_state
|
||||||
if not app_state.TRACE_DASP:
|
if not app_state.TRACE_DASP:
|
||||||
return
|
return
|
||||||
|
import datetime
|
||||||
|
ts = datetime.datetime.now().strftime("%H:%M:%S.%f")[:-3]
|
||||||
name = _MSG_TYPE_NAMES.get(msg.msg_type, f"?{msg.msg_type}")
|
name = _MSG_TYPE_NAMES.get(msg.msg_type, f"?{msg.msg_type}")
|
||||||
seq = f" seq={msg.seq_num}" if msg.seq_num >= 0 else ""
|
seq = f" seq={msg.seq_num}" if msg.seq_num >= 0 else ""
|
||||||
ack = f" ack={msg.ack}" if msg.ack >= 0 else ""
|
ack = f" ack={msg.ack}" if msg.ack >= 0 else ""
|
||||||
plen = len(msg.payload) if msg.payload else 0
|
plen = len(msg.payload) if msg.payload else 0
|
||||||
dest = f" {addr[0]}:{addr[1]}" if addr else ""
|
hexdump = ""
|
||||||
print(f"[DASP] {direction}{dest} {name}{seq}{ack} payload={plen}")
|
if datagram:
|
||||||
|
hexdump = " [" + datagram.hex(" ") + "]"
|
||||||
|
print(f"[DASP] {ts} {direction} {name}{seq}{ack} payload={plen}{hexdump}")
|
||||||
|
|
||||||
|
|
||||||
class DaspException(Exception):
|
class DaspException(Exception):
|
||||||
@@ -916,8 +920,8 @@ class DaspSocket:
|
|||||||
# ── Network I/O ─────────────────────────────────────────────
|
# ── Network I/O ─────────────────────────────────────────────
|
||||||
|
|
||||||
def _send_msg(self, session, msg):
|
def _send_msg(self, session, msg):
|
||||||
_trace(">>>", msg, (session.host, session.port))
|
|
||||||
data = msg.encode()
|
data = msg.encode()
|
||||||
|
_trace("S", msg, data)
|
||||||
with self._send_lock:
|
with self._send_lock:
|
||||||
try:
|
try:
|
||||||
self._sock.sendto(data, (session.host, session.port))
|
self._sock.sendto(data, (session.host, session.port))
|
||||||
@@ -931,8 +935,9 @@ class DaspSocket:
|
|||||||
nbytes, addr = self._sock.recvfrom_into(buf)
|
nbytes, addr = self._sock.recvfrom_into(buf)
|
||||||
if nbytes < 5:
|
if nbytes < 5:
|
||||||
continue
|
continue
|
||||||
|
raw = bytes(buf[:nbytes])
|
||||||
msg = DaspMsg.decode(buf, nbytes)
|
msg = DaspMsg.decode(buf, nbytes)
|
||||||
self._dispatch(msg, addr)
|
self._dispatch(msg, addr, raw)
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
continue
|
continue
|
||||||
except OSError:
|
except OSError:
|
||||||
@@ -940,8 +945,8 @@ class DaspSocket:
|
|||||||
continue
|
continue
|
||||||
break
|
break
|
||||||
|
|
||||||
def _dispatch(self, msg, addr):
|
def _dispatch(self, msg, addr, raw=None):
|
||||||
_trace("<<<", msg, addr)
|
_trace("R", msg, raw)
|
||||||
session_id = msg.session_id
|
session_id = msg.session_id
|
||||||
with self._sessions_lock:
|
with self._sessions_lock:
|
||||||
session = self._sessions.get(session_id)
|
session = self._sessions.get(session_id)
|
||||||
|
|||||||
Reference in New Issue
Block a user