diff --git a/app_state.py b/app_state.py index b1efef1..4636570 100644 --- a/app_state.py +++ b/app_state.py @@ -8,7 +8,7 @@ so later rebinding would only ever be seen by the module that did it. import configparser import os -VERSION = "0.0.0.171" +VERSION = "0.0.0.172" PROPERTIES_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "editor.properties") DEFAULT_GRID_SIZE = 10 DEFAULT_ADD_TIMESTAMP = True diff --git a/dasp.py b/dasp.py index 4f62ec9..f2f0098 100644 --- a/dasp.py +++ b/dasp.py @@ -98,7 +98,43 @@ def _trace(direction, msg, datagram=None): hexdump = "" if datagram: hexdump = " [" + datagram.hex(" ") + "]" - _trace_out(f"[DASP] {ts} {direction} {name}{seq}{ack} payload={plen}{hexdump}") + line = f"[DASP] {ts} {direction} {name}{seq}{ack} payload={plen}{hexdump}" + color = _TRACE_COLORS.get(direction) + if color and _console_colors(): + line = f"{color}{line}\033[0m" + _trace_out(line) + + +# Sent lines yellow, received lines green, so the two directions read apart at a glance. +_TRACE_COLORS = {"S": "\033[93m", "R": "\033[92m"} +_colors_ok = None + + +def _console_colors(): + """True when stdout is a console that understands ANSI colour codes. + + Windows consoles need virtual-terminal processing switched on first; a pipe or + file gets plain text so captured logs stay free of escape codes. + """ + global _colors_ok + if _colors_ok is None: + import sys + stream = sys.__stdout__ + try: + _colors_ok = stream is not None and stream.isatty() + except (OSError, ValueError): + _colors_ok = False + if _colors_ok and os.name == "nt": + try: + import ctypes + kernel32 = ctypes.windll.kernel32 + handle = kernel32.GetStdHandle(-11) # STD_OUTPUT_HANDLE + mode = ctypes.c_uint32() + _colors_ok = bool(kernel32.GetConsoleMode(handle, ctypes.byref(mode)) + and kernel32.SetConsoleMode(handle, mode.value | 0x0004)) + except (OSError, AttributeError): + _colors_ok = False + return _colors_ok # Trace lines are printed by a thread of their own. The send and receive threads must diff --git a/editor.properties b/editor.properties index 693fc10..780d2ca 100644 --- a/editor.properties +++ b/editor.properties @@ -7,7 +7,7 @@ left_pane_width = 230 trace_dasp = True [Device] -host = 192.168.1.251 +host = localhost port = 1876 user = admin