[sans-io] improve layer debugging

This commit is contained in:
Maximilian Hils 2017-12-16 00:48:14 +01:00
parent 005a5afe47
commit 34274744a1

View File

@ -21,6 +21,11 @@ class Layer:
context: Context context: Context
_paused: typing.Optional[Paused] _paused: typing.Optional[Paused]
_paused_event_queue: typing.Deque[events.Event] _paused_event_queue: typing.Deque[events.Event]
debug: typing.Optional[str] = None
"""
Enable debug logging by assigning a prefix string for log messages.
Different amounts of whitespace for different layers work well.
"""
def __init__(self, context: Context) -> None: def __init__(self, context: Context) -> None:
self.context = context self.context = context
@ -28,9 +33,6 @@ class Layer:
self._paused = None self._paused = None
self._paused_event_queue = collections.deque() self._paused_event_queue = collections.deque()
def _debug(self, *args):
pass # print(*args)
@abstractmethod @abstractmethod
def _handle_event(self, event: events.Event) -> commands.TCommandGenerator: def _handle_event(self, event: events.Event) -> commands.TCommandGenerator:
"""Handle a proxy server event""" """Handle a proxy server event"""
@ -44,12 +46,17 @@ class Layer:
isinstance(event, events.CommandReply) and isinstance(event, events.CommandReply) and
event.command is self._paused.command event.command is self._paused.command
) )
if self.debug is not None:
yield commands.Log(
f"{self.debug}{'>>' if pause_finished else '>!'} {event}", "debug"
)
if pause_finished: if pause_finished:
yield from self.__continue(event) yield from self.__continue(event)
else: else:
self._paused_event_queue.append(event) self._paused_event_queue.append(event)
self._debug("Paused Event Queue: " + repr(self._paused_event_queue))
else: else:
if self.debug is not None:
yield commands.Log(f"{self.debug}>> {event}", "debug")
command_generator = self._handle_event(event) command_generator = self._handle_event(event)
yield from self.__process(command_generator) yield from self.__process(command_generator)
@ -65,8 +72,10 @@ class Layer:
return return
while command: while command:
if self.debug is not None:
if not isinstance(command, commands.Log):
yield commands.Log(f"{self.debug}<< {command}", "debug")
if command.blocking is True: if command.blocking is True:
self._debug("start pausing")
command.blocking = self # assign to our layer so that higher layers don't block. command.blocking = self # assign to our layer so that higher layers don't block.
self._paused = Paused( self._paused = Paused(
command, command,
@ -80,17 +89,16 @@ class Layer:
def __continue(self, event: events.CommandReply): def __continue(self, event: events.CommandReply):
"""continue processing events after being paused""" """continue processing events after being paused"""
self._debug("continue")
command_generator = self._paused.generator command_generator = self._paused.generator
self._paused = None self._paused = None
yield from self.__process(command_generator, event.reply) yield from self.__process(command_generator, event.reply)
while not self._paused and self._paused_event_queue: while not self._paused and self._paused_event_queue:
event = self._paused_event_queue.popleft() event = self._paused_event_queue.popleft()
self._debug(f"<# Paused event: {event}") if self.debug is not None:
yield commands.Log(f"{self.debug}!> {event}", "debug")
command_generator = self._handle_event(event) command_generator = self._handle_event(event)
yield from self.__process(command_generator) yield from self.__process(command_generator)
self._debug("#>")
mevents = events # alias here because autocomplete above should not have aliased version. mevents = events # alias here because autocomplete above should not have aliased version.